Creating Custom Hierarchical Recordsets, Part 2
By Scott Mitchell
In Part 1 we briefly looked at data shaping and custom Recordsets, mentioning that ADO supported the creation of custom hierarchical Recordsets. In this part, we'll look at the specific syntax needed to start creating such hierarchical Recordsets!
The Syntax for Creating Custom Hierarchical Recordsets
When creating a custom hierarchical Recordset, use the following syntax:
|
In the above example note that there are places to specify a column's DataTypeConstant.
These constants are defined in adovbs.inc, so you will need to, at minimum, use a server-side include
to dump the contents of adovbs.inc into this page. (To learn more about adovbs.inc
be sure to read this article.) You should also take a quick look
at the various data type names (they're under the heading DataTypeEnum Values; you can view a
copy of adovbs.inc online here.)
Of course, to turn the above syntax into an actual hierachical Recordset, we must create a Recordset object.
Since we are going to be adding rows to this custom Recordset, it is important to specify that the Recordset's
LockType is something other than read-only (adLockReadOnly). In the following
code block we specify the specific shape syntax, and create the hierarchical Recordset:
|
Finally, we need to add our data to the Recordset. Since we have a hierarchical Recordset we need to add both
information about each Employee and information on the projects they are working on. To make things simple, let's
begin by adding information on our employees. We use the .AddNew method to add a new column to the
Recordset, and the Update method to commit the newly added information.
Below you can see that our company has three employees:
|
Now that we've added our three employees, we can go ahead and add the list of projects for each employee. Note
that we begin by creating a child Recordset object that is set to the Value of the rsProjects
column in our parent Recordset:
|
Note that since each parent item (employee) is related to each project via the EmployeeID in the
child and parent Recordsets, we simply alter the EmployeeID column in the child Recordsets to assign
the various projects to various employees. Note that we could have also iterated through the rows of the
objParentRS Recordset, setting objChildRS equal to the Value of the
rsProjects column.
Now that we have inserted all this information into our hierarchical Recordset, how do we go about displaying it?
Since we have a Recordset within a Recordset, we must have two Do While Not objRS.EOF loops:
one to iterate through each row in the parent Recordset and the other to iterate through each child Recordset for
each row in the parent Recordset:
|
Conclusion
In this article we examined how to create custom hierarchical Recordsets in ADO. While this article only examined
how to create single custom parent-child hierarchies, you can create much more complex custom hierarchies using
the same syntax. For information on more complex data shaping relationships be sure to read
Overcoming Data Shaping Limitations and
Advanced Data Shaping Techniques.
Happy Programming!
Attachments:




