Discussion:
Preventing duplicates in certain fields of a form
(too old to reply)
wmdmurphy
2008-03-26 13:54:01 UTC
Permalink
I would like to prevent a user from entering a duplicate company name in a
form. I know this can be done by setting a unique index on company name in
the company table and I have done this. But I would also like to give the
user a message when they attempt this. I've tried adding code in before
update and the after update but so far this does not seem to work. What is
the best coding approach for this?

Bill
fredg
2008-03-26 16:12:29 UTC
Permalink
Post by wmdmurphy
I would like to prevent a user from entering a duplicate company name in a
form. I know this can be done by setting a unique index on company name in
the company table and I have done this. But I would also like to give the
user a message when they attempt this. I've tried adding code in before
update and the after update but so far this does not seem to work. What is
the best coding approach for this?
Bill
It really would have been nice if you had posted the actual code you
used.
Which BeforeUpdate? Which AfterUpdate? The Form's or the Control's?

Anyway, code the Company Name Control's BeforeUpdate event:

If DCount("*","TableName","[FieldName] = """ & Me.[ControlName] &
"""") >0 then
MsgBox "This company name has already been entered."
Cancel = True
End If

You will receive the message and focus will return to the control.
--
Fred
Please respond only to this newsgroup.
I do not reply to personal e-mail
Loading...