Calling the Record Finder Code

The On Click event of the First button (shown in Figure M-4) includes this code:

Private Sub cmdFirst_Click() On Error GoTo Error_Handler

FindRecordLike "first"

Exit_Procedure:

On Error Resume Next Exit Sub

Error_Handler:

DisplayUnexpectedError Err.Number, Err.Description

Resume Exit_Procedure

Resume

End Sub

The code really has only one operative line:

FindRecordLike "first"

The code behind cmdNext_Click() is almost identical. Instead of using "first" as the parameter for FindRecordLike, it sends in "next":

FindRecordLike "next"

Now here's the code for the subroutine FindRecordLike. It also resides in the index form that contains the record finder controls (shown in Figure M-4). It looks like this:

Private Sub FindRecordLike(strFindMode As String)

On Error GoTo Error_Handler

Call ww_FindRecord(frmCallingForm:=Me, _ ctlFindFirst:=Me!cmdFirst, _ ctlFindNext:=Me!cmdNext, _ ctlSearchText:=Me!txtFind, _ ctlSearchOption:=Me!optFind, _ strFindMode:=strFindMode, _ strField1:="BusinessName", _ strField2:="LastName", _ strField3:="FirstName", _ strField4:="City")

Exit_Procedure:

On Error Resume Next Exit Sub

Error_Handler:

DisplayUnexpectedError Err.Number, Err.Description

Resume Exit_Procedure

Resume

End Sub

This Sub accepts a parameter strFindMode of "first" or "next", which it passes directly to the ww_FindRecord procedure. In fact, pretty much all this procedure does is call ww_FindRecord. The interesting part is the set of parameters that is passed to ww_FindRecord, many of which are explained in the next section. First, take a look at the parameters strFieldl, strField2, strField3, and strField4.

For the record finder routine to know which fields to search for your user's phrase, you need to make it known. You can do this by sending in up to ten field names. These must be names of fields that appear in the Recordsource for the form. Although they don't technically have to appear on the form itself, it will seem strange to the user to find records containing a phrase that he can't see.

Why not automate the list of fields? It would be possible to use VBA to cycle through all of the fields displayed on the form, using the form's Controls collection, and send their names to the ww_FindRecord procedure automatically. However, that wouldn't necessarily be desirable because you may not want all the fields to be searchable. By sending them yourself using this simple code, you can carefully control which fields are searched.

This example sends only four field names to be searched, but it could have specified up to ten. Fields 2 through 10 are optional parameters, and are explained later in this chapter.

0 0

Responses

  • giusy
    Why can't I use Recordsetclone in Access 2007?
    7 years ago

Post a comment