tech on the net
Home About Us Feedback Site Map

Microsoft

Access Excel Word

Database

SQL Oracle / PLSQL SQL Server MySQL MariaDB PostgreSQL

Web Development

HTML CSS Color Picker

Language

C Language

More

ASCII Table Linux UNIX Java Clipart Joke of the Moment
Share this page:

MS Access: DLookup Function

Learn 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

Syntax for numerical values:

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

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

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

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

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

Parameters or Arguments

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.

Applies To

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

Example

Let's 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).

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.

Microsoft Access