Search code examples
ms-accessvbams-access-2013dcount

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

Solution

  • 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