As Allen says indexing the column uniquely will control this at table level,
which will prevent duplicates regardless of how the data is entered, so
should be done. You can reinforce this at control level, however, by
validating the entry in the control's BeforeUpdate event procedure:
Const ConMESSAGE = "This value already exists in the table."
If Not IsNull(DLookup("MyID", "MyTable", "MyField = """ & Me.MyField &
"""")) Then
MsgBox conMESSAGE, vbExclamation, "Invalid Operation"
Cancel = True
End If
where MyID is the key of the table and MyField is the field you want to
contain only unique values. I've assumed its data type is text, hence the
quotes around the value. This will alert the user as soon as they enter a
value in the control. Its not entirely bullet-proof as in a multi-user
environment another user could simultaneously be entering the same value, and
if both users' records are not yet saved the duplication won't be detected
until an attempt is made to save the record by whichever user is slowest off
the mark in doing so. The index violation would be detected then, however,
so you might want to put some code in the form's Error event procedure to
handle this gracefully rather than presenting the user with the standard
system generated error message. You can find out what the error is by
temporarily putting MsgBox DataErr in the Error event procedure deliberately
triggering it to show the data error number. You can then replace the code
in the Error event procedure with code which tests for that DataErr value and
handles it in a more user friendly manner.
Ken Sheridan
Stafford, England