Application.DLast 方法 (Access)Application.DLast method (Access)

若要從資料表或查詢中的特定欄位傳回隨機的記錄,當您需要從該欄位的任何值時,您可以使用DLast函數。You can use the DLast function to return a random record from a particular field in a table or query when you need any value from that field.

語法Syntax

運算式DLastExpr網域準則expression.DLast (Expr, Domain, Criteria)

expression 代表 Application 物件的變數。expression A variable that represents an Application object.

參數Parameters

名稱Name 必要/選用Required/Optional 資料類型Data type 描述Description
ExprExpr 必要Required StringString 運算式,這會識別要尋找其第一個值或最後一個值的欄位。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 必要Required StringString 字串運算式,識別構成網域的記錄集。A string expression identifying the set of records that constitutes the domain.
CriteriaCriteria 選用Optional VariantVariant 選用的字串運算式,用來限制要執行 DLast 函數的資料範圍。An optional string expression used to restrict the range of data on which the DLast function is performed. 例如,criteria 通常相當於 SQL 運算式中的 WHERE 子句,但不用加上 WHERE 這個字。For example, criteria is often equivalent to the WHERE clause in an SQL expression, without the wrd WHERE. 如果省略 criteria,則 DLast 函數會計算整個範圍的 exprIf criteria is omitted, the DLast function evaluates expr against the entire domain. criteria 中包含的所有欄位也都必須是 domain 中的欄位,否則 DLast 函數會傳回 NullAny field that is included in criteria must also be a field in domain; otherwise, the DLast function returns a Null.

傳回值Return value

VariantVariant

註解Remarks

注意

如果想要傳回一組記錄 (範圍) 內的第一筆或最後一筆記錄,則應該建立遞增或遞減排序的查詢,並將 TopValues 屬性設定為 1。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 Recordset 物件,並使用 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.

範例Example

下列範例顯示如何使用各種類型的準則與DLast函數。The following examples show how to use various types of criteria with the DLast function.

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

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

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

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

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

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

    ' ***************************
    ' Combinations
    ' Multiple types of criteria
    variable = DLast("[FieldName]", "TableName", "[Criteria1] = " & Forms![FormName]![Control1] _
             & " AND [Criteria2] = '" & Forms![FormName]![Control2] & "'" _
            & " AND [Criteria3] =#" & Forms![FormName]![Control3] & "#")
    
    ' Use two fields from a single record.
    variable = DLast("[LastName] & ', ' & [FirstName]", "tblPeople", "[PrimaryKey] = 7")
            
    ' Expressions
    variable = DLast("[Field1] + [Field2]", "tableName", "[PrimaryKey] = 7")
    
    ' Control Structures
    variable = DLast("IIf([LastName] Like 'Smith', 'True', 'False')", "tableName", "[PrimaryKey] = 7")
    ' ***************************

支援和意見反應Support and feedback

有關於 Office VBA 或這份文件的問題或意見反應嗎?Have questions or feedback about Office VBA or this documentation? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.