Error in text search facility code

  • Thread starter Keith Yong via AccessMonster.com
  • Start date
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.
 
S

SteveS

Keith said:
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.


Made a couple of modifications. Try this: (watch for line wrap)

'-----Begin Code --------
Private Sub cmdSearch_Click()

Dim strNRIC As String
Dim strSearch As String
Dim NRICRef 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 = Me.NRIC

' the next two lines are unnecessary
' you can refer to txtsearch directly
'txtSearch.SetFocus
'strSearch = Me.txtSearch

'If matching record found sets focus in strStudentID and shows msgbox
'and clears search control

If NRICRef = Me.txtSearch Then
MsgBox "Match Found For: " & Me.txtSearch, , "Congratulations!"
NRIC.SetFocus
Me.txtSearch = ""

'If value not found sets focus back to txtSearch and shows msgbox
Else
MsgBox "Match Not Found For: " & Me.txtSearch & " - Please
TryAgain.", _
, "Invalid Search Criterion!"
Me.txtSearch.SetFocus
End If
End Sub

'-----End Code --------


HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top