B
BobV
Group:
I am trying to write a search subroutine that will search for a user
provided search string, and once the user has found all of the instances,
the subroutine will allow the user to re-start the same search from the
beginning of the recordset. However with my code (below), once all instances
of the search string have been found, the user is not allowed to search on
that string again, it seems that "rs.NoMatch" is always set to TRUE for
that search string.
How do I allow the user to restart the search for the same search string
from the beginning of the recordset? The code below will not restart the
search. It will always tell the user that there are no matches if the user
has found all instances of the search string. I have tried closing the
recordset with "rs.close", but that didn't solve the problem.
Thanks,
BobV
Note: This code is behind a Form called SearchForm, and it executes when the
user has pressed the OK button to initiate the search.
'OK BUTTON
Private Sub OKButton_Click()
Dim rs As DAO.Recordset
Dim Locate As String
On Error Resume Next
If SearchBy = 1 Then
If IsNull(Me.AssetIDSearch) Then
MsgBox "You must first enter an Asset ID Number.",
vbExclamation, "MISSING INFORMATION ERROR"
Me.AssetIDSearch.SetFocus
Exit Sub
Else
Locate = Me.AssetIDSearch
End If
ElseIf SearchBy = 2 Then
If IsNull(Me.DescriptionSearch) Then
MsgBox "You must first enter a Description.", vbExclamation,
"MISSING INFORMATION ERROR"
Me.DescriptionSearch.SetFocus
Exit Sub
Else
Locate = Me.DescriptionSearch
End If
Else
'No Selection Made
GoTo ExitHandler
End If
Set rs = Forms![Add / Edit Assets].RecordsetClone
If SearchBy = 1 Then
rs.FindNext "AssetID like '" & Locate & "*'"
ElseIf SearchBy = 2 Then
rs.FindNext "Description like '" & Locate & "*'"
End If
If rs.NoMatch Then
MsgBox "Could not find. Try again."
Else
Forms![Add / Edit Assets].Bookmark = rs.Bookmark
End If
Forms![Add / Edit Assets]!Group.SetFocus
rs.Close
Set rs = Nothing
ExitHandler:
DoCmd.Close acForm, "SearchForm", acSaveNo
DoCmd.SelectObject acForm, "Add / Edit Assets"
DoCmd.Maximize
End Sub
I am trying to write a search subroutine that will search for a user
provided search string, and once the user has found all of the instances,
the subroutine will allow the user to re-start the same search from the
beginning of the recordset. However with my code (below), once all instances
of the search string have been found, the user is not allowed to search on
that string again, it seems that "rs.NoMatch" is always set to TRUE for
that search string.
How do I allow the user to restart the search for the same search string
from the beginning of the recordset? The code below will not restart the
search. It will always tell the user that there are no matches if the user
has found all instances of the search string. I have tried closing the
recordset with "rs.close", but that didn't solve the problem.
Thanks,
BobV
Note: This code is behind a Form called SearchForm, and it executes when the
user has pressed the OK button to initiate the search.
'OK BUTTON
Private Sub OKButton_Click()
Dim rs As DAO.Recordset
Dim Locate As String
On Error Resume Next
If SearchBy = 1 Then
If IsNull(Me.AssetIDSearch) Then
MsgBox "You must first enter an Asset ID Number.",
vbExclamation, "MISSING INFORMATION ERROR"
Me.AssetIDSearch.SetFocus
Exit Sub
Else
Locate = Me.AssetIDSearch
End If
ElseIf SearchBy = 2 Then
If IsNull(Me.DescriptionSearch) Then
MsgBox "You must first enter a Description.", vbExclamation,
"MISSING INFORMATION ERROR"
Me.DescriptionSearch.SetFocus
Exit Sub
Else
Locate = Me.DescriptionSearch
End If
Else
'No Selection Made
GoTo ExitHandler
End If
Set rs = Forms![Add / Edit Assets].RecordsetClone
If SearchBy = 1 Then
rs.FindNext "AssetID like '" & Locate & "*'"
ElseIf SearchBy = 2 Then
rs.FindNext "Description like '" & Locate & "*'"
End If
If rs.NoMatch Then
MsgBox "Could not find. Try again."
Else
Forms![Add / Edit Assets].Bookmark = rs.Bookmark
End If
Forms![Add / Edit Assets]!Group.SetFocus
rs.Close
Set rs = Nothing
ExitHandler:
DoCmd.Close acForm, "SearchForm", acSaveNo
DoCmd.SelectObject acForm, "Add / Edit Assets"
DoCmd.Maximize
End Sub