Recordset2.NextRecordset メソッド (DAO)

適用先: Access 2013、Office 2013

構文

式 。NextRecordset

式Recordset2 オブジェクトを表す変数。

戻り値

Boolean

注釈

ODBCDirect ワークスペースでは、次の例のように、OpenRecordset のソース引数に複数の選択クエリを含む Recordset、または選択クエリ 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 に設定する必要があります。 NextRecordset メソッドは、一部またはすべての SELECT ステートメントが 0 件のレコードを返す場合でも True を返し、 False を返すのは、個別の SQL 句をすべて確認した後のみです。

    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