VBA 中的 DCount() 始终返回空值

DCount() In VBA Always Returning A Null Value

我正在尝试使用 DCount() 函数从我的 table 中 return 计数。我的问题是它总是 return 是 NULL 值。

我应该如何重写此 VBA 语句以使其 return 成为准确的计数?

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

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

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

使用即时 window 测试您的 D 函数将简化调试。

你应该使用:

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

它至少会 return 0(零)...如果 table 和字段名称是正确的。