Recordset2.NextRecordset 方法 (DAO)
适用于:Access 2013、Office 2013
语法
表达式 。NextRecordset
表达 一个代表 Recordset2 对象的变量。
返回值
布尔值
备注
在 ODBCDirect 工作区中,可以打开一个 Recordset,其中包含 OpenRecordset 的源参数中的多个选择查询,或打开 select query QueryDef 对象的 SQL 属性,如以下示例所示。
SELECT LastName, FirstName FROM Authors
WHERE LastName = 'Smith';
SELECT Title, ISBN FROM Titles
WHERE Pub_ID = 9999
将使用第一个查询打开返回的 Recordset。 若要从后续查询获取记录的结果集,请使用 NextRecordset 方法。
如果有更多的可用记录(也就是说, OpenRecordset 调用或 SQL 属性中有另一个选择查询),从下一个查询返回的记录将被载入 Recordset,同时 NextRecordset 将返回 True,表示记录可用。 如果没有更多的可用记录(也就是说,最后一个选择查询的结果已被载入 Recordset),则 NextRecordset 将返回 False,且 Recordset 为空。
还可以使用 Cancel 方法刷新 Recordset 的内容。 但是, Cancel 还会刷新任何尚未加载的附加记录。
示例
以下示例使用 NextRecordset 方法查看 SELECT 复合查询中的数据。 在执行此类查询时, DefaultCursorDriver 属性必须设置为 dbUseODBCCursor。 即使部分或全部 SELECT 语句返回零记录, NextRecordset 方法也会返回 True;仅当检查了所有单个 SQL 子句后,它才返回 False。
Sub NextRecordsetX()
Dim wrkODBC As Workspace
Dim conPubs As Connection
Dim rstTemp As Recordset2
Dim intCount As Integer
Dim booNext As Boolean
' Create ODBCDirect Workspace object and open Connection
' object. The DefaultCursorDriver setting is required
' when using compound SQL statements.
Set wrkODBC = CreateWorkspace("", _
"admin", "", dbUseODBC)
wrkODBC.DefaultCursorDriver = dbUseODBCCursor
' Note: The DSN referenced below must be set to
' use Microsoft Windows NT Authentication Mode to
' authorize user access to the Microsoft SQL Server.
Set conPubs = wrkODBC.OpenConnection("Publishers", , , _
"ODBC;DATABASE=pubs;DSN=Publishers")
' Construct compound SELECT statement.
Set rstTemp = conPubs.OpenRecordset("SELECT * " & _
"FROM authors; " & _
"SELECT * FROM stores; " & _
"SELECT * FROM jobs")
' Try printing results from each of the three SELECT
' statements.
booNext = True
intCount = 1
With rstTemp
Do While booNext
Debug.Print "Contents of recordset #" & intCount
Do While Not .EOF
Debug.Print , .Fields(0), .Fields(1)
.MoveNext
Loop
booNext = .NextRecordset
Debug.Print " rstTemp.NextRecordset = " & _
booNext
intCount = intCount + 1
Loop
End With
rstTemp.Close
conPubs.Close
wrkODBC.Close
End Sub
完成同一任务的另一种方法是创建一个包含 SQL 复合语句的预备语句。 QueryDef 对象的 CacheSize 属性必须设置为 1, Recordset 对象必须为仅向前类型,并且是只读的。
Sub NextRecordsetX2()
Dim wrkODBC As Workspace
Dim conPubs As Connection
Dim qdfTemp As QueryDef
Dim rstTemp As Recordset2
Dim intCount As Integer
Dim booNext As Boolean
' Create ODBCDirect Workspace object and open Connection
' object. The DefaultCursorDriver setting is required
' when using compound SQL statements.
Set wrkODBC = CreateWorkspace("", _
"admin", "", dbUseODBC)
wrkODBC.DefaultCursorDriver = dbUseODBCCursor
' Note: The DSN referenced below must be set to
' use Microsoft Windows NT Authentication Mode to
' authorize user access to the Microsoft SQL Server.
Set conPubs = wrkODBC.OpenConnection("Publishers", , , _
"ODBC;DATABASE=pubs;DSN=Publishers")
' Create a temporary stored procedure with a compound
' SELECT statement.
Set qdfTemp = conPubs.CreateQueryDef("", _
"SELECT * FROM authors; " & _
"SELECT * FROM stores; " & _
"SELECT * FROM jobs")
' Set CacheSize and open Recordset object with arguments
' that will allow access to multiple recordsets.
qdfTemp.CacheSize = 1
Set rstTemp = qdfTemp.OpenRecordset(dbOpenForwardOnly, _
dbReadOnly)
' Try printing results from each of the three SELECT
' statements.
booNext = True
intCount = 1
With rstTemp
Do While booNext
Debug.Print "Contents of recordset #" & intCount
Do While Not .EOF
Debug.Print , .Fields(0), .Fields(1)
.MoveNext
Loop
booNext = .NextRecordset
Debug.Print " rstTemp.NextRecordset = " & _
booNext
intCount = intCount + 1
Loop
End With
rstTemp.Close
qdfTemp.Close
conPubs.Close
wrkODBC.Close
End Sub