在下面的代码示例中Current Product List , 是一个存储在当前数据库中的现有 select 查询。
Dim dbsNorthwind As DAO.Database
Dim rstProducts As DAO.Recordset
Set dbsNorthwind = CurrentDb
Set rstProducts = dbsNorthwind.OpenRecordset("Current Product List")
如果存储的 select 查询尚不存在, 则**OpenRecordset** 方法还接受 SQL 字符串而不是查询的名称。 可以按如下所示重写上面的示例。
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)
此方法的缺点是每次运行查询字符串时都必须对其进行编译,而存储的查询则在首次保存时进行编译,这通常可以使性能稍有改善。