Application.DLookup 方法 (Access)

使用 DLookup 函数可以从指定记录集 (域) 获取特定字段的值。

语法

表达式。DLookup (Expr、 域、 条件)

expression:表示 Application 对象的变量。

参数

名称 必需/可选 数据类型 说明
Expr 必需 字符串 表达式,用于标识想要返回其值的域。 它可以是标识表或查询中字段的字符串表达式,也可以是执行该字段中数据的计算的表达式。 在 expr 中,可以在表中包括字段的名称、窗体上的控件、常量或函数。 如果 expr 包含函数,它可以是内置函数或用户定义的函数,但不能是其他域聚合函数或 SQL 聚合函数。
域 必需 字符串 字符串表达式,用于标识组成域的记录集。 可以是表名称或不需要参数的查询的查询名称。
Criteria 可选 Variant 可选的字符串表达式,用于限制作为 DLookup 函数执行对象的数据的范围。 例如,criteria通常是相当于 SQL 表达式中的 WHERE 子句位置,但是不使用 WHERE一词。 如果criteria省略, DLookup函数对整个域求 expr的值。 条件中包含的任何字段也必须是域中的字段;否则,DLookup 函数返回 Null。

返回值

Variant

备注

使用 DLookup 函数可显示不在窗体或报表的记录源中的字段的值。 例如,假设有一个基于订单明细表的表单。 窗体显示 OrderID、 ProductID、 UnitPrice、 Quantity 和 Discount 字段。 但是, “ProductName” 字段位于另一个表中,即“产品”表。 可以在计算控件中使用 DLookup 函数在同一窗体上显示 ProductName 。

DLookup 函数根据条件中指定的信息返回单个字段值。 尽管 criteria 是一个可选参数,但如果不为 criteria 提供值, DLookup 函数将在域中返回一个随机值。

如果没有记录满足 条件,或者 域 不包含任何记录, 则 DLookup 函数将返回 Null。

如果多个字段满足 条件, DLookup 函数将返回第一个匹配项。 应指定条件以确保DLookup函数返回的域值唯一。 可以使用主键值作为条件,如[EmployeeID]以下示例,以确保DLookup函数返回唯一值:

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

无论是在宏或模块、查询表达式还是计算控件中使用 DLookup 函数,都必须仔细构造 criteria 参数,以确保正确计算它。

使用 DLookup 函数可在查询的 “条件” 行、查询的计算字段表达式中或更新查询的 “更新到 ”行中指定条件。

如果要显示的字段不在窗体或报表所基于的记录源中,也可以在窗体或报表上计算控件中的表达式中使用 DLookup 函数。 例如,假设你有一个基于“订单详细信息”表的“订单详细信息”窗体,其中包含一个名为“ProductID”的文本框,该文本框显示 “ProductID ”字段。 若要根据文本框中的值从 Products 表中查找 ProductName ,可以创建另一个文本框,并将其 ControlSource 属性设置为以下表达式:

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

提示

  • 虽然可以使用 DLookup 函数显示来自外表字段中的值,但是通过创建包含两表中所需字段的查询,然后将窗体或报表建立在这个查询的基础上,效率将更高。
  • 也可以使用“查阅向导”来查找外表中的值。

示例

以下示例从满足条件的记录的 CompanyName 字段中返回名称信息。 域是运货商表。 criteria 参数将生成的记录集限制为 ShipperID 等于 1 的记录集。

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

“发货人”表中的下一个示例使用窗体控件“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 支持和反馈,获取有关如何接收支持和提供反馈的指南。