Validate a record exists

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to validate a record exists in one of my tables from a button on
my form. I was wondering if someone could explain how to do this.

Here is the full problem:

On my form it has customer information. At the bottom of the form I have a
button "Questionnaire." When this button is pushed it opens a bound form
connected to my tblQuestionnaire. However, sometimes the account doesn't have
a record in that table. In that case I want to open a similar form (unbound)
that will allow me to create the record and insert it into the
tblQuestionnaire.

I don't know the code to open a table check if my acct # matches an acct# in
the table.

Could someone please help?
 
James:

One approach would be to open a recordset and look at the RecordCount
property to see if any matching records were found. The following code
assumes a textbox named txtAccountNumber on the form. For example:

Function test35()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim sSQL As String

sSQL = "SELECT * FROM MyTable WHERE AccountNumber=" & txtAccountNumber

Set db = CurrentDb

Set rs = db.OpenRecordset(sSQL)

If rs.RecordCount = 0 Then
'Open unbound form and create new record
Else
'Open bound form
End If

Set rs = Nothing
Set db = Nothing

End Function

--
David Lloyd
MCSD .NET
http://LemingtonConsulting.com

This response is supplied "as is" without any representations or warranties.


I am trying to validate a record exists in one of my tables from a button on
my form. I was wondering if someone could explain how to do this.

Here is the full problem:

On my form it has customer information. At the bottom of the form I have a
button "Questionnaire." When this button is pushed it opens a bound form
connected to my tblQuestionnaire. However, sometimes the account doesn't
have
a record in that table. In that case I want to open a similar form (unbound)
that will allow me to create the record and insert it into the
tblQuestionnaire.

I don't know the code to open a table check if my acct # matches an acct# in
the table.

Could someone please help?
 
Back
Top