How to Test if a Record Exists

H

HumanJHawkins

Hi all,

For the recordset associated with a form, how can I tell if a record is
in the set... For example, if my primary key is pkID, how can I tell if
the form that the user is looking at has pkID 15 in it's dataset?

(Once again) I attempted to use for example:

Me.pkID.SetFocus
Me.FindRecord (15)

And this works just fine as far as the UI is concerned... It will jump
down and highlight the row where pkID is 15. But the trouble is that I
need to programatically know whether it found it or not. And FindRecord
doesn't return a value to let me know if it found anything or not.

Thanks in advance for any help.
 
J

John Vinson

Hi all,

For the recordset associated with a form, how can I tell if a record is
in the set... For example, if my primary key is pkID, how can I tell if
the form that the user is looking at has pkID 15 in it's dataset?

(Once again) I attempted to use for example:

Me.pkID.SetFocus
Me.FindRecord (15)

And this works just fine as far as the UI is concerned... It will jump
down and highlight the row where pkID is 15. But the trouble is that I
need to programatically know whether it found it or not. And FindRecord
doesn't return a value to let me know if it found anything or not.

Thanks in advance for any help.

Ignore the spammer.

I'd suggest using a bit more code:

Dim rs As DAO.Recordset
Set rs = Me.RecordsetClone
rs.FindFirst "[pkID] = " & Me.txtFindPK
If rs.NoMatch Then
MsgBox "Record not found"
Else
Me.Bookmark = rs.Bookmark
End If
Set rs = Nothing

This searches for the value in an (unbound) textbox named txtFindPK
rather than for a literal 15, but you see where this is going...

John W. Vinson[MVP]
 
H

HumanJHawkins

Thanks!

John said:
Hi all,

For the recordset associated with a form, how can I tell if a record is
in the set... For example, if my primary key is pkID, how can I tell if
the form that the user is looking at has pkID 15 in it's dataset?

(Once again) I attempted to use for example:

Me.pkID.SetFocus
Me.FindRecord (15)

And this works just fine as far as the UI is concerned... It will jump
down and highlight the row where pkID is 15. But the trouble is that I
need to programatically know whether it found it or not. And FindRecord
doesn't return a value to let me know if it found anything or not.

Thanks in advance for any help.

Ignore the spammer.

I'd suggest using a bit more code:

Dim rs As DAO.Recordset
Set rs = Me.RecordsetClone
rs.FindFirst "[pkID] = " & Me.txtFindPK
If rs.NoMatch Then
MsgBox "Record not found"
Else
Me.Bookmark = rs.Bookmark
End If
Set rs = Nothing

This searches for the value in an (unbound) textbox named txtFindPK
rather than for a literal 15, but you see where this is going...

John W. Vinson[MVP]
 
Top