0

我正在尝试使用 DCount() 函数从我的表中返回一个计数。我的问题是它总是返回一个 NULL 值。

我应该如何重新编写此 VBA 语句以使其返回准确的计数?

ReturnedCount = DCount("CountOfItems", "[__TestTable]", "NameOfItem = " & ItemName)
Debug.Print ReturnedCount
4

2 回答 2

1

NameOfItem暗示一个字符串。将字符串作为参数传递给 D-Function 时,需要用单引号括起来;就像在查询中将它们作为参数传递一样。

ReturnedCount = DCount("CountOfItems", "[__TestTable]", "NameOfItem = '" & ItemName & "'")

使用即时窗口测试您的 D-Function 将简化调试。

在此处输入图像描述

于 2017-10-18T20:23:53.070 回答
0

你应该使用:

On Error Goto 0
ReturnedCount = DCount("*", "[__TestTable]", "NameOfItem = '" & ItemName & "'")

如果表名和字段名正确,它至少会返回 0(零)。

于 2017-10-18T20:39:24.910 回答