Others

Fix Microsoft Access DLookup Expression to Exclude Current Record

Partner EditorPartner Editor Oct 9, 2026 868 views

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.

Fix an Access DLookup Expression and Exclude the Current Record
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

Open your Microsoft Access form in Design View, select the control you are validating, and open its property sheet to access the VBA editor.

2
Locate the BeforeUpdate Event

Find the BeforeUpdate event for your specific field (e.g., JobItem_BeforeUpdate), where the data validation should be performed before the record is saved.

3
Append the Exclusion Criteria

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] & "'")

4
Verify Text Quotes

Ensure that any text-based primary key (like studentNo in the example) is wrapped in single quotes (') within the double-quoted criteria string.

5
Save and Test

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.

Update the Criteria String with the AND Operator and Correct Quotes
Text vs. Numeric Primary Keys: If your primary key is numeric rather than text, you do not need the single quotation marks around the variable in the criteria string. Simply omit the single quotes in the concatenation.
Free Microsoft Office alternative

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. 1. Download and Install WPS Office: Get WPS Office for free from the official website and install it on your device.
  2. 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. 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.
Free to use with a familiar, easy-to-learn interfaceSeamless compatibility with Microsoft Office file formats (XLSX, DOCX, PPTX)Advanced spreadsheet functions (like VLOOKUP and XLOOKUP) for data validation without writing VBA codeLightweight application that runs smoothly on most devices
microsoft office alternative - wps office

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.