Search Button

G

Guest

I've created a search button on a form to locate a record. The ides is to
the record# and press the search button and the record in a form is returned.
The code that I have is listed below. I receive a run time error 424
Object Required when I use it. Would someone please give me a pointer on
why t is not working, please.


Private Sub cmdSearch_Click()
Dim strInvoiceNumber 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 strStudentID

DoCmd.ShowAllRecords
DoCmd.GoToControl ("InvoiceNum")
DoCmd.FindRecord Me!txtSearch

InvoiceNum.SetFocus
strnvoiceNumber= InvoiceNum.Text
txtSearch.SetFocus
strSearch = txtSearch.Text

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

If strInvoiceNumber = strSearch Then
MsgBox "Match Found For: " & strSearch, , "Congratulations!"
InvoiceNum.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
 
G

Guest

The first two lines of every page of code should be

Option Compare Database
Option Explicit

"Option Explicit" requires you to Declare (DIM) variables; if you misspell a
variable, it alerts you. You can set it so that new code pages have it
automatically: in the IDE goto TOOLS/OPTIONS - Editor and checking "Require
Variable Declaration".


In this line:
strnvoiceNumber= InvoiceNum.Text

the "I" is missing after "str"


And delete both instances of ".Text"


Why did you DIM "strInvoiceNumber" and "strSearch" as strings if the data
you are searching for are numbers (Invoice Number)??? I would think they
would be Long's.



HTH
--
Steve S
--------------------------------
"Veni, Vidi, Velcro"
(I came; I saw; I stuck around.)


MyMel said:
I've created a search button on a form to locate a record. The ides is to
the record# and press the search button and the record in a form is returned.
The code that I have is listed below. I receive a run time error 424
Object Required when I use it. Would someone please give me a pointer on
why t is not working, please.


Private Sub cmdSearch_Click()
Dim strInvoiceNumber 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 strStudentID

DoCmd.ShowAllRecords
DoCmd.GoToControl ("InvoiceNum")
DoCmd.FindRecord Me!txtSearch

InvoiceNum.SetFocus
strnvoiceNumber= InvoiceNum.Text
txtSearch.SetFocus
strSearch = txtSearch.Text

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

If strInvoiceNumber = strSearch Then
MsgBox "Match Found For: " & strSearch, , "Congratulations!"
InvoiceNum.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
 
G

Guest

Hi Steve,

Thank you for your help it's working now.

Private Sub cmdSearch_Click()

Dim txtSearch As String
Dim InvoiceNumber As String

If IsNull(Me![txtSearch]) Or (Me![txtSearch]) = "" Then
MsgBox "Please enter a value!", vbOKOnly, "Invalid Search Criterion!
"""
Me![txtSearch].SetFocus
Exit Sub
End If

DoCmd.ShowAllRecords
DoCmd.GoToControl ("InvoiceNum")
DoCmd.FindRecord Me!txtSearch

If Me!txtSearch = Me!InvoiceNumber Then
MsgBox "Invoice# Found: " & Me!txtSearch
Else
MsgBox "Match Not Found For: " & Me!txtSearch & " - Please Try
Again.", _
, "Invalid Invoice#!"
Me.txtSearch.SetFocus
End If

End Sub

SteveS said:
The first two lines of every page of code should be

Option Compare Database
Option Explicit

"Option Explicit" requires you to Declare (DIM) variables; if you misspell a
variable, it alerts you. You can set it so that new code pages have it
automatically: in the IDE goto TOOLS/OPTIONS - Editor and checking "Require
Variable Declaration".


In this line:
strnvoiceNumber= InvoiceNum.Text

the "I" is missing after "str"


And delete both instances of ".Text"


Why did you DIM "strInvoiceNumber" and "strSearch" as strings if the data
you are searching for are numbers (Invoice Number)??? I would think they
would be Long's.



HTH
--
Steve S
--------------------------------
"Veni, Vidi, Velcro"
(I came; I saw; I stuck around.)


MyMel said:
I've created a search button on a form to locate a record. The ides is to
the record# and press the search button and the record in a form is returned.
The code that I have is listed below. I receive a run time error 424
Object Required when I use it. Would someone please give me a pointer on
why t is not working, please.


Private Sub cmdSearch_Click()
Dim strInvoiceNumber 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 strStudentID

DoCmd.ShowAllRecords
DoCmd.GoToControl ("InvoiceNum")
DoCmd.FindRecord Me!txtSearch

InvoiceNum.SetFocus
strnvoiceNumber= InvoiceNum.Text
txtSearch.SetFocus
strSearch = txtSearch.Text

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

If strInvoiceNumber = strSearch Then
MsgBox "Match Found For: " & strSearch, , "Congratulations!"
InvoiceNum.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
 

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