Gigi
Unbound combo or list boxes are great for this type of thing.
Add a combo box to the form, and use the wizard...
- Find a record on my form based on the value I select...
- Choose your field, in your case
lastname
Side bar - Although you selected
lastname, Access will select
patientid and
lastname so that it can find a specific record based on the primary key.
- On the next screen, "Hide key column" will be checked - this is good.
- The last page of the wozard will prompt you for the caption on the label.
After this is over, look at the combo box in design mode with the properties window open. (from the menu, "View" -> "Properties")
Select the combo box. On the "Data" tab of the properties window you will see...
ControlSource: empty
RowSource: a select statement. Click on this field, and then click on the command button "..." that will appear to the right. This will open the Query Builder. Notes that Access has added two fields
PatientID and
lastname.
Add the field firstname. Close and save the query builder.
BoundColumn: will be 1, patientID
Now click on the "Format" tab, you will see...
ColumnCount: 2
Change to 3 to accommodate firstname.
ColumnWidths: 0";1"
Change to 0":1";1" to accommodate firstname
Sidebar: A column with 0" will be "invisible"
ListWidth Either 1" or Auto.
Change to 2" to accommodate firstname
Now click on the "Event" tab, you will see "[Event Procedure]" for the AfterUpdate event. Click on the AfteyrUpdate field, and then click on the "..." command button to open the Visual Basic coding window. You will something along the lines of...
Code:
Private Sub Combo7_AfterUpdate()
' Find the record that matches the control.
Dim rs As Object
Set rs = Me.Recordset.Clone
rs.FindFirst "[PatientID] = " & Str(Me![Combo7])
Me.Bookmark = rs.Bookmark
End Sub
Pretty cool stuff. My only beef about the wizards that they do not allow you to determine the name of the sub routine. In the depicted example, the control is named Combo7. A more meaningful name would be
cmbFindPatient - BUT dont change this.
Close the VBA coding window, and click on the "Other" tab. The Name field will depict the same name as seen in the coding window.
Save and test the combo box. The name selected in the combo box should be retrieved AfterUpdate event.
Richard