Skip to content

Latest commit

 

History

History
40 lines (26 loc) · 1.4 KB

create-a-dao-recordset-from-a-query.md

File metadata and controls

40 lines (26 loc) · 1.4 KB
title ms.assetid ms.date ms.localizationpriority
Create a DAO Recordset from a query
d84870d4-58e4-9d48-9951-72d928929002
09/21/2018
medium

Create a DAO Recordset from a query

You can create a Recordset object based on a stored select query. In the following code example, Current Product List is an existing select query stored in the current database.

Dim dbsNorthwind As DAO.Database 
Dim rstProducts As DAO.Recordset 
 
Set dbsNorthwind = CurrentDb 
Set rstProducts = dbsNorthwind.OpenRecordset("Current Product List") 

If a stored select query does not already exist, the OpenRecordset method also accepts an SQL string instead of the name of a query. The previous example can be rewritten as follows.

Dim dbsNorthwind As DAO.Database 
Dim rstProducts As DAO.Recordset 
Dim strSQL As String 
 
Set dbsNorthwind = CurrentDb 
strSQL = "SELECT * FROM Products WHERE Discontinued = No " & _ 
         "ORDER BY ProductName" 
Set rstProducts = dbsNorthwind.OpenRecordset(strSQL) 

The disadvantage of this approach is that the query string must be compiled each time it runs, whereas the stored query is compiled the first time it is saved, which usually results in slightly better performance.

[!includeSupport and feedback]