Hello and TIA for guidance.
I am building a reusable search procedure (thanks go to Graham Thorpe
for his example that set me on my way). Everything works up until the
2nd match is found, the command doesn't go to the third match, it
exits out of the loop. I've read everything I could find in NG's, but
I can't figure out where I'm going wrong. Here is my code.
Function cmdSearch(ctlSearchText As Control, ctlFoundText As Control,
qrySearch As String)
'author Graham Thorpe 25-01-02
'modification Liz Malcolm 6/1/2004
'call from "On Click" event of a search button
On Error GoTo Err_cmdSearch
Dim dbSearch As DAO.Database
Set dbSearch = CurrentDb()
Dim rstSearch As DAO.Recordset
Set rstSearch = dbSearch.OpenRecordset(qrySearch)
Dim strTextRef As String
'strTextRef is the passed match to ctlFoundText
'ctlFoundText is the passed reference to the control on the form
'with the value you want to match
Dim strSearch As String
'strSearch is the passed value from the unbound search box
'ctlSearchText is the passed reference to the unbound search box
'txtSearch is the name of the unbound search box
'check search box for Null Value or Entry
If IsNull(ctlSearchText) Or (ctlSearchText) = "" Then
Call DataMissing_msg
ctlSearchText.SetFocus
Exit Function
End If
'Perform the search using value entered into txtSearch
rstSearch.MoveFirst
ctlFoundText.SetFocus
DoCmd.FindRecord ctlSearchText
ctlFoundText.SetFocus
strTextRef = ctlFoundText.Text
ctlSearchText.SetFocus
strSearch = ctlSearchText.Text
'If matching record is found, focus is set in matched control,
'and search control is cleared
If strTextRef = strSearch Then
Do While strTextRef = strSearch And Not rstSearch.EOF
DoEvents
If MsgBox("Match Found - Search Again?", vbYesNo, "Search
Results") = vbYes Then
ctlFoundText.SetFocus
rstSearch.MoveNext
If Not rstSearch.NoMatch = True Then
MsgBox ("There are no more matches")
Exit Do
Else
DoCmd.GoToRecord , , acNext 'display next record
End If
Else
Exit Do
End If
Loop
ctlFoundText.SetFocus
ctlSearchText = ""
'if value not found focus is set to a new record and search control is
cleared
Else
Call search_msg
Call NewRec
ctlSearchText.SetFocus
ctlSearchText = ""
End If
rstSearch.Close
Set rstSearch = Nothing
dbSearch.Close
Set dbSearch = Nothing
Exit_cmdsearch:
Exit Function
Err_cmdSearch:
MsgBox Err.Description
Resume Exit_cmdsearch
End Function
Again TIA for any help.
Liz M.