View Full Version : Using VBA to Control Combo Boxes


mikewood1980
05-27-2008, 03:22 AM
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

ErikSnoek
05-27-2008, 03:47 AM
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.