Populating a combobox from XMLHTTP responseText

AndyMb

New Member
Joined
Jul 1, 2015
Messages
20
Hi,

My code below fetches a list of addresses from a Postcode via an API call

Currently it spits out the results to column A and works well.

What I want is for the results to populate a combobox instead.

VBA Code:
Private Sub CommandButton1_Click()

    Dim objHTTP As Object
    Dim MyScript As Object
    Dim i As Long
    Dim myData As Object
    Dim postC, urlX As String
    Set MyScript = CreateObject("MSScriptControl.ScriptControl")
    MyScript.Language = "JScript"
    ComboBox4.Enabled = True
    postC = TextBox10.Value
    urlX = "https://api.ideal-postcodes.co.uk/v1/postcodes/" & postC & "?api_key=iddqd"
    URL = urlX

    Set objHTTP = CreateObject("MSXML2.XMLHTTP")
    objHTTP.Open "GET", URL, False
    objHTTP.send
    
    Set RetVal = MyScript.Eval("(" + objHTTP.responseText + ")")
    objHTTP.abort
    
    i = 2
    
   For Each myData In MyList
       Cells(i, 1).Value = myData.line_1
       i = i + 1
   Next
    
    Set MyList = Nothing
    Set objHTTP = Nothing
    Set MyScript = Nothing
    
End Sub

I have tried replacing
Code:
   For Each myData In MyList

       Cells(i, 1).Value = myData.line_1

       i = i + 1

   Next

with

Code:
For Each myData In MyList
ComboBox4.AddItem
Next

but that doesn't work probably because I need to somehow split myData.line_1 from the rest of the results?

myData.line_1 is the only list of values I want from the XML source

thanks
 

Some videos you may like

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple

AndyMb

New Member
Joined
Jul 1, 2015
Messages
20
Sorry, I missed out a line in my original code. See below

VBA Code:
Private Sub CommandButton1_Click()

    Dim objHTTP As Object
    Dim MyScript As Object
    Dim i As Long
    Dim myData As Object
    Dim postC, urlX As String
    Set MyScript = CreateObject("MSScriptControl.ScriptControl")
    MyScript.Language = "JScript"
    ComboBox4.Enabled = True
    postC = TextBox10.Value
    urlX = "https://api.ideal-postcodes.co.uk/v1/postcodes/" & postC & "?api_key=iddqd"
    URL = urlX

    Set objHTTP = CreateObject("MSXML2.XMLHTTP")
    objHTTP.Open "GET", URL, False
    objHTTP.send
    
    Set RetVal = MyScript.Eval("(" + objHTTP.responseText + ")")
    objHTTP.abort
    
    i = 2
    

        Set MyList = RetVal.result

    For Each myData In MyList
        Cells(i, 1).Value = myData.line_1

        i = i + 1
    Next
    
    Set MyList = Nothing
    Set objHTTP = Nothing
    Set MyScript = Nothing
    
    
End Sub
 

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
6,712
Code:
For Each myData In MyList
ComboBox4.AddItem
Next

but that doesn't work probably because I need to somehow split myData.line_1 from the rest of the results?
Have you tried this?
VBA Code:
    For Each myData In MyList
        ComboBox4.AddItem myData.line_1
    Next
 

Watch MrExcel Video

Forum statistics

Threads
1,127,407
Messages
5,624,587
Members
416,036
Latest member
eloisa manzanarez

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top