(Access) 的 Application.DLookup 方法

使用 DLookup 函式,從網域) (指定的記錄集取得特定欄位的值。

語法

運算式DLookup (ExprDomainCriteria)

expression 代表 Application 物件的變數。

參數

名稱 必要/選用 資料類型 描述
Expr 必要 字串 運算式,識別您要傳回其值的欄位。 它可以是識別資料表或查詢中欄位的字串運算式,也可以是對 該欄位中的資料執行計算的運算式expr 中可以包含資料表中欄位的名稱、表單上的控制項、常數或函數。 如果 expr 包含函式,它可以是內建或使用者定義,但不能是另一個網域匯總或 SQL 彙總函式。
網域 必要 String 字串運算式,識別構成網域的記錄集。 它可以是不需要參數之查詢的資料表名稱或查詢名稱。
Variant 用來限制在其上執行 DVarP 函數的資料範圍的選用的字串運算式。例如, criteria等於通常 WHERE 子句的 SQL 運算式,不含 where 位置。如果省略 criteria ,則 DVarP 函數會評估 expr針對整個網域。包含在 criteria任何欄位也必須是 domain; 中的欄位否則 DVarP 函數會傳回 Null 。 Variant 選用的字串運算式,用來限制要執行 DLookup 函數的資料範圍。 例如,criteria 通常相當於 SQL 運算式中的 WHERE 子句,但不用加上 WHERE 這個字。 如果省略 criteria,則 DLookup 函數會計算整個範圍的 exprcriteria 中包含的所有欄位也都必須是 domain 中的欄位,否則 DLookup 函數會傳回 Null

傳回值

Variant

註解

使用 DLookup 函式來顯示不在表單或報表記錄來源中的欄位值。 例如,假設您有以訂單詳細資料資料表為基礎的表單。 表單會顯示 OrderIDProductIDUnitPriceQuantityDiscount 欄位。 不過, ProductName 欄位在另一個資料表 Products 資料表中。 您可以在匯出控制項中使用 DLookup 函式,在相同的表單上顯示 ProductName

DLookup 函數會根據 criteria 中指定的資訊傳回單一欄位值。 雖然 criteria 是選擇性的引數,但是如果沒有為 criteria 提供值,DLookup 函數則會傳回範圍中的隨機值。

如果沒有任何記錄符合 準則,或網 未包含任何記錄, 則 DLookup 函式會傳回 Null

如果一個以上的欄位符合 criteria,則 DLookup 函數會傳回第一個相符項目。 您應該指定準則,確定 DLookup 函數傳回的欄位值是唯一的。 您可能想要針對準則使用主鍵值,如下列範例所 [EmployeeID] 示,以確保 DLookup 函式會傳回唯一的值:

Dim varX As Variant 
varX = DLookup("[LastName]", "Employees", "[EmployeeID] = 1")

在巨集、模組或計算控制項中使用 DLookup 函數時,必須小心建構 criteria 引數,以確定可以正確進行計算。

使用 DLookup 函數來指定 查詢之 Criteria 資料列中的準則、在查詢的匯出欄位運算式內,或在更新查詢的 [ 更新至 ] 資料列中指定準則。

如果您需要顯示的欄位不在表單或報表所根據的記錄來源中,您也可以在表單或報表上匯出控制項的運算式中使用 DLookup 函式。 例如,假設您有一個以訂單詳細資料資料表為基礎的訂單詳細資料表單,其中有一個名為 ProductID 的文字方塊會顯示 ProductID 欄位。 若要根據文字方塊中的值從 Products 資料表查閱 ProductName ,您可以建立另一個文字方塊,並將其 ControlSource 屬性設定為下列運算式:

=DLookup("[ProductName]", "Products", "[ProductID] =" _ 
     & Forms![Order Details]!ProductID)

提示

  • 雖然 DLookup 可用來顯示外部資料表中欄位的值,但是為兩個資料表建立包含所需欄位的查詢,再依該查詢建立表單或報表的方法更有效率。
  • 您也可使用「查詢精靈」在外部資料表中尋找值。

範例

下列範例會從符合準則之記錄的CompanyName欄位傳回名稱資訊。 範圍為 Shippers 資料表。 criteria 引數會限制為其 ShipperID 等於 1 的記錄結果集。

Dim varX As Variant 
varX = DLookup("[CompanyName]", "Shippers", "[ShipperID] = 1")

Shippers 資料表的下一個範例會使用表單控制項 ShipperID,為 DLookup 函數提供準則。 請注意,控制項的參照不會包含於表示為字串的引號內。 如此可確定每次呼叫 DLookup 函數時,Microsoft Access 都將取得控制項目前值。

Dim varX As Variant 
varX = DLookup("[CompanyName]", "Shippers", "[ShipperID] = " _ 
    & Forms!Shippers!ShipperID)

下一個範例會使用變數 intSearch 取得值。

Dim intSearch As Integer 
Dim varX As Variant 
 
intSearch = 1 
varX = DLookup("[CompanyName]", "Shippers", _ 
    "[ShipperID] = " & intSearch)

以下範例示範如何搭配使用各種準則與 DLookup 函式。

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

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

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

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

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

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

    ' ***************************
    ' Combinations
    ' Multiple types of criteria
    variable = DLookup("[FieldName]", "TableName", "[Criteria1] = " & Forms![FormName]![Control1] _
             & " AND [Criteria2] = '" & Forms![FormName]![Control2] & "'" _
            & " AND [Criteria3] =#" & Forms![FormName]![Control3] & "#")
    
    ' Use two fields from a single record.
    variable = DLookup("[LastName] & ', ' & [FirstName]", "tblPeople", "[PrimaryKey] = 7")
            
    ' Expressions
    variable = DLookup("[Field1] + [Field2]", "tableName", "[PrimaryKey] = 7")
    
    ' Control Structures
    variable = DLookup("IIf([LastName] Like 'Smith', 'True', 'False')", "tableName", "[PrimaryKey] = 7")
    ' ***************************
```The following example shows how to use **DLookUp** in a Do Loop. It demonstrates how to build the Criteria string on each pass through the loop.

```vba
' The loop verifies data from an input data set, in this case Operating System names, 
' against those contained in a Master List.

Do Until I1 > NRec1
    ' An apostrophe is concatenated at the beginning and at the end of a datum referenced by "rs1!OS", 
    ' which is then concatenated to build the entire criteria string.
    Str_2 = "'" & rs1!OS & "'"
    Str_1 = "[OS] = " & Str_2
    
    J1 = DLookup("[ID]", "tbl_81_Operating_Systems_Master_List", Str_1)
    
    If IsNull(J1) = True Then
        ' If an OS name is not found, then a flag is set and the name of the unknown OS is output to a table.
        rs3.AddNew
        rs3.OS = rs1!OS
        rs3.Update
        Err_Fl = False

    End If

    rs1.MoveNext
    I1 = I1 + 1

Loop

支援和意見反應

有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應