Using VBA to Run Queries

You can run queries from VBA in a number of different ways. You can run either SQL strings or queries that are already stored in your database in a variety of ways. Table 12.1 shows some common methods of running queries that are available to you.

Table 12.1. Ways to Run Queries from VBA
Action, Method, or Object Object Runs From Source Example
runSQL action DoCmd SQL string
docmd.runSQL strSQL 
OpenQuery action DoCmd Stored query
docmd.OpenQuery 
"qryTotals" 
OpenRecordset method (DAO) Connection or database SQL string or stored query
db. 
OpenRecordset 
strSQL 
Open method (ADO) Connection or recordset SQL string, stored query or stored procedure
rs.Open "SELECT 
* FROM 
Customers, cn 
Execute method (DAO) Connection ...

Get Access 2002 Programming by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.