Here is a demo
The bottom line, I have written the code to load any treeview with just this much code. To include self referencing tables.
public tvw as Treeviewform
Private Sub Form_Load()
Set tvw = New TreeviewForm
tvw.Init Me.xTree.Object, "qryCustomers_Orders_OrderDetails", "None"
End Sub
Now the only trick is you have to build a query from you data.
1) For each table the query looks EXACTLY like this for the fields ID, parentID, and nodeText:
SELECT
[Identifier] & [CustomerID] AS ID,
"None" AS parentID,
[CustomerID] & " " & [CompanyName] AS nodeText,
"Cust" AS identifier
FROM Customers;
ID: is a combination of a unique IDentifier and a primary key
parentID: For the highest level table it is "None" unless self referencing. For any other table it is the parent table identifier and the foriegn key (see the demo)
Node Text: Whatever you want in the display
identifier: a unique identifier for the table
Here is the second table.
SELECT
[identifier] & [orders.OrderID] AS ID,
"Cust" & orders.customerID AS parentID,
"Order: " & [OrderID] & " Date: " & [OrderDate] AS nodeText, "Ord" AS identifier
FROM Customers INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID
ORDER BY "Cust" & orders.customerID;
Once you have wrote your queries do a union query on all of the other queries.
To initialize the treeview
tvw.Init Me.xTree.Object, "qryCustomers_Orders_OrderDetails", "None"
where the first parameter is the treeview object
the second is the name of your union query
and the third is the name of the parent ID of your first table
The code has numerous other properties and methods. I think it is better crafted then any MS examples I have seen.
When I use a tree view I want to be able to click on a node and return the primary key to that record so I can then do something with the record. So my treeviews are very particular about unique keys. The biggest difficulty with treeview is ensuring that every single node has a unique key.