I have created some code, with some help from ChatGPT, to consume an API to retrieve data and store it in a Microsoft Access table. It does not give me any errors, but it only adds one blank row into the table. I'm only trying to add one field called link to cut down on troubleshooting. Any pointers would be greatly appreciated.
Sub ConsumeAPIAndInsertData()
Dim url As String
Dim jsonData As String
Dim httpRequest As Object
Dim response As String
Dim jsonResponse As Object
Dim db As DAO.Database
Dim rs As DAO.Recordset
' Set the URL for the API endpoint
url = "HIDDENTOFORPROTECTION"
' Create the JSON data to send
jsonData = "{""token"": ""HIDDENTOFORPROTECTION"", ""pretty"": true, ""form_id"": 555555}"
' Create a new XMLHTTP object
Set httpRequest = CreateObject("MSXML2.XMLHTTP.6.0")
' Open the request
httpRequest.Open "POST", url, False
httpRequest.setRequestHeader "Content-Type", "application/json"
' Send the request with JSON data
httpRequest.send jsonData
' Get the response text
response = httpRequest.responseText
' Debug: Print the raw response
Debug.Print response
' Parse the JSON response
Set jsonResponse = JsonConverter.ParseJson(response)
' Check if jsonResponse is a dictionary/object
If TypeName(jsonResponse) = "Dictionary" Then
' Debug: Check the structure of the parsed JSON
Dim key As Variant
For Each key In jsonResponse.Keys
'Debug.Print key & ": " & jsonResponse(key)
Next key
' Open the database and table
Set db = CurrentDb
Set rs = db.OpenRecordset("Playground", dbOpenDynaset)
' Add a new record to the Playground table
rs.AddNew
' Check if "link" exists and handle the value
If Not IsNull(jsonResponse("link")) Then
rs!link = jsonResponse("link")
Else
rs!link = "" ' Set to empty if link is null
End If
' Update the record
rs.Update
' Clean up
rs.Close
Set rs = Nothing
Set db = Nothing
Else
Debug.Print "Response is not a dictionary."
End If
Set jsonResponse = Nothing
Set httpRequest = Nothing
MsgBox "Data inserted successfully!"
End Sub
Sub ConsumeAPIAndInsertData()
Dim url As String
Dim jsonData As String
Dim httpRequest As Object
Dim response As String
Dim jsonResponse As Object
Dim db As DAO.Database
Dim rs As DAO.Recordset
' Set the URL for the API endpoint
url = "HIDDENTOFORPROTECTION"
' Create the JSON data to send
jsonData = "{""token"": ""HIDDENTOFORPROTECTION"", ""pretty"": true, ""form_id"": 555555}"
' Create a new XMLHTTP object
Set httpRequest = CreateObject("MSXML2.XMLHTTP.6.0")
' Open the request
httpRequest.Open "POST", url, False
httpRequest.setRequestHeader "Content-Type", "application/json"
' Send the request with JSON data
httpRequest.send jsonData
' Get the response text
response = httpRequest.responseText
' Debug: Print the raw response
Debug.Print response
' Parse the JSON response
Set jsonResponse = JsonConverter.ParseJson(response)
' Check if jsonResponse is a dictionary/object
If TypeName(jsonResponse) = "Dictionary" Then
' Debug: Check the structure of the parsed JSON
Dim key As Variant
For Each key In jsonResponse.Keys
'Debug.Print key & ": " & jsonResponse(key)
Next key
' Open the database and table
Set db = CurrentDb
Set rs = db.OpenRecordset("Playground", dbOpenDynaset)
' Add a new record to the Playground table
rs.AddNew
' Check if "link" exists and handle the value
If Not IsNull(jsonResponse("link")) Then
rs!link = jsonResponse("link")
Else
rs!link = "" ' Set to empty if link is null
End If
' Update the record
rs.Update
' Clean up
rs.Close
Set rs = Nothing
Set db = Nothing
Else
Debug.Print "Response is not a dictionary."
End If
Set jsonResponse = Nothing
Set httpRequest = Nothing
MsgBox "Data inserted successfully!"
End Sub