HOWTO: Extract Child Recordsets from Shaped RecordsetID: Q191744
|
When shaping a hierarchical recordset, examine the Type property of each field object to determine if it is actual data or a child recordset (Type = adChapter).
Consider the following sample SHAPE statement:
SHAPE {SELECT * FROM customers}
APPEND ({SELECT * FROM orders} AS rsOrders
RELATE customerid TO customerid)
The first N columns of the recordset returned correspond to the columns
returned by the SQL statement in the first set of brackets after the SHAPE statement. That is, the first N columns will be actual data. After that, a given column in the recordset may be of type adChapter, which indicates a child recordset, or it could be data from a calculated column. (This is not demonstrated in the preceding SQL statement.)
Public Sub PrintTbl(rs, indent)
Dim s As String, col As ADODB.Field, rsChild As ADODB.Recordset
' This routine distinguishes between columns in the recordset with
' data, i.e. type <> adChapter, and those which contain a child
' recordset, for example, type = adChapter.
Do While Not rs.EOF
s = Space(indent)
For Each col In rs.Fields
If col.Type <> adChapter Then
If Len(s) > indent Then s = s & " | "
s = s & col.Value
Else
' Display data columns encountered so far (if any).
If Len(s) > indent Then Debug.Print Space(indent) & s
' Recursively call printtbl to display child recordset.
Set rsChild = col.Value
PrintTbl rsChild, indent + 4
' Reset in case there are further data columns.
s = Space(indent)
End If
Next
' In case we have any data columns that have not been
' displayed yet.
If Len(s) > indent Then Debug.Print s
rs.MoveNext
Loop
End Sub
Private Sub Command1_Click()
Dim strConnect, rst As ADODB.Recordset
Set rst = New ADODB.Recordset
strConnect = "Provider=MSDataShape;data provider=msdasql;" _
& "data source=AdvWorks;database=nwind;"
rst.Source = "SHAPE {SELECT * FROM customers} APPEND " _
& "({SELECT * FROM orders} AS rsOrders " _
& "RELATE customerid TO customerid)"
rst.ActiveConnection = strConnect
rst.Open , , adOpenStatic, adLockBatchOptimistic
debug.print " PRINTING CUSTOMERS TABLE"
printtbl rst, 0
Set rst.ActiveConnection = Nothing
rst.Close
Set rst = Nothing
End Sub
For additional information about shaping syntax and hierarchical recordsets, please see the following article in the Microsoft Knowledge Base:
Q189657 HOWTO: Use the ADO SHAPE Command
http://www.microsoft.com/msdn/news/feature/datajul98/newado20.htm
Additional query words: kbVC600 kbDatabase kbADO200
Keywords : kbADO200 kbDatabase kbVBp600 kbMDAC200
Version : WINDOWS:2.0
Platform : WINDOWS
Issue type : kbhowto
Last Reviewed: May 24, 1999