Using VBA to Control Combo Boxes

mikewood1980

Registered User.
Local time
Today, 09:33
Joined
May 14, 2008
Messages
45
Hi
I would like to use a combo box in a form, but would like to control the contents using VBA code. Im not sure how to get started - can anyone give me an example of how to populate a combo box from another table in the database? (I know that this can be done using queries - but I need more control that this over the list in the combo box)

Thanks for in advance for your help
Mike Wood
 
Code:
    Dim rstTable As DAO.Recordset
    Set rstTable = CurrentDb.OpenRecordset("SELECT Name FROM tblTable", dbReadOnly)
 
    If Not rstTable.EOF Then
        rstTable.MoveFirst
 
        Do While Not rstTable.EOF
 
            cbxYourComboBox.AddItem rstTable.Fields("Name")
 
            rstTable.MoveNext
        Loop
    End If
 
    rstTable.Close
    Set rstTable = Nothing

Ps. Set the "Row source type" of the combobox to "Value List" for this code to work.
 

Users who are viewing this thread

Back
Top Bottom