Excel and SQL....

jag108

Active Member
Joined
May 14, 2002
Messages
433
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. MacOS
I was wondering if it is possible to populate a drop down box, from data retrieved by SQL?
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Yes it is. Create a recordset using the method of your choice (I'd use ADO or DAO), loop through the recordset and use the AddItem method of the ComboBox e.g. here's an example that uses ADO to connect to an Access database. You will need to reference the Microsoft ActiveX Data Objects library by clicking Tools, References and looking for it in the list.

Code:
Private Sub UserForm_Initialize()
    Dim adoRS As ADODB.Recordset, adoCN As ADODB.Connection
    Dim strSQL As String


    'Open connection to an Access database
    Set adoCN = New ADODB.Connection
    adoCN.Provider = "Microsoft.Jet.OLEDB.4.0"
    adoCN.ConnectionString = "H:\temp\db1.mdb"
    adoCN.Open


    'Open a recordset
    strSQL = "SELECT DISTINCT MPCUnit FROM FTEData;"
    Set adoRS = New ADODB.Recordset

    adoRS.Open strSQL, adoCN, adOpenForwardOnly, adLockReadOnly




    'Now populate the combobox
    With Me.ComboBox1

        .Clear

        While Not adoRS.EOF

            .AddItem adoRS.Fields(0).Value
            adoRS.MoveNext

        Wend

    End With


    'Close recordset, connection objects and clean up
    adoRS.Close
    adoCN.Close
    Set adoRS = Nothing
    Set adoCN = Nothing


End Sub
 
Upvote 0
Cool thanks...

Next silly question?

I do not connect to an MS Access DB, I connect to an Oracle DB, which has its own domain, what would be the connection string for something like that.

I have tried the Excel ODBC connetion string which is generated by the macro code generator, but this fails with an error. I have added the Active X reference. Error "Could not find installable ISAM."

Connection:="ODBC;DSN=xxx.xxx.xxx.xxx;UID=myID/psswd;DBQ=xxx.xxx.xxx.xxx;ASY=OFF

Sory to be perdantic.

Once again thanks for your help.
 
Upvote 0

Forum statistics

Threads
1,224,862
Messages
6,181,466
Members
453,045
Latest member
Abraxas_X

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