Article ID: 191744
Article Last Modified on 3/2/2005
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=dsnNwind;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
NOTE: Make sure that you change the Connect string appropriately for your system. That is, change "dsnNwind" to the name of a ODBC dsn that points to the Nwinds.MDB that comes with Visual Basic. Alternatively, create an ODBC DSN named dsnNwind that points to the Nwinds.MDB that comes with Visual Basic.189657 How To Use the ADO SHAPE Command
Keywords: kbhowto kbdatabase KB191744