Hello everyone,
Forgive me if this is a dumb question, because this is really my first time using MS Access, so I may be going about this all wrong.
I'm trying to make an API call, to post data to a website, and I'm getting stuck on how perform a loop inside of the call.
Here's my code:
Code:
Private Sub Command5_Click()
Dim result As String
Dim URL As String, postData As String
Dim winHttpReq As Object
Set winHttpReq = CreateObject("WinHttp.WinHttpRequest.5.1")
Dim rs As DAO.Recordset
' -- SQL QUERY
Set rs = CurrentDb.OpenRecordset("SELECT * FROM Orders WHERE OrderNumber = 1003")
' -- BEGIN THE API CALL
If Not rs.EOF Then
URL = "https://websiteURL.com?token=myAPItoken&xmlData=" & _
"<Invoices>" & _
"<row no='1'>" & _
"<FL val='Customer Number'>" & rs.Fields("cid") & "</FL>" & _
"<FL val='Name'>" & rs.Fields("name") & "</FL>" & _
"<FL val='Product Details'>"
' -- QUERY MULTIPLE PRODUCT DETAILS BASED ON THE CURRENT ORDER NUMBER
Set prods = CurrentDb.OpenRecordset("SELECT * FROM OrderDetails WHERE OrderNumber = 1003")
With prods
Do While Not .BOF
'Do something with the recordset/Your Code Goes Here
"<product no='1'>" & _
"<FL val='Product Id'>prodsField("prodID")</FL>" & _
"<FL val='Product Name'>prodsField("prodName")</FL>" & _
"<FL val='Unit Price'>prodsField("price")</FL>" & _
"<FL val='Quantity'>prodsField("qty")</FL>" & _
"</product>"
Loop
End If
End With
"</FL>" & _
"</row>" & _
"</Invoices>"
winHttpReq.Open "POST", URL, False
winHttpReq.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
winHttpReq.Send (postData)
result = winHttpReq.responseText
End If
End Sub
The issue I'm running in to is that the 'Do While' loop breaks the API URL. I'm not sure how to interject the loop inside of the API call.
If anyone can help me with this, I'd really appreciate it!
Thanks!