Follow us:
access functions

MS Access: DLookup Function

This MSAccess tutorial explains how to use the Access DLookup function with syntax and examples.

Description

The Microsoft Access DLookup function returns a value from an Access table (or domain).

Syntax

The syntax for the DLookup function varies depending on what datatype you are uisng in the last parameter. Below we show how to use the DLookup function with numeric, string, and date value in the final parameter.

Numeric

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

String

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

Notice the single apostrophe before and after the string value.

Date

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

Notice the # symbol before and after the date value.

Parameters or Arguments

FieldName
A field, calculation, control on a form, or function that you wish to return.
TableName
The set of records. This can be a table or a query name.
Criteria
Optional. It is the WHERE clause to apply to the TableName.

Applies To

The DLookup function can be used in the following versions of Microsoft Access:

  • Access 2013, Access 2010, Access 2007, Access 2003, Access XP, Access 2000

Example

Let's look at how to use the DLookup function in MS Access:

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).

Example in VBA Code

The DLookup function can be used in VBA code in Microsoft Access.

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.

Example in SQL/Queries

You can also use the DLookup function in a query in Microsoft Access.

For example:

Microsoft Access

In this query, we have used the DLookup function as follows:

Expr1: DLookup("OrderDate","Order Details","OrderID = 10248")

This query will return the OrderDate from the Order Details table where the OrderID is equal to 10248. The results will be displayed in a column called Expr1. You can replace Expr1 with a column name that is more meaningful.

For example:

OrderDateValue: DLookup("OrderDate","Order Details","OrderID = 10248")

The results would now be displayed in a column called OrderDateValue.

Share: