Fill a ComboBox from a Database

SkylabOne

New Member
Joined
Oct 22, 2013
Messages
21
Dear all,

I am linking my Access Database to a UserForm in Word and have written below code. When I test the macro there are no errors, but the ComboBox is empty. Can anyone tell me what I'm doing wrong here?

Code:
Private Sub UserForm1_Initialize()    
    On Error GoTo UserForm1_Initialize_Err
    Dim cnn As New ADODB.Connection
    Dim rst As New ADODB.Recordset
    cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
             "Data Source=C:\ClientDatabase\ClientDatabase.mdb"
    rst.Open "SELECT DISTINCT [ClientName] FROM tblClients ORDER BY [ClientName];"


             cnn , adOpenStatic
    rst.MoveFirst
    With Me.ComboBox1
        .Clear
        Do
             .AddItem rst![ClientName]
             rst.MoveNext
        Loop Until rst.EOF
    End With
UserForm1_Initialize_Exit:
    On Error Resume Next
    rst.Close
    cnn.Close
    Set rst = Nothing
    Set cnn = Nothing
UserForm1_Initialize_Err:
    MsgBox Err.Number & vbCrLf & Err.Description, vbCritical, "Error!"
    Resume UserForm1_Initialize_Exit
End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
It doesn't sound like your query is returning any records. Can you step through your code in debug mode and find out?
 
Upvote 0
Code:
             [B]cnn[/B] , adOpenStatic

If I compile the project cnn is selected and the error Compile error: Invalid use of property is shown. Does that help?
 
Upvote 0
Is it possible that I'm using an old code which doesn't work for Word/Access 2013?

I have added a ListBox now, with the exact same code and it doesn't populate with the list from the database either. So I presume there is something wrong with the way I've connected.

Hopefully someone sees my mistake.

Thank you,
Rolf
 
Upvote 0
The code line should look something like this:
Code:
rst.Open "SELECT DISTINCT [ClientName] FROM tblClients ORDER BY [ClientName]", cnn, adOpenStatic, adLockReadOnly
 
Upvote 0
The code line should look something like this:
Code:
rst.Open "SELECT DISTINCT [ClientName] FROM tblClients ORDER BY [ClientName]", cnn, adOpenStatic, adLockReadOnly

Thank you for the suggestion, but it still doesn't work, even though the error is fixed.
The link to the source definitely works, as it opens the file when I paste it into Explorer.
I will transfer the data into Excel and try the same thing there. Perhaps it is something with Access or the particular database I've made.

Thanks for the help!
 
Upvote 0

Forum statistics

Threads
1,215,575
Messages
6,125,631
Members
449,241
Latest member
NoniJ

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