K
Keith Yong via AccessMonster.com
Hi I have managed to find a code on the web for searching records in a
form. I have edited the control names to my needs.
The user types the required info ((ie: NRIC = S1234567A) into the unbound
text box (Name for the unbound text box is txtsearch) and clicks on a
search command button. Then the search code will go to the record with the
corresponding NRIC.
There seems to be something wrong with the code still, as I am not able to
go to the record I want although I have entered a valid NRIC. No matter
what I enter into the search box, it'll return as invalid (ie: S1234567A
not found)
Can anybody help me identify what is wrong with my code? I'm not too good
at coding here.
The code is as follows:
Private Sub cmdSearch_Click()
Dim strNRIC 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 value!", 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 NRIC
DoCmd.ShowAllRecords
DoCmd.GoToControl ("NRIC")
DoCmd.FindRecord Me!txtSearch
NRIC.SetFocus
NRICRef = NRIC.Text
txtSearch.SetFocus
strSearch = txtSearch.Text
'If matching record found sets focus in strStudentID and shows msgbox
'and clears search control
If NRICRef = txtSearch Then
MsgBox "Match Found For: " & strSearch, , "Congratulations!"
NRIC.SetFocus
txtSearch = ""
'If value not found sets focus back to txtSearch and shows msgbox
Else
MsgBox "Match Not Found For: " & strSearch & " - Please Try
Again.", _
, "Invalid Search Criterion!"
txtSearch.SetFocus
End If
End Sub
Thanks a million.
form. I have edited the control names to my needs.
The user types the required info ((ie: NRIC = S1234567A) into the unbound
text box (Name for the unbound text box is txtsearch) and clicks on a
search command button. Then the search code will go to the record with the
corresponding NRIC.
There seems to be something wrong with the code still, as I am not able to
go to the record I want although I have entered a valid NRIC. No matter
what I enter into the search box, it'll return as invalid (ie: S1234567A
not found)
Can anybody help me identify what is wrong with my code? I'm not too good
at coding here.
The code is as follows:
Private Sub cmdSearch_Click()
Dim strNRIC 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 value!", 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 NRIC
DoCmd.ShowAllRecords
DoCmd.GoToControl ("NRIC")
DoCmd.FindRecord Me!txtSearch
NRIC.SetFocus
NRICRef = NRIC.Text
txtSearch.SetFocus
strSearch = txtSearch.Text
'If matching record found sets focus in strStudentID and shows msgbox
'and clears search control
If NRICRef = txtSearch Then
MsgBox "Match Found For: " & strSearch, , "Congratulations!"
NRIC.SetFocus
txtSearch = ""
'If value not found sets focus back to txtSearch and shows msgbox
Else
MsgBox "Match Not Found For: " & strSearch & " - Please Try
Again.", _
, "Invalid Search Criterion!"
txtSearch.SetFocus
End If
End Sub
Thanks a million.