The information in this article applies to:
- Microsoft Access version 7.0
SYMPTOMS
When the FindNext macro action is run from a command button on a form to
find the next instance of the search criteria specified by the previous
FindRecord action, the first instance is found repeatedly. The FindNext
macro action fails to advance to the next record to find the next instance
of the search criteria.
NOTE: The same behavior can be demonstrated using the FindRecord macro
action with the FindFirst argument set to No.
CAUSE
Microsoft Access for Windows 95 and Microsoft Access version 2.0 behave
differently in regard to where the FindNext macro action begins the
search for the next instance of the search criteria.
The FindNext action in Microsoft Access version 2.0 always begins
searching starting with the next record in the recordset. In Microsoft
Access for Windows 95, if the currently selected text is the same as the
search text at the time the FindNext macro action is invoked, the search
begins immediately following the selection in the same field as the
selection, and in the same record. Otherwise, it begins searching at the
start of the current record.
The advantage to the Microsoft Access for Windows 95 behavior is that
you can repeatedly find multiple instances of the same search criteria
that might appear in a single record. The FindNext macro action in
Microsoft Access version 2.0 is limited to finding only the first instance
of the search criteria in a record, regardless of other instances of the
same search criteria in the record.
The problem occurs in Microsoft Access for Windows 95 when you push a
custom Find Next command button on a form. When this happens, focus
moves from the current control to the button. If text is selected in the
current control, it will not remain selected when the focus moves to the
button. The FindNext action will then begin searching from the start of the
record because the current selection is no recognized. The first
instance of the search criteria will be found repeatedly in the same
record.
RESOLUTION
Use one of the following techniques to work around this behavior:
- Use a different mechanism to invoke the FindNext action that allows
focus to remain in the current control. This can be accomplished in
one of the following ways:
- Use a custom Toolbar button rather than a command button.
- Use a function key or keystroke invoked by a AutoKeys macro.
- Use Visual Basic code to reset focus back to the control that
contains the selection before invoking the FindNext macro action.
For a detailed discussion of how to implement these techniques, please see
the "Workaround" sections following the "More Information" section.
MORE INFORMATION
Steps to Reproduce Behavior
- Open the sample database Northwind.mdb.
- Create a new macro called "Find" with the following actions:
Macro Name Macro Action
--------------------------
Company GoToControl
FindRecord
Company Actions
--------------------------------------------
GoToControl:
ControlName: CompanyName
FindRecord:
Find What: =[Forms]![Customers]![txtFind]
Match: Any Part of Field
Match Case: No
Search: All
Search As Formatted: No
Only Current Field: Yes
Find First: Yes
Macro Name Macro Action
---------------------------
NextCompany GoToControl
FindNext
NextCompany Actions
---------------------------
GoToControl:
ControlName: CompanyName
NOTE: You can optionally use the FindRecord macro action in place of
FindNext. Use the same arguments as the FindRecord macro action with
the exception of the FindFirst argument, which should be set to No.
NOTE: Do not omit the equals sign (=) to the left of
[Forms]![Customers]![txtFind] in the FindWhat argument above.
- Open the Customers form in Design view.
- Add a new text box to the form with the following properties:
Name: txtFind
ControlSource: <leave empty>
DefaultValue: "a"
- Add a command button to the form with the following properties:
Name: FindCompany
Caption: Find Company
OnClick: Find.Company
- Create a second command button with the following properties:
Name: FindNextCompany
Caption: Find Next Company
OnClick: Find.NextCompany
- View the Customers form in Form view.
- Click the Find Company command button to find the first customer
record that contains the letter "a". Note that the "A" in "Alfreds
Futterkiske" is selected, as expected.
- Click the Find Next Company command button to find the next instance
of the letter "a" in the Company Name field. Note that the "A" in
"Alfreds Futterkiske" is selected again. This is not expected.
Workaround 1: Using a Custom Toolbar Button
- While the Customers form with the modifications made in the "Steps To
Reproduce Behavior" section is open in Form view, using the right mouse
button (right click), click the toolbar and choose Customize.
- Choose "All Macros" from the Categories list.
- Drag "Find.Company" from the Objects list to the toolbar.
- Right click the new toolbar button and choose "Choose Button Image."
- Click to select the "Text" check box and type "Find Company" in the
text box to the right. Click OK.
- Drag "Find.NextCompany" from the Objects list to the toolbar.
- Right Click the new toolbar button and choose "Choose Button Image."
- Click to select the "Text" check box and type "Find Next Company" in
the text box to the right. Click OK.
- Click the Close button in the Customize Toolbars dialog box to return
to the Customers form.
- Click the Find Company toolbar button. Note that the "A" in "Alfreds
Futterkiske" is selected, as expected.
- Click the Find Next Company toolbar button. Note that the first "A" in
"Ana Trujillo Emparedados y helados" is selected, as expected.
- Click the Find Next Company toolbar button again. Note that the second
"a" in "Ana Trujillo Emparedados y helados" is selected, as expected.
Workaround 2: Using the F3 Key
- Create the following new macro and name it AutoKeys:
Macro Name Macro Action
---------------------------
{F3} FindNext
- Open the modified Customers form from the "Steps To Reproduce Behavior
Section" in Form view.
- Click the Find Company command button to find the first customer
record that contains the letter "a". Note that the "A" in "Alfreds
Futterkiske" is selected, as expected.
- Press the F3 key to repeat the search. Note that the "A" in "Ana
Trujillo Emparedados y helados" is selected, as expected.
Workaround 3: Using Visual Basic with a Command Button
- Open the modified Customers form from the "Steps To Reproduce Behavior"
section in Design view.
- Add a new command button to the form with the following properties:
Name: NewFindCompany
Caption: New Find Company
For the OnClick event procedure, click the Build button and choose
Code Builder from the Choose Builder dialog box. Edit the procedure as
follows:
Private Sub NewFindCompany_Click()
Me![CompanyName].SetFocus
DoCmd.FindRecord Me![txtFind], acAnywhere
RecordFind
End Sub
- Create a second command button with the following properties:
Name: NewFindNextCompany
Caption: New Find Next Company
For the OnClick event procedure, click the Build button and choose
Code Builder from the Choose Builder dialog box. Edit the procedure as
follows:
Private Sub NewFindNextCompany_Click()
RestoreFind
DoCmd.FindNext
RecordFind
End Sub
- In the Object box at the top of the code window, choose "(General)"
- Add the following lines of code to the Declarations section of the
form module:
Dim FoundControl As Control
Dim FoundControlSelStart As Integer
Dim FoundControlSelLength As Integer
- Add the following procedures to the General section of the module:
Sub RestoreFind()
FoundControl.SetFocus
FoundControl.SelStart = FoundControlSelStart
FoundControl.SelLength = FoundControlSelLength
End Sub
Sub RecordFind()
Set FoundControl = Me.ActiveControl
FoundControlSelStart = Me.ActiveControl.SelStart
FoundControlSelLength = Me.ActiveControl.SelLength
End Sub
- View the form in Form view.
- Click the Find Company command button to find the first customer
record that contains the letter "a". Note that the "A" in "Alfreds
Futterkiske" is selected, as expected.
- Click the Find Next Company command button to find the next instance
of the letter "a" in the Company Name field. Note that the "A" in "Ana
Trujillo Emparedados y helados" is selected, as expected.