Checking For Duplicate Values

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

Guest

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?
 
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.
 
Back
Top