How to Test if a Record Exists

  • Thread starter Thread starter HumanJHawkins
  • Start date Start date
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.
 
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]
 
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]
 

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

Back
Top