Prevent Duplicates

G

Guest

I have a form where a value can be entered into text box. I obtained an if
statement from this newsgroup that prevented a value being entered if it was
a duplicate of one already in the database. That code was:

In BeforeUpdate event of the DocumentNumber control on the form...

If DCount("*", "YourTableName", "DocumentNumber = " & Me.DocumentNumber &
"") > 0 Then
MsgBox "Duplicate. Try Again"
Cancel = True
End If

This code works perfectly except if the user decides that they want to leave
the text box empty, in which case a horrible error message comes up. What
code should I add to that shown above so that Access allows a value of null?
 
F

Frank Stone

hi.
try something like this.
if isempty(your text box) then
msgbox("need input here")
exit sub
else
If DCount("*", "YourTableName", "DocumentNumber = "
& Me.DocumentNumber &
"") > 0 Then
MsgBox "Duplicate. Try Again"
Cancel = True
end if
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

Top