Part IV: Professional Database Development
884
the search criteria, Access moves the datasheet record pointer to the first row that contains Smith
in the LastName field.
When you use code to search through the contents of a bound Access form, you actually perform
these same steps using VBA statements.
Follow these steps to create an AfterUpdate event procedure behind the combo box:
- Display frmProductsExample1 in Design view, click cboQuickSearch, and press
F4 to display the Property Sheet. - Select the Event tab and select the AfterUpdate event.
- Click the combo box arrow in the AfterUpdate event property and select Event
Procedure. - Click the Builder button that appears in the right side of the property.
The procedure appears in a separate VBA code window. The event procedure template
(Private Sub cboQuickSearch_AfterUpdate()...End Sub) is automatically
created in the form’s code module. As you’ve learned, whenever you create an event pro-
cedure, the name of the control and event are part of the subprocedure.
- Enter the four lines of code exactly as shown in Figure 25.4.
FIGURE 25.4
Using the FindRecord method to find a record
The first line is
txtProductID.SetFocus
This statement moves the cursor to the txtProductID control. Just as you need to manually
move the cursor to a column in a datasheet in order to use the Find icon in the Access ribbon, you
must place the cursor in the bound control you want to use as the search’s target. In this case,
you’re moving the cursor to the control containing the ProductID value because the search will
look for a particular ProductID in the form’s bound recordset.