Make a query Count() return 0 instead of empty

calendar_today Asked Jul 30, 2009
thumb_up 9 upvotes
history Updated April 14, 2026

Direct Answer

You can return ISNULL(Count(……), 0) and all should be fine – would be in MS SQL Server – but I just saw you're using Access. Since I don't know Access enough, I'm not sure…. This is a prose walkthrough, ranked #46th of 67 by community upvote score, from 2009.


The Problem (Q-score 3, ranked #46th of 67 in the Access VBA archive)

The scenario as originally posted in 2009

I have a report that tracks how long certain items have been in the database, and does so by tracking it over a series of age ranges (20-44, 45-60, 61-90, 91-180, 180+). I have the following query as the data source of the report:

SELECT DISTINCT Source.ItemName, 
Count(SELECT Source.DateAdded FROM Source WHERE Int(Date()-Source.DateAdded) > 20) AS Total, 
Count(SELECT Source.DateAdded FROM Source WHERE Int(Date()-Source.DateAdded) BETWEEN 20 AND 44) AS BTWN_20_44, 
Count(SELECT Source.DateAdded FROM Source WHERE Int(Date()-Source.DateAdded) BETWEEN 45 AND 60) AS BTWN_45_60, 
Count(SELECT Source.DateAdded FROM Source WHERE Int(Date()-Source.DateAdded) BETWEEN 61 AND 90) AS BTWN_61_90, 
Count(SELECT Source.DateAdded FROM Source WHERE Int(Date()-Source.DateAdded) BETWEEN 91 AND 180) AS BTWN_91_180, 
Count(SELECT Source.DateAdded FROM Source WHERE Int(Date()-Source.DateAdded) > 180) AS GT_180
FROM Source
GROUP BY Source.ItemName;

This query works great, except if there aren’t any entries a column. Instead of returning a count of 0, an empty value is returned.

How do I get Count() to return a 0 instead of empty?

Why community consensus is tight on this one

Across 67 Access VBA entries in the archive, the accepted answer here holds solid answer (above median) status — meaning voters are unusually aligned on the right fix.


The Verified Solution — solid answer (above median) (+9)

Verbal answer — walkthrough without a code block

Note: the verified answer is a prose walkthrough. If you need a runnable sample, check Access VBA entries ranked in the top 10 of the same archive.

You can return

ISNULL(Count(......), 0)

and all should be fine – would be in MS SQL Server – but I just saw you’re using Access. Since I don’t know Access enough, I’m not sure this will work – can you try it?

OK – glad to see there’s something similar in Access (if not exactly the same as in SQL Server).

Marc


When to Use It — vintage (14+ years old, pre-2013)

Ranked #46th in its category — specialized fit

This pattern sits in the 71% tail relative to the top answer. Reach for it when your scenario closely matches the question title; otherwise browse the Access VBA archive for a higher-consensus alternative.

What changed between 2009 and 2026

The answer is 17 years old. The Access VBA object model has been stable across Office 2013, 2016, 2019, 2021, 365, and 2024/2026 LTSC, so the pattern still compiles. Changes that might affect you: 64-bit API declarations (use PtrSafe), blocked macros in downloaded files (Mark-of-the-Web), and the shift toward Office Scripts for web-first workflows.

help
Frequently Asked Questions

Is this above-median answer still worth copying?
expand_more

Answer score +9 vs the Access VBA archive median ~4; this entry is solid. The score plus 3 supporting upvotes on the question itself (+3) means the asker and 8 subsequent voters all validated the approach.

The answer has no code block — how do I turn it into a snippet?
expand_more

Use the walkthrough above as a checklist, then open a top-10 Access VBA archive entry for a concrete starting template you can adapt.

This answer is 17 years old. Is it still relevant in 2026?
expand_more

Published 2009, which is 17 year(s) before today’s Office 2026 build. The Access VBA object model has had no breaking changes in that window. Three things to re-test: (1) blocked macros on downloaded files (Mark-of-the-Web), (2) 64-bit API declarations (PtrSafe, LongPtr), (3) any shift toward Office Scripts for web scenarios.

Which Access VBA pattern ranks just above this one at #45?
expand_more

The pattern one rank above is “username and password prompt when trying to do SQL queries when connecting Microsoft Access to Delphi 7”. If your use case overlaps, compare both before committing.

Data source: Community-verified Q&A snapshot. Q-score 3, Answer-score 9, original post 2009, ranked #46th of 67 in the Access VBA archive. Last regenerated April 14, 2026.