Generate Query Definition

Chris Morris

Registered User.
Local time
Today, 23:30
Joined
May 3, 2011
Messages
20
Hoping you can help.

I'd like to use DLookup, DCount etc. on a SQL string held within the VBA code itself. Something like:

stSQL = "SELECT * From Contacts"

MsgBox DCount("Contact_ID", stSQL, "Deceased = False")


I'm trying to avoid creating lots of queries.
 
Unless you save this Query as a Temporary Query, the only way to do is,
Code:
MsgBox CurrentDb.OpenRecordSet("SELECT Count(*) FROM (" & strSQL & ") As tmpQry WHERE tmpQry.Deceased = False").Fields(0)
 
Or perhaps something like:
MsgBox DCount("Contact_ID", "Contacts", "Deceased = False")

Dont overuse DLookups/Dcounts, queries usually are faster, particularly if you are next going to do
MsgBox DCount("Contact_ID", "Contacts", "Deceased = True")

and more Dcounts on Contacts.

Something like:
Code:
Select 
sum(iif(Deceased;1;0) ) Deceased,
sum(iif(Deceased;0;1) ) Living
From Contacts
Will be faster, much more faster if you get more and more dcounts/sums
 

Users who are viewing this thread

Back
Top Bottom