validation rule for not allowing duplicate entries

G

guggd2868

Hi yall

I am trying to set a form to enter data into a table, but I want to be
able to set up a validation rule that will stop users if they enter in
a value that has already been input. The form is for putting in
document ID numbers and names. Right now, if I enter a doc number
that already exists, Access just ignores the entire entry and doesn't
add anything to the table. Obviously, this is a problem. Is there a
way to add a validation rule that refers back to other entries in the
same field?

Thanks

D Gugg
 
K

Klatuu

Use the form's Before Update event and check the table using a DLookup to
see if the value already exists in the field:

If Not IsNull(DLookup("DocumentID", "DocumentTable", "DocumentID = " &
Me.txtDocumentID)) Then
MsgBox "Document Number " & Me.txtDocumentID & " Already Exists
Cancel = True
Me.txtDocumentID.Undo
End If

The above code assumes DocumentID is a numeric field in your table.
Change the names to use your actual names.
 
T

tedmi

Look up the BeforeUpdate Event (not proerty) in VB help. The example there
does exactly what you need. Use the event of the form, not any of the fields
on it.
 

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

Top