DFirst, DLast Functions

Description

You can use the DFirst and DLast functions to return a random record from a particular field when you simply need any value from that field.

Syntax

DFirst(expr, domain[, criteria])DLast(expr, domain[, criteria])

The DFirst and DLast functions use the following arguments.

Argument Description
expr Expression that identifies the field from which you want find the first or last value. It can be either a string expression identifying a field in a table or query, or an expression that performs a calculation on data in that field. You can include in expr the name of a table field, a control on a form, a constant, or a function. If expr includes a function, it can be either built-in or user-defined, but not another domain aggregate or SQL aggregate function.
domain String expression identifying the set of records that constitutes the domain.
criteria Optional string expression used to restrict the range of data on which DFirst or DLast is performed. For example, criteria is often equivalent to the WHERE clause in an SQL expression, without the word WHERE. If criteria is omitted, DFirst and DLast evaluate expr against the entire domain. Any field included in criteria must also be a field in domain; otherwise, DFirst and DLast return a Null.


Remarks

You can use DFirst and DLast in a calculated control on a form or report, in a query expression, or in a macro or module.

You can use DFirst and DLast to return a random record in a table or query.

If you want to return the first or last record in a set of records, you should create a query sorted as either Ascending or Descending and set the TopValues property to 1. For more information, see the TopValues property topic. From Visual Basic, you can also create a Recordset object, and use the MoveFirst or MoveLast method to return the first or last record in a set of records.

See Also

DLookup Function, Domain Aggregate Functions.