I have this code for search from a textbox. It works fine but if you search and no user is found it will always display the first record. Does anyone see where I could change something to always display a new record when no matches are found? Help is much appreciated!
Private Sub cmdSearch_Click()
Dim strusersRef As String
Dim strSearch As String
'Check txtSearch for Null value or Nill Entry first.
If IsNull(Me![txtSearch]) Or (Me![txtSearch]) = "" Then
MsgBox "Please enter a Username!", vbOKOnly, "Invalid Search Criterion!"
Me![txtSearch].SetFocus
Exit Sub
End If
'---------------------------------------------------------------
'Performs the search using value entered into txtSearch
'and evaluates this against values in strStudentID
DoCmd.ShowAllRecords
DoCmd.GoToControl ("username")
DoCmd.FindRecord Me!txtSearch
username.SetFocus
strusersRef = username.Text
txtSearch.SetFocus
strSearch = txtSearch.Text
'If matching record found sets focus in strStudentID and shows msgbox
'and clears search control
If strusersRef = strSearch Then
MsgBox "User Found For: " & strSearch, , "Congratulations!"
username.SetFocus
txtSearch = ""
'If value not found sets focus back to txtSearch and shows msgbox
Else
MsgBox "User Not Found For: " & strSearch & " - Please Try Again.", _
, "Invalid Search Criterion!"
txtSearch.SetFocus
End If
End Sub
Private Sub cmdSearch_Click()
Dim strusersRef As String
Dim strSearch As String
'Check txtSearch for Null value or Nill Entry first.
If IsNull(Me![txtSearch]) Or (Me![txtSearch]) = "" Then
MsgBox "Please enter a Username!", vbOKOnly, "Invalid Search Criterion!"
Me![txtSearch].SetFocus
Exit Sub
End If
'---------------------------------------------------------------
'Performs the search using value entered into txtSearch
'and evaluates this against values in strStudentID
DoCmd.ShowAllRecords
DoCmd.GoToControl ("username")
DoCmd.FindRecord Me!txtSearch
username.SetFocus
strusersRef = username.Text
txtSearch.SetFocus
strSearch = txtSearch.Text
'If matching record found sets focus in strStudentID and shows msgbox
'and clears search control
If strusersRef = strSearch Then
MsgBox "User Found For: " & strSearch, , "Congratulations!"
username.SetFocus
txtSearch = ""
'If value not found sets focus back to txtSearch and shows msgbox
Else
MsgBox "User Not Found For: " & strSearch & " - Please Try Again.", _
, "Invalid Search Criterion!"
txtSearch.SetFocus
End If
End Sub