ADO Hierarchical Recordsets via SHAPE APPEND via C++/VBA/Java

ID: Q185425

The information in this article applies to:

SUMMARY

This article describes how to use the SHAPE APPEND syntax to produce hierarchical recordsets and how to traverse them. Sample code is provided for VBA.

MORE INFORMATION

Hierarchical recordsets present an alternative to using JOIN syntax when accessing parent-child data. Hierachical recordsets differ from a JOIN in that with a JOIN, both the parent table fields and child table fields are represented in the same recordset. With a hierarchical recordset, the recordset contains only fields from the parent table. In addition, the recordset contains an extra field that represents the related child data, which you can assign to a second recordset variable and traverse.

Hierachical recordsets are made available via the MSDataShape provider, which is implemented by the client cursor engine.

A new clause, SHAPE, is provided to relate SELECT statements in a hierarchical fashion. The syntax is summarized below:

   SHAPE {parent-command} [[AS] name]
   APPEND ({child-command} [[AS] name] RELATE parent-field TO child-field)
   [,({child2-command} ...)]

NOTE: The example below illustrates a hierarchical recordset using the publishers and titles tables in the SQL Server pubs database. The same code could easily be modified to use the Biblio Microsoft Access database that ships with Visual Studio. (You would need to change the SHAPE syntax and debug print for two fields: PUB_ID to PUBID and PUB_NAME to NAME.)

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures.

VBA Example

This step-by-step example is written for Visual Basic, but could be used in Microsoft Access just as easily.

1. Create a new VBA project and add a form named Form1 and a command button

   named Command1.

2. Add a reference to the Microsoft ActiveX Data Objects 2.0 Library.

3. Add the following code to the form:

      Private Sub Command1_Click()
        Dim cn As ADODB.Connection, rsPub As ADODB.Recordset, _
          rsTitle As ADODB.Recordset, SQL As String
        Set cn = New ADODB.Connection
        Set rsPub = New ADODB.Recordset
        cn.Provider = "MSDataShape"
        cn.Open "dsn=moonbase;uid=sa;pwd=;database=pubs"
        SQL = "SHAPE {SELECT * FROM publishers} APPEND " & _
              "({SELECT * FROM titles} AS PubTitles " & _
              "RELATE pub_id TO pub_id)"
        rsPub.Open SQL, cn, adOpenStatic, adLockReadOnly, adCmdText
        Do While Not rsPub.EOF
          Debug.Print "Publisher", rsPub!pub_name
          Set rsTitle = rsPub!PubTitles.Value
          Do While Not rsTitle.EOF
            Debug.Print , rsTitle!Title
            rsTitle.MoveNext
          Loop
          rsTitle.Close
          rsPub.MoveNext
        Loop
        rsPub.Close
        cn.Close
        Set rsTitle = Nothing
        Set rsPub = Nothing
        Set cn = Nothing
      End Sub

4. Run the form and click the command button. The list of publishers and
   titles will be displayed in the Debug/Immediate window.

REFERENCES

ADO Help; search on: "Shape Append Command"

For more information on SHAPE syntax, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q189657
   TITLE     : HOWTO: Use the ADO SHAPE Command

(c) Microsoft Corporation 1998, All Rights Reserved. Contributions by Malcolm Stewart, Microsoft Corporation Additional query words: OffCon epucon kbsweptmdac200 JEREMYE Feb-02-1999
Keywords          : kbADO kbADO200 kbDatabase kbJava KbVBA kbVC500 kbVB500 
Version           : WINDOWS:2.0
Platform          : WINDOWS

Last Reviewed: February 4, 1999