record not found

  • Thread starter Jonathan Snyder via AccessMonster.com
  • Start date
J

Jonathan Snyder via AccessMonster.com

I have a hot button on my form using the docmd.findrecord. If the record
is not found, I would like to display a message stating so. Here is my
code, but it always displays the not found message even if the record is
found.
Can anyone please help? Thanks

Private Sub CmdFindCert_Click()
On Error GoTo Err_CmdFindCert_Click

Dim cert As String
cert = InputBox("Enter a Certificate Number", "Search")
If cert = "" Then Exit Sub


Me.Certificate_Number.SetFocus
DoCmd.FindRecord cert, acEntire, , acSearchAll, , acCurrent
If Not IsNull(cert) Then
MsgBox "not found"
Else: Exit Sub


End If

Exit_CmdFindCert_Click:
Exit Sub

Err_CmdFindCert_Click:
MsgBox Err.Description
Resume Exit_CmdFindCert_Click

End Sub
 
W

Wayne Morgan

DoCmd.FindRecord cert, acEntire, , acSearchAll, , acCurrent
If Not IsNull(cert) Then

You are searching based on the value of "cert". You are then checking to see
if "cert" is Null. Well, you just typed in a value for "cert" when you did
the Input box and exited the sub if the value was "". So, if you got this
far then "cert" has a value and ISN'T Null. Your If statement is asking if
"cert" isn't Null, which it isn't, so the Msgbox gets displayed.

There is actually nothing here to indicate whether or not a record was
found. The DoCmd.FindRecord will move to the record if it is found or do
nothing if it isn't found. What you could do in your If statement would be
to compare the value of the field you were searching to "cert". If they
match, then a record was found. If they don't match, then a record wasn't
found. Should you happen to be sitting on a Null value in the searched
field, you may need to use the Nz function for the comparison.

Example:
If cert <> Nz([NameOfSearchedField]) Then
or
If cert <> Nz(Me.NameOfSearchedControl) Then
or
If cert <> Nz(Me.Controls("NameOfSearchedControl")) Then

In the last option, the "NameOfSearchedControl" could be replaced with a
string variable that contains the name of the control. Leave out the quotes
if you use a variable.
 

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