Get Access-data in UserForm

Stevenn

Active Member
Joined
Feb 8, 2012
Messages
259
I want to fill a dropdown list in my UserForm with data from Access.

Right now I'm getting the data with the code below

Code:
Private Sub UserForm_Initialize()
 
    Dim objConnection   As New ADODB.Connection
    Dim objRecordset    As New ADODB.Recordset
 
    objConnection.Open "Provider=Microsoft.Ace.OLEDB.12.0; Data Source=" & DataSource & ";"
 
    objRecordset.Open "SELECT DISTINCT Name FROM Persons", objConnection, adOpenStatic
 
    objRecordset.MoveFirst
 
    With Me.cboPersons
 
        .Clear
 
        Do
 
            .AddItem objRecordset.Fields("Name").Value
 
            objRecordset.MoveNext
 
        Loop Until objRecordset.EOF
 
    End With
End Sub

But I want to show Persons.Name, but save Persons.Id instead of Persons.Name. Is it possible with VBA in Excel 2007 and Access 2007? Can I by the way do the Access data selection smarter than the code I am using right now?
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Something like
Code:
objRecordset.Open "SELECT DISTINCT ID, Name FROM Persons", objConnection, adOpenForwardOnly
    With Me.cboPersons
        .ColumnCount = 2
        .BoundColumn = 1
        .Columnwidths = "0;75"
        .Clear
        .Column = objRecordset.GetRows()
    End With
objRecordset.Close
 
Upvote 0

Forum statistics

Threads
1,223,157
Messages
6,170,420
Members
452,325
Latest member
BlahQz

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