Solved

Use DCOUNT, etc. with dynamic SQL statement in VBA

Posted on 2009-04-13
3
585 Views
Last Modified: 2012-05-06
In a VBA module, I need to get a count of records from a particular recordset, not a table.  Is there some way I can embed the SQL statement in the DCount instead of using a table name?  

Something like the Code sample below?


If DCount("Zip", "SELECT tbl_PostalCodes_US.Zip FROM tbl_PostalCodes_US WHERE (((tbl_PostalCodes_US.Zip)=Left('" & Me.[Zip] & "',5)));") > 1 Then

Open in new window

0
Comment
Question by:DHompster
  • 2
3 Comments
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 500 total points
ID: 24134678
you can use this

If DCount("Zip", "tbl_PostalCodes_US","[Zip]='" & Left(Me.[Zip],5) &"'") > 1 Then
0
 

Author Closing Comment

by:DHompster
ID: 31569767
Duh!  Thanks!  You're solution is perfect for my example.  Unfortunately, I gave a lame example.  What about the idea of substituting a SQL statement (a SELECT stmt) in the place of the table name?
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 24134920
No you can not use a select statement, it has to be a table or a saved query as the domain.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now