简体   繁体   English

MS Access DLOOKUP,带有文本和嵌套DLOOKUP作为条件

[英]MS Access DLOOKUP with text and nested DLOOKUP for criteria

I have a combobox on a form that contains search terms. 我在包含搜索词的表单上有一个组合框。 The user chooses a search term and this looks up to a table containing the number X. The RVU (a number) of X is looked up in another table given the category is equal to the string 'PHYS'. 用户选择一个搜索词,然后查找包含数字X的表。如果类别等于字符串“ PHYS”,则在另一个表中查找X的RVU(数字)。 I was using nested DLOOKUP statements to look up the number X and then use that number X and the string criteria to look up the RVU. 我使用嵌套的DLOOKUP语句查找数字X,然后使用该数字X和字符串条件查找RVU。 Here's my code: 这是我的代码:

FH_array(0) = Val(Nz(DLookup("[RVU]", "[FORES IP Picker]", "[IP]= " & Val(Nz(DLookup("[FORES]", "[IP Number Xwalk]", "[Reference Name] = '" & Me.Ref_Name & "'"), 0))), ""))

I wasn't having luck so I broke it down to debug: 我没有运气,所以我将其分解以进行调试:

a = Val(Nz(DLookup("[FORES]", "[IP Number Xwalk]", "[Reference Name] = '" & Me.Ref_Name & "'"), 0))
Debug.Print "a:"; a 'returns value 279
aa = Val(nz(DLookup("[RVU]", "[FORES IP Picker]", "[IP] = " & a & " and [Cost Category] = 'PHYS')))
Debug.Print "aa:"; aa

I'm getting a syntax error on the line for variable aa. 我在变量aa的行上遇到语法错误。 if I changed the code from 如果我更改了代码

aa = DLookup("[RVU]", "[FORES IP Picker]", "[IP] = " & a & " and [Cost Category] = 'PHYS')

to

aa = DLookup("[RVU]", "[FORES IP Picker]", "[Cost Category] = 'PHYS'" And "[IP] = " & a)

I get a run-time error 13 type mismatch 我收到运行时错误13类型不匹配

All the variables are declared as variant and called properly. 所有变量都声明为variant,并已正确调用。 The array FH_array is sized correctly. 数组FH_array的大小正确。 I copied this code from another database that does the same type of nested DLOOKUP but it has only one criteria and therefore works. 我从另一个执行相同类型的嵌套DLOOKUP的数据库复制了此代码,但是它只有一个条件,因此可以正常工作。 I can't figure out what syntax I'm missing or where the type mismatch is to get it to work. 我无法弄清楚缺少什么语法,或者类型不匹配的地方才能使其正常工作。

You need a single valid string as the DLookup criteria option. 您需要一个有效的字符串作为DLookup条件选项。 Use the Immediate window to examine what you have for the criteria in the final DLookup example. 使用立即窗口检查最终DLookup示例中的条件条件。

Debug.Print "[Cost Category] = 'PHYS'" And "[IP] = " & a

That is actually a "logical conjunction" of two strings. 这实际上是两个字符串的“逻辑合取”。 (That will make more sense if you review the Access help topic for the And Operator .) (如果您查看And运算符的Access帮助主题,那将更有意义。)

And since you're using And with two string expressions, Access complains about type mismatch, the same as it does with this simpler example: 并且由于您使用的是And与两个字符串表达式,因此Access会抱怨类型不匹配,这与以下简单示例相同:

Debug.Print "a" And "b"

So you need to create a single valid string for the criteria option ... 因此,您需要为条件选项创建一个有效的字符串。

a = 279
Debug.Print "[Cost Category] = 'PHYS' And [IP] = " & a
[Cost Category] = 'PHYS' And [IP] = 279

Translating that back to the last DLookup in your question ... 将其转换回您问题中的最后一个DLookup ...

Dim strCriteria As String
strCriteria = "[Cost Category] = 'PHYS' And [IP] = " & a
Debug.Print strCriteria ' <- just to make sure you got what you need
aa = DLookup("[RVU]", "[FORES IP Picker]", strCriteria)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM