Text Search Problem

crow

Registered User.
Local time
Today, 14:41
Joined
Jan 10, 2005
Messages
22
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
 
I don't think

DoCmd.FindRecord Me!txtSearch

is correct.

I think using a bookmark will do what you want, e.g.

dim db as dao.database
dim rs as dao.recordset
set rs=me.recordsetclone
rs.findfirst "[FieldBeingSearched] =" & chr(34) & Me!txtSearch & chr(34)
me.bookmark=rs.bookmark
 
Search In Textbox

Thanks for your reply but I now keep getting errors. Maybe I am placing your idea in the wrong area. Where did you mean to put this code? Sorry I am new to Access!
 
Replace DoCmd.FindRecord Me!txtSearch with the posted code.

Of course, "[FieldBeingSearched]" has be be relaced with the appropriate field name.
 
Text Box Search

Thanks again for the reply but when I put this code in place of the other, I get an error at this part of the code. Could it be something about the dao? I thought I read that somewhere that database properties can be changed but I am not sure where or if it can.

db As dao.database
 

Users who are viewing this thread

Back
Top Bottom