適用先: Access 2013、Office 2013
この例では、階層 Recordset 内の行へのアクセスを容易にする StayInSync プロパティの機能を示します。
外側のループで、各作成者の姓名、州、および ID を表示します。 各行に追加された Recordset が Fields コレクションから取得され、親の Recordset が新しい行に移動するたびに StayInSync プロパティによって自動的に rstTitleAuthor に割り当てられます。 内側のループでは、追加されたレコードセットの各行から 4 つのフィールドを表示します。
'BeginStayInSyncVB
Public Sub Main()
On Error GoTo ErrorHandler
'To integrate this code create a DSN called Pubs
'with a user_id = sa and no password
Dim Cnxn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim rstTitleAuthor As New ADODB.Recordset
Dim strCnxn As String
' open connection with Data Shape attributes
Set Cnxn = New ADODB.Connection
strCnxn = "Provider=MSDataShape;Data Provider='sqloledb';Data Source='MySqlServer';" & _
"Initial Catalog='Pubs';Integrated Security='SSPI';"
Cnxn.Open strCnxn
' create recordset
Set rst = New ADODB.Recordset
rst.StayInSync = True
rst.Open "SHAPE {select * from Authors} " & _
"APPEND ( { select * from titleauthor} AS chapTitleAuthor " & _
"RELATE au_id TO au_id) ", Cnxn, , , adCmdText
Set rstTitleAuthor = rst("chapTitleAuthor").Value
Do Until rst.EOF
Debug.Print rst!au_fname & " " & rst!au_lname & " " & _
rst!State & " " & rst!au_id
Do Until rstTitleAuthor.EOF
Debug.Print rstTitleAuthor(0) & " " & rstTitleAuthor(1) & " " & _
rstTitleAuthor(2) & " " & rstTitleAuthor(3)
rstTitleAuthor.MoveNext
Loop
rst.MoveNext
Loop
' clean up
rst.Close
Cnxn.Close
Set rst = Nothing
Set Cnxn = Nothing
Exit Sub
ErrorHandler:
' clean up
If Not rst Is Nothing Then
If rst.State = adStateOpen Then rst.Close
End If
Set rst = Nothing
If Not Cnxn Is Nothing Then
If Cnxn.State = adStateOpen Then Cnxn.Close
End If
Set Cnxn = Nothing
If Err <> 0 Then
MsgBox Err.Source & "-->" & Err.Description, , "Error"
End If
End Sub
'EndStayInSyncVB