IcyPopTarts
IcyPopTarts

Reputation: 504

DCount() In VBA Always Returning A Null Value

I am attempting to use the DCount() Function to return a count from my table. My issue is that it always returns a NULL value.

How should I Re-write this VBA statement so that it returns the accurate count?

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

Upvotes: 0

Views: 1099

Answers (2)

Gustav
Gustav

Reputation: 55831

You should use:

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

It will at least return 0 (zero) ... if the table and field names are correct.

Upvotes: 0

user6432984
user6432984

Reputation:

NameOfItem implies a string. You need to wrap strings in single quotes when passing them as a parameter to a D-Function; just like passing them as a parameter in a Query.

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

Using the immediate window to test your D-Functions will simplify debugging.

enter image description here

Upvotes: 1

Related Questions