DSum与tempvars返回错误

时间:2016-03-08 18:54:22

标签: vba ms-access access-vba

我有一些VBA代码设置了TempVar

TempVars!ThisQtr = quarter
TempVars!LastQtr = lastQuarter
TempVars!LastYr = lastYear
TempVars!ThisYr = currentYear

大!我知道它确实设置了它,因为我已多次检查。
现在我的问题:
我正在尝试在查询中使用它

DSum("[SumBase]","CompareUnionQuery","[AU] = '" & [AU] & "' AND [GRP_ID] = " & [GRP_ID] & " AND [ACCOUNT] = '" & [ACCOUNT] & "'" & " AND [Fiscal_Year] = " & [TempVars]![ThisYr] & " AND [QTR]= " & [TempVars]![ThisQtr])

当我使用值更改TempVars时,这会起作用。谁能帮我这个? 我试图在它们周围加一条引号,但这不起作用。

enter image description here

这个表达式确实有效:

DSum("[SumBase]","CompareUnionQuery","[AU] = '" & [AU] & "' AND [GRP_ID] = " & [GRP_ID] & " AND [ACCOUNT] = '" & [ACCOUNT] & "'" & " AND [Fiscal_Year] = 2015 AND [QTR]= '3'")

1 个答案:

答案 0 :(得分:1)

比较DSum表达式的最后几部分......

  1. AND [QTR]= '3'")
  2. AND [QTR]= " & [TempVars]![ThisQtr])
  3. 您报告#1有效,#2触发“数据类型不匹配”错误。因此,在TempVar值之前和之后添加单引号...

    AND [QTR]= '" & [TempVars]![ThisQtr] & "'")