Checking For Duplicate Values

P

Playa

I am trying to check for duplicate values in a table but am unsure how to do
this with code on a form. Does anyone know how to code this?
 
S

Sandra Daigle

In the BeforeUpdate event of the *primary key control (or controls)* try
something like the
following:

Private Sub Custid_BeforeUpdate(Cancel As Integer)
if not isnull(dlookup("Custid","myTable","Custid=" & me.Custid)) then
msgbox "Record Already exists!"
cancel=true
endif

End Sub


The Dlookup will return a null if there is no match so you are looking
for a Not null condition to indicate a duplicate. If any of your
fields are text you will have to wrap the value in Quotes so that the
value is passed to the Dlookup as a literal string. There are many
posts in this newsgroup that describe how to do this if you aren't
sure - or post again and I'll give you the syntax.
 

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