Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Code For - If Record Is Not Found Then MsBox etc. 2

Status
Not open for further replies.

dominicdunmow

Technical User
Jul 28, 2004
125
CA

I use the following code for a find record commandbutton on one a form.

I would like a message box to appear if the requested record is not found. Can anyone help me with the addition I need to make to this:-

Private Sub Find_Click()
' Find the record that matches the control.
Dim rs As Object
Set rs = Me.Recordset.Clone
rs.FindFirst "[ID] = " & Str(Nz(Me![Opp_ID_Search], 0))
If Not rs.EOF Then Me.Bookmark = rs.Bookmark
End Sub
 
[tt]rs.FindFirst "[ID] = " & Str(Nz(Me![Opp_ID_Search], 0))
If Not rs.EOF Then
Me.Bookmark = rs.Bookmark
else
msgbox "record not found"
end if[/tt]

Roy-Vidar
 
how's about

if not ... then
...
else
...
endif

--------------------
Procrastinate Now!
 
Modify the line below to.

If Not rs.EOF Then
Me.Bookmark = rs.Bookmark
Else
MsgBox "Record Not Found",vbExclamation,"Validation Error!"
End If

is that what you are looking for?
 
lol, well, there u go...

--------------------
Procrastinate Now!
 

Ah,

I've just realised the Else lines that you have both suggested are the same as what I ofirignally used, but it gives me the mesgbox everytime, even if the record does exist.

Any clues?
 
The code presented here, should either move to correct record or provide the message box. If it does both, then I'd guess the routine runs twice, for some reason. Try using the after update event of the control, in stead of the on click event.

Roy-Vidar
 
ah, yes, forgot...

you are cycling through all the records, so of course it's get to .eof...

there's many ways around this, but simplest for you would probably to set a variable to record if it's found or not:

dim fnd as Boolean
Dim rs As Object
Set rs = Me.Recordset.Clone
rs.FindFirst "[ID] = " & Str(Nz(Me![Opp_ID_Search], 0))
If Not rs.EOF Then
Me.Bookmark = rs.Bookmark
fnd = true
else
fnd = false
end if

if fnd = true then
msgbox "record not found"
endif

of course, there's easier ways of doing this but would involve changing more of your original code

--------------------
Procrastinate Now!
 
Ouch - scratch the after update, I thougth you where doing this from a combo, where the after update vs click could be an explanation of being run twice vs once. Don't know, similar works as it should on my setup.

Roy-Vidar
 
fyi, personally, I use something like:

dim rs as ...
set rs ...("select blah from blah where blah = blah")

this way, I can use

if rs.bof and rs.eof then
'not found
else
'found
endif

of course you are creating an entire recordset this way...

--------------------
Procrastinate Now!
 
And this ?
Private Sub Find_Click()
' Find the record that matches the control.
Dim rs As DAO.Recordset
Set rs = Me.RecordsetClone
rs.FindFirst "[ID] = " & Nz(Me![Opp_ID_Search], 0)
If Not rs.NoMatch Then
Me.Bookmark = rs.Bookmark
Else
MsgBox "No ID found"
End If
End Sub

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top