Very roughly, there are two broad ways to populate controls, generally textboxes:
- You can fill data into fields from a listbox, combobox or with other code. This way of populating controls is very useful for unbound forms but means that any changes to the data have to be written back to the table.
- You can filter a recordset, create a recordset or move to a record in a recordset based on a selection in a list box or combobox. This means that 'live' data is returned and when it is changed, the change directly affects the table.
With the first method, a combo box or listbox would have several columns, say, ID, FirstName, LastName, and controls would be populated by referring to the column:
Combo:
cboOne.Column(0) 'ID
cboOne.Column(2) 'Lastname
Simple ListBox:
lstOne.Column(1) 'Firstname
The controls can either have a control source that refers to the column property or they can be populated programmatically in a suitable event.
With the second method, selecting an item in the listbox or combo boxes fires code that either filters the recordset or moves to the relevant record.
Using the combo box wizard, you will get this code:
Code:
Private Sub cboOne_AfterUpdate()
' Find the record that matches the control.
Dim rs As Object
Set rs = Me.Recordset.Clone
rs.FindFirst "[ID] = " & Str(Me![cboOne])
Me.Bookmark = rs.Bookmark
End Sub
This can be modified to suit a listbox.