Searching if Record is in Table

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

Guest

Can someone help me put together the vba code that would search an access
table for a record entered into a form field and if the record is not found
enable the data entry fields to create the new record into the table.

Example of problem.
Is record 121212 in table X?
If so, return record 121212 already exists in table X.
If not, allow enable the data entry fields (name, address, etc...)

THANKS IN ADVANCE.
 
Create a field in the form that is unbound, where you can type the number you
want to search for

On the after upate event of the field run the code

' check if the record exist
If IsNull(DLookup("FieldName", "TableName", "FieldName='" &
Me.FieldnameInTheForm & "'")) Then
' if the record doesn't exist we'll move to a new record, and add the
number to the real field that is bound to the table
DoCmd.GoToRecord , , acNewRec
Me.RealFieldName = Me.FieldName
Else
' if the record exist we'll apply a filter and move to that record
Me.Filter = "FieldName = '" & Me.FieldName & "'"
Me.FilterOn = True
End If

============================================
' In this code sample I assume that the field is text, if the field is a
number type, then use this

' check if the record exist
If IsNull(DLookup("FieldName", "TableName", "FieldName=" &
Me.FieldnameInTheForm)) Then
' if the record doesn't exist we'll move to a new record, and add the
number to the real field that is bound to the table
DoCmd.GoToRecord , , acNewRec
Me.RealFieldName = Me.FieldName
Else
' if the record exist we'll apply a filter and move to that record
Me.Filter = "FieldName = " & Me.FieldName
Me.FilterOn = True
End If
=======================================================
 
OFER - YOU ARE THE MAN!!!! THANK YOU! ;-)

Ofer said:
Create a field in the form that is unbound, where you can type the number you
want to search for

On the after upate event of the field run the code

' check if the record exist
If IsNull(DLookup("FieldName", "TableName", "FieldName='" &
Me.FieldnameInTheForm & "'")) Then
' if the record doesn't exist we'll move to a new record, and add the
number to the real field that is bound to the table
DoCmd.GoToRecord , , acNewRec
Me.RealFieldName = Me.FieldName
Else
' if the record exist we'll apply a filter and move to that record
Me.Filter = "FieldName = '" & Me.FieldName & "'"
Me.FilterOn = True
End If

============================================
' In this code sample I assume that the field is text, if the field is a
number type, then use this

' check if the record exist
If IsNull(DLookup("FieldName", "TableName", "FieldName=" &
Me.FieldnameInTheForm)) Then
' if the record doesn't exist we'll move to a new record, and add the
number to the real field that is bound to the table
DoCmd.GoToRecord , , acNewRec
Me.RealFieldName = Me.FieldName
Else
' if the record exist we'll apply a filter and move to that record
Me.Filter = "FieldName = " & Me.FieldName
Me.FilterOn = True
End If
=======================================================
 

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