How put the correct string in VBA Access to Post to Json



  • The code below is posting on blanks { }

    Option Compare Database
    Option Explicit
    Private Sub CmdSales_Click()
    Dim http As Object
    Dim JSON As Object
    Dim i As Integer
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim item As Object
    Set http = CreateObject("MSXML2.XMLHTTP")
    Set db = CurrentDb
    Set rs = db.OpenRecordset("Qry1")
    http.Open "POST", "http://jsonplaceholder.typicode.com/Invoices/?inv=" & Me.CboInv, False
    http.send
    Set JSON = ParseJson(http.responseText)
    i = 2
    For Each item In JSON

        With rs
            .AddNew
            ![INV] = item("INV")
            ![Customer] = item("Customer")
            ![TaxId] = item("TaxId")
            ![Address] = item("Address")
            ![InvoiceDate] = item("InvoiceDate")
            ![Product] = item("Product")
            ![Qty] = item("Qty")
            ![Price] = item("Price")
            ![VAT] = item("VAT")
            ![TotalPrice] = item("TotalPrice")
            End With
        i = i + 1
    Next
    rs.Close
    Set rs = Nothing
    Set db = Nothing
    Set JSON = Nothing
    Set http = Nothing
    
    MsgBox ("complete")
    

    End Sub

    You help will be highly appreciated!


  • administrators

    @nector You should understand the code instead of copy-pasting. Above code neither generates JSON nor posting any data. I've already mentioned that posting data is done by sending it with send method using http.send data

    You can do something like this

    Dim postData As New Dictionary
    postData("id") = 1
    postData("name") = "codingislove"
    http.send ConvertToJson(postData, Whitespace:=2)
    

    Above code will post {"id: 1, "name": "codingislove"} data to the API
    Copy pasting the code won't work. Do understand the code and implement it as required.


Log in to reply
 

Looks like your connection to Codingislove Forum was lost, please wait while we try to reconnect.