ACC: Macro to Check for Duplicate Values in a FieldID: Q103135
|
Novice: Requires knowledge of the user interface on single-user computers.
This article demonstrates how to create a macro that uses the DLookup()
function to check for duplicate values in a field.
Microsoft Access provides automatic checking for duplicate values in a
field that has been designated as the primary key. However, this check
occurs after all fields for a record have been entered, just before the
record is committed to the database. The following sample macro is based on
the sample database Northwind.mdb (or NWIND.MDB in versions 1.x and 2.0)
and will perform a duplicate check as the data is entered in a field:
Macro Name Condition Action
--------------------------------------
MyMacro 1. MsgBox
... SendKeys
1. DLookup("[CustomerID]", "Customers", "[CustomerID] = _
Form.[CustomerID]") is Not Null
NOTE: In versions 1.x and 2.0, there is a space in Customer ID.
MyMacro Actions
--------------------------------------
MsgBox
Message: That value already exists.
SendKeys
Keystrokes: +{TAB}
Wait: No
Note that the ellipsis (...) under the condition indicates that the
condition controls both macro actions. The SendKeys action will
return the pointer to the field just exited.
For more information about duplicate values in a field, search the Help Index for "duplicate values," or ask the Microsoft Access 97 Office Assistant.
Additional query words: validate same validationrule
Keywords : kbusage McrCond
Version : 1.0 1.1 2.0 7.0 97
Platform : WINDOWS
Issue type : kbinfo
Last Reviewed: March 25, 1999