A combo box control is what is usually used for searching for a specific
record. Typically, the combo is an unbound control. It is left unbound
because changes in it will cause changes in the current record and may create
errors due to duplicate keys.
If you set the Auto Expand property of the combo to Yes, it will provide the
"type ahead" feature. You will usually use the After Update and Not In List
events of the combo. The After Update event you will use to locate a record
and make it the current record. The Limit To List event is used when the
value entered is not in the table. To use this event, set the Limit To List
property to Yes.
The combo will need a row source. It is usually a query based on the
primary key or other field you want to search by. If you are using a field
other than the primary key, it is a good idea to create an index for the
field. If you are wanting to look up customers, you probably have a customer
ID and a customer name. You will probably want to show the user the customer
name but do the search with the customer ID. This is accomplished by making
the combo a multi column combo. Here is an example of a row source for the
combo:
SELECT CustID, CustName FROM tblCustomer;
To show only the customer name, you set the column widths property so the ID
column has a width of 0. As to columns, you need to be aware that when
counting columns in a combo, identifying the bound column is 1 based, but
addressing column values is 0 based. That is, if you want the first column
to be the bound column, you set it to 1, but if you want to return the value
of the first column, it is
Me.cboCustomer.Column(0).
Now to the events. Here is the code for the After Update event you will use
to locate a customer and make it the current record:
Set rst = Me.RecordsetClone
rst.FindFirst "[CustID] = " & Me.cboCustomer
Me.Bookmark = rst.Bookmark
set rst = Nothing
Now we need to address the Not In List event so that a user can add a new
record or cancel if they entered an incorrect value:
If MsgBox(NewData & " Is Not In The Customer Table " & vbNewLine _
& "Do you want to add it", _
vbInformation + vbYesNo, "Not Found") = vbYes Then
CurrentDb.Execute ("INSERT INTO tblCustomer (CustID) " _
& "VALUES ('" & NewData & "');"), dbFailOnError
Me.Requery
Set rst = Me.RecordsetClone
rst.FindFirst "[CustID] = '" & NewData & "'"
Me.Bookmark = rst.Bookmark
Set rst = Nothing
Response = acDataErrAdded
Else
Me.cboCustomer.Undo
Response = acDataErrContinue
End If
Robert T said:
Hi:
On a typical form where there are hundreds of customers stored in
tblCustomers, I would like to know how to find a specific customer and go to
his or her record.
I use another database program called Alpha Five where you click a button on
the form and an alphabetical drop down list of customers pops up in
alphabetical order. You select a customer and it uses the value in CustID to
find and move to that customer’s record. Can we do the same thing in Access
2003?
If we can’t use a drop down list, how can I get a box to pop up so the user
can enter a customer’s last name or CustID number and then find and move to
that customer’s record?
Thanks,
Robert