Only display query results if records found

SalesOp

Registered User.
Local time
Today, 08:04
Joined
Apr 6, 2016
Messages
21
Dear Experts

In access macro I would like to display the results of an OpenQuery only if the query returns records. I don’t want to open empty query result sheets. I know that DCount() returns the number of the records in a query but I am not sure how to use it without actually opening an IF block in macro. I don’t need the IF block since there is nothing to do in the Then and Else sections.

I tried the RunCode with DCount("*","qryValue-00-Month")>0 but it didn’t work and didn’t display the records.

Thanks in advance.
SalesOp
 
Yes Dcount works, star, then name of query.

Code:
If Dcount("*","qsMyQuery")=0 then
   MsgBox "no results"
Else
  Docmd.openquery "qsMyQuery"
End if
 
Or, in one line only...
Code:
If DCount("*","qsMyQuery") Then DoCmd.OpenQuery "qsMyQuery"
 
Thanks for replies,

However, this will entail that the query will be executed twice once to compute the number of records for Dcount and the 2nd time for OpenQuery and display the results. Seems a bit inefficient if the query is complex.

Any thought?
 
Don't peel the banana if it is bruised on the inside. You have to peel the banana to know if it is bruised. Impasse.
 
No, do count does NOT execute the query. It counts.
 
How does DCount() count records returned by a query without executing the query?
 

Users who are viewing this thread

Back
Top Bottom