Accessing Rows in a Hierarchical Recordset (Example)

The following example shows the steps necessary to access rows in a hierarchical Recordset:

  1. Recordset objects from the authors and titleauthor tables are related by author ID.

  2. The outer loop displays each author's first and last name, state, and identification.

  3. The appended Recordset for each row is retrieved from the Fields collection and assigned to rstTitleAuthor.

  4. The inner loop displays four fields from each row in the appended Recordset.

The StayInSync property is set to false for purposes of illustration, so that you can see the chapter change explicitly in each iteration of the outer loop. To make the code example more efficient, you can move the assignment in step 3 before the first line in step 2, so that the assignment is performed only once. Then set the StayInSync property to true, so that rstTitleAuthor will implicitly and automatically change to the corresponding chapter whenever rst moves to a new row.


Sub datashape()
   Dim cnn As New ADODB.Connection
   Dim rst As New ADODB.Recordset
   Dim rstTitleAuthor As New ADODB.Recordset

   cnn.Provider = "MSDataShape"
   cnn.Open    "Data Provider=MSDASQL;" & _
               "Data Source=SRV;Integrated Security=SSPI;Database=Pubs"
' STEP 1
   rst.StayInSync = FALSE
   rst.Open    "SHAPE  {select * from authors} "  & _
               "APPEND ({select * from titleauthor} " & _
               "RELATE au_id TO au_id) AS chapTitleAuthor", _
' STEP 2
   While Not rst.EOF
      Debug.Print    rst("au_fname"), rst("au_lname"), _
                     rst("state"), rst("au_id")
' STEP 3
      Set rstTitleAuthor = rst("chapTitleAuthor").Value
' STEP 4
      While Not rstTitleAuthor.EOF
         Debug.Print rstTitleAuthor(0), rstTitleAuthor(1), _
                     rstTitleAuthor(2), rstTitleAuthor(3)
End Sub

See Also

Data Shaping Overview Field Object Fields Collection (ADO) Formal Shape Grammar Microsoft Data Shaping Service for OLE DB (ADO Service Provider) Recordset Object (ADO) Required Providers for Data Shaping Shape APPEND Clause Shape Commands in General Shape COMPUTE Clause Visual Basic for Applications functions