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
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,589
Messages
6,120,415
Members
448,960
Latest member
AKSMITH

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
Back
Top