在Access2007中使用Dlookup函数时出错

时间:2011-06-22 21:40:01

标签: ms-access ms-access-2007 access-vba

我在Access2007的报告中使用以下Dlookup函数;即使我更改了条件,该函数也始终返回查询的第一条记录。这是代码:

    =DLookUp("Result","Query1","[Date_Registered] = # " & "2011/02/01#" And "[Department] =" & 'IT')

根据上述标准,返回的值应位于记录#4中,但始终返回查询的第一条记录的值。请帮忙。 谢谢,

1 个答案:

答案 0 :(得分:1)

您给予DLookup的标准不是有效的VBA字符串。如果你打破它,并像这样将它提供给Debug.Print,它将触发编译错误:

Debug.Print "[Date_Registered] = # " & "2011/02/01#" And "[Department] =" & 'IT'

我认为你应该构建你的字符串并将其加载到变量中,然后Debug.Print变量,最后将变量作为第三个(条件)参数传递给DLookup。我不知道日期字符串和'IT'的来源,但这应该指向正确的方向:

Dim strCriteria As String
strCriteria = "[Date_Registered] = #" & "2011/02/01" & _
    "# AND [Department] =" & "'IT'"
Debug.Print strCriteria
Debug.Print DLookup("Result", "Query1", strCriteria)

该代码片段中的第一个Debug.Print语句将打印到立即窗口:

[Date_Registered] = #2011/02/01# AND [Department] ='IT'