Table of contents

Application.DFirst 方法 (访问)Application.DFirst Method (Access)

office 365 dev account|上次更新日期: 2018/4/3
2 参与人员

可以使用DFirst函数可以从表或查询中的特定字段返回随机记录,只是需要字段中的任何值。You can use the DFirst function to return a random record from a particular field in a table or query when you simply need any value from that field.


expression.expression.DFirst(* * Expr,* *,* 条件)DFirst( ** Expr, ** Domain, ** Criteria* )

expression:表示 Application 对象的变量。expression A variable that represents an Application object.


名称Name必需/可选Required/Optional数据类型Data Type说明Description
ExprExpr必需RequiredStringString表达式,用于标识要从中查找第一个或最后一个值的字段。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.在_expr_中可以包括表、 窗体、 常量或函数上的控件中的字段的名称。In expr, you can include the name of a field in a table, a control on a form, a constant, or a function.如果_expr_包含一个函数,它可以是内置的或用户定义的但不是能是其他域聚合函数或 SQL 聚合函数。If expr includes a function, it can be either built-in or user-defined, but not another domain aggregate or SQL aggregate function.
DomainDomain必需RequiredStringString字符串表达式,用于标识组成域的记录集。A string expression identifying the set of records that constitutes the domain.
条件Criteria可选OptionalVariantVariant可选的字符串表达式,用于限制DFirst函数执行对象的数据范围。An optional string expression used to restrict the range of data on which the DFirst function is performed.例如,条件_通常是相当于在 SQL 表达式中,而无需 wrd WHERE 子句中。For example, _criteria is often equivalent to the WHERE clause in an SQL expression, without the wrd WHERE.如果省略_条件_, DFirst函数计算_expr_对整个域。If criteria is omitted, the DFirst function evaluates expr against the entire domain.在_条件_中包含的任何字段还必须在_域_; 域否则, DFirst函数将返回NullAny field that is included in criteria must also be a field in domain; otherwise, the DFirst function returns a Null.

返回值Return Value



请注意 如果您想要返回一组记录 (域) 中的第一个或最后一个记录,您应该创建一个查询按升序或降序并将TopValues属性设置为 1。Note If you want to return the first or last record in a set of records (a domain), you should create a query sorted as either ascending or descending and set the TopValues property to 1.从 Visual Basic 中,还可以创建 ADO记录集对象并使用MoveFirstMoveLast方法返回一组记录中的第一个或最后一个记录。From Visual Basic, you can also create an ADO Recordset object and use the MoveFirst or MoveLast method to return the first or last record in a set of records.


下面的示例演示如何使用DFirst函数中使用各种类型的条件。The following examples show how to use various types of criteria with the DFirst function.

示例代码提供方:社区成员图标UtterAccess 社区Sample code provided by:Community Member Icon TheUtterAccess community

    ' ***************************
    ' Typical Use
    ' Numerical values. Replace "number" with the number to use.
    variable = DFirst("[FieldName]", "TableName", "[Criteria] = number")

    ' Strings.
    ' Numerical values. Replace "string" with the string to use.
    variable = DFirst("[FieldName]", "TableName", "[Criteria]= 'string'")

    ' Dates. Replace "date" with the string to use.
    variable = DFirst("[FieldName]", "TableName", "[Criteria]= #date#")
    ' ***************************

    ' ***************************
    ' Referring to a control on a form
    ' Numerical values
    variable = DFirst("[FieldName]", "TableName", "[Criteria] = " &; Forms!FormName!ControlName)

    ' Strings
    variable = DFirst("[FieldName]", "TableName", "[Criteria] = '" &; Forms!FormName!ControlName &; "'")

    ' Dates
    variable = DFirst("[FieldName]", "TableName", "[Criteria] = #" &; Forms!FormName!ControlName &; "#")
    ' ***************************

    ' ***************************
    ' Combinations
    ' Multiple types of criteria
    variable = DFirst("[FieldName]", "TableName", "[Criteria1] = " &; Forms![FormName]![Control1] _
             &; " AND [Criteria2] = '" &; Forms![FormName]![Control2] &; "'" _
            &; " AND [Criteria3] =#" &; Forms![FormName]![Control3] &; "#")

    ' Use two fields from a single record.
    variable = DFirst("[LastName] &; ', ' &; [FirstName]", "tblPeople", "[PrimaryKey] = 7")

    ' Expressions
    variable = DFirst("[Field1] + [Field2]", "tableName", "[PrimaryKey] = 7")

    ' Control Structures
    variable = DFirst("IIf([LastName] Like 'Smith', 'True', 'False')", "tableName", "[PrimaryKey] = 7")
    ' ***************************

关于参与者About the Contributors

UtterAccess 是主要的 Microsoft Access Wiki 和帮助论坛。单击此处即可加入。UtterAccess is the premier Microsoft Access wiki and help forum. Click here to join.

另请参阅See also


Application 对象Application Object

© 2018 Microsoft