HomePrivacy PolicyFeedbackLink to usSite Map

MS Access: DLookup Function


In Access, the DLookup function returns a value from a specified set of records (or domain).

For numerical values:

DLookup("FieldName" , "TableName" , "Criteria = n")

For strings:  (note the single apostrophe before and after the string value)

DLookup("FieldName" , "TableName" , "Criteria= 'string'")

For dates:  (note the # before and after the date value)

DLookup("FieldName" , "TableName" , "Criteria= #date#")

FieldName is a field, calculation, control on a form, or function that you wish to return.

TableName is the set of records. This can be a table or a query name.

Criteria is optional. It is the WHERE clause to apply to the TableName.


Acknowledgements: A special thanks to Carlos for contributing to this definition.


For example:

Let's take a look at a simple example:

DLookup("[UnitPrice]", "Order Details", "OrderID = 10248")

In this example, you would be retrieving the UnitPrice field from the Order Details table where the OrderID is 10248. This is the same as the following SQL statement:

SELECT UnitPrice AS Expr1
FROM [Order Details]
WHERE ((([Order Details].OrderID)=10248));


You can also retrieve a calculation using the DLookup function. For example:

DLookup("UnitPrice * Quantity", "Order Details", "OrderID = 10248")

This example would return the UnitPrice field multiplied by the Quantity field from the Order Details table where the OrderID is 10248. This is the same as the following SQL statement:

SELECT UnitPrice * Quantity AS Expr1
FROM [Order Details]
WHERE ((([Order Details].OrderID)=10248));


You could also use a form control in the DLookup function. For example:

DLookup("CustomerID", "Orders", "OrderID = " & Forms![Orders]!OrderID)

This example would return the CustomerID field from the Orders table for the record that is currently being displayed in the Orders form (based on OrderID).


VBA Code

The DLookup function can be used in VBA code. For example:

Dim LDate As Date

LDate = DLookup("OrderDate", "Orders", "OrderID = 10248")

In this example, the variable called LDate would now contain the OrderDate value from the Orders table where the OrderID is 10248.


SQL/Queries

You can also use the DLookup function in a query.