DFirst Method

Microsoft Access Visual Basic

Show All

DFirst Method

       

Use the DFirst function to return a random record from a particular field in a table or query, when you need any value from that field. Use the DFirst function in a macro, module, query expression, or calculated control on a form or report. Variant.

expression.DFirst(Expr, Domain, Criteria)

expression   Required. An expression that returns one of the objects in the Applies To list.

Expr  Required String. An expression that identifies the field from which you want to 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. In expr, you can include the name of a field in a table, 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  Required String. A string expression identifying the set of records that constitutes the domain.

Criteria  Optional Variant. An optional string expression used to restrict the range of data on which the DFirst function is performed. For example, criteria is often equivalent to the WHERE clause in an SQL expression, without the word WHERE. If criteria is omitted, the DFirst function evaluates expr against the entire domain. Any field that is included in criteria must also be a field in domain; otherwise, the DFirst function returns a Null.

Remarks

Note   If you want to return the first record in a set of records (a domain), you should create a query sorted as 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.

Example

The following example prints the value of the "OrderDate" field from the Orders table in the Immediate window in the Visual Basic Editor. Microsoft Access picks one of the field records at random. This example is useful for quickly displaying  the contents of a field to check data consistency.

? DFirst("[Orders]![OrderDate]", "[Orders]")