Fix Microsoft Access DLookup Expression to Exclude Current Record
Question details
The user needs to correct a DLookup expression in MS Access to properly validate data without flagging the currently edited record as a duplicate.

- Product
- Microsoft Access
- Device & OS
- not provided
- Scenario
- Writing a VBA validation script in Access using DLookup to prevent duplicate entries while updating an existing record.
- Observed behavior
- The DLookup function returns an error due to missing operators or incorrect quotes, or falsely flags the current record as a duplicate during a record update.
Open your Microsoft Access database in Design View and ensure you have access to the VBA editor for the specific form or control you are modifying.
Update the Criteria String with the AND Operator and Correct Quotes
Modify the DLookup criteria string to correctly format text variables and exclude the current primary key to avoid duplicate errors on update.
When a user edits an existing record, failing to exclude that specific record in your DLookup criteria will cause Access to detect the record as a duplicate of itself. Additionally, text-based primary keys must be carefully enclosed in single quotation marks within the criteria string.
Open your Microsoft Access form in Design View, select the control you are validating, and open its property sheet to access the VBA editor.
Find the BeforeUpdate event for your specific field (e.g., JobItem_BeforeUpdate), where the data validation should be performed before the record is saved.
Rewrite the DLookup function to append the AND operator and <> (not equal to) condition for the current record. Use this structure: vntMsgTC = DLookup("[Lname] & ', ' & [fname] & ' - Job Item ' & [JobItem]", "Students", "[JobItem] = " & Me.[JobItem] & " AND studentNo <> '" & Me.[studentNo] & "'")
Ensure that any text-based primary key (like studentNo in the example) is wrapped in single quotes (') within the double-quoted criteria string.
Save your VBA code, switch back to Form View, and test updating an existing record to ensure it no longer triggers a false duplicate error.

Looking for a Lightweight Alternative to Manage Your Data?
While Microsoft Access is a powerful database tool, many data tracking and validation tasks can be easily managed using spreadsheets. WPS Office provides a free, highly compatible, and lightweight alternative to Microsoft Office. With a familiar UI, you can seamlessly migrate your tabular data to WPS Spreadsheet and use built-in data validation to prevent duplicates without complex VBA coding.
- 1. Download and Install WPS Office: Get WPS Office for free from the official website and install it on your device.
- 2. Export Access Data to Excel Format: In MS Access, right-click your table and export it as an Excel (.xlsx) file, which is fully compatible with WPS Spreadsheet.
- 3. Manage Data with WPS Spreadsheet: Open the exported file in WPS Spreadsheet and use the 'Data Validation' feature under the Data tab to easily prevent duplicate entries without using code.

Frequently Asked Questions
Why does DLookup falsely identify a record as a duplicate when I edit it?
When updating an existing record, the DLookup function searches the entire table. If you do not explicitly exclude the current record's primary key from the search criteria, the function finds the pre-edited version of the same record and assumes it is a duplicate entry.
How do I handle date variables in a DLookup criteria string?
Unlike text variables that require single quotation marks, date variables in Microsoft Access VBA criteria strings must be enclosed in hash or pound signs (#). For example: "[OrderDate] = #" & Me.[OrderDate] & "#".
In which event should I place the duplicate validation code in Access?
Data validation checks using DLookup are typically placed in the BeforeUpdate event of the specific control (e.g., a text box) or the form itself. This allows you to catch the error and cancel the update before the data is committed to the database.




