How do i populate a combobox with values from a certain field on my database when i click the control?


My combobox control name :cbodept

Dbase table: Offices

and Dbase Table field is: Office

Preferably code. Thanks

Recommended Answers

All 8 Replies

Abe, you should know by now that you can not just ask for code, show your efforts first.:)

Option Explicit

Dim oConn As ADODB.Connection
Dim oRs As ADODB.Recordset

Private Sub Command2_Click()

    'Using MS Access database...
    Dim sConn As String
    Dim oConn As New ADODB.Connection 'Declare a new connection...
    Dim oRs As New ADODB.Recordset 'Declare a new recordset...
        
    sConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\YourDatabaseNameHere.MDB;Persist Security Info=False"
        
    oConn.Open sConn
    
    oRs.Open "SELECT * FROM YourTableNameHere", oConn, adOpenStatic, adLockOptimistic
    
    Dim xAdd As Integer

For xAdd = 0 To oRs.Recordcount - 1
cbodept.AddItem oRs.Office
oRs.MoveNext
Next xAdd
    
    oRs.Close
    oConn.Close
End If
End Sub

I cant really have a code for that sir, its my first time to deal with this kind of prob.

I am use in entering values on comboboxes manually, writing values on the property list.

I'll try your code and get back if i have more further question.

No problem, let me know how it went.:)

On Line 22, I think it should be like this.

cbodept.AddItem oRs!Office

Thanks Jhai, my bad. This is what happens when you rush through solutions!;)

Haha :D but its a good solution so thats just fine.

It Works sir.

Just have to add some Declarations and modifications. :)

Thanks.

It was a pleasure, also to Jhai for picking up on my stupid error!. Happy coding.:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.