VBA & Macro Problems

Use DLookup with an Access Lookup Field in VBA

Elise WilliamsElise Williams Nov 25, 2026 868 views

Question details

Use DLookup in VBA to find a record based on the displayed value of an Access lookup field.

Use DLookup with an Access Lookup Field in VBA
Product
Microsoft Access
Device & OS
not provided
Scenario
A VBA procedure needs to find an employee whose position is displayed as CEO in an Access lookup field.
Observed behavior
The lookup field displays position text, but the table stores a numeric foreign key, so criteria using the displayed text may not match.
Before you start

Open the tables in Access and check the lookup field's stored foreign key and the related table's position ID and text fields. Replace the example table and field names with those in your database.

Solution 1Recommended

Look up the position ID before finding the employee

Use this two-stage DLookup when the employee table stores a numeric position ID but displays a text label.

An Access lookup field can display a value from a related table while storing its key. Search the related table for the displayed text first, then use the returned key in the employee table.

1
Open the VBA editor

In Access, press Alt+F11 and open the module or event procedure where you want to perform the lookup.

2
Find the position ID

Declare a Variant and retrieve the ID with: Dim positionID As Variant positionID = DLookup("[FieldPositionID]", "[Positions]", "[FieldPosition] = 'CEO'")

3
Find the matching employee

Check for Null before using the ID. For example, if Table1 stores the key in FieldPositionID and the result field is EmployeeName, use: Dim employeeName As Variant If Not IsNull(positionID) Then employeeName = DLookup("[EmployeeName]", "[Table1]", "[FieldPositionID] = " & CLng(positionID)) End If

4
Check the result

Run the procedure and check positionID and employeeName in the VBA editor. A Null result means the corresponding DLookup did not find a match.

Use the stored key's data type: Numeric criteria do not need quotes. If the stored key is text instead, enclose its value in quotes in the criteria string and escape any quotes within the value.
Free Microsoft Office alternative

Work with exported Access data in WPS Spreadsheets

WPS Office is a free, lightweight alternative for working with Office documents in a familiar interface. Export Access data to Excel format to move it into WPS Spreadsheets; WPS does not run Access DLookup queries or fix Access VBA.

Review and analyze data exported from Access in a spreadsheetOpen and edit Microsoft Excel XLSX and XLS filesUse a familiar spreadsheet interface for a smooth transitionWork with a free, lightweight Office suite
microsoft office alternative - wps office

Frequently Asked Questions

Why doesn't DLookup match the text displayed in my Access lookup field?

The field may display text from a related table while storing a numeric foreign key. Look up the text in the related table first, then use its ID in the criteria for the table containing the lookup field.

What does a Null result from DLookup mean?

DLookup returns Null when no record matches the criteria or when the returned field is Null. Store the result in a Variant and use IsNull before converting or using it.

Do I need quotes around a DLookup criterion?

Use quotes around text values, such as "[FieldPosition] = 'CEO'". For a numeric key, concatenate the number without surrounding it with quotes.

Can DLookup return more than one matching employee?

No. DLookup returns one value, and if several records match without an ordering guarantee, the returned match may not be the one you expect. Use an Access query if you need all matching employees.