Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Treeview Control Issues

Status
Not open for further replies.

MelissaKT

Technical User
Jun 22, 2004
95
0
0
US
I have a treeview control on one of my access forms. The first part of the code that I use to populate the control is working fine. However, the second part, or the child portion, isn't populating like it should. It actually acts more like a parent. I can't see what's wrong - maybe I've stared at it too long? I would really appreciate any help!

Code:
    Dim strQuery1 As String
    Dim strQuery2 As String
    Dim rst As ADODB.Recordset
    Dim strNode1Text As String
    Dim strNode2Text As String
    Dim strVisibleText As String
    Dim nod As Object
    Set rst = New ADODB.Recordset
    strQuery1 = "spTreeviewDAyOfSaleParcels"
    strQuery2 = "spTreeViewDayOfSaleParcelINFO"
    Dim cnn As New ADODB.Connection
    Set cnn = Application.CurrentProject.Connection
    With Me.tvParcels
        'Fill Level 1
        
        rst.Open strQuery1, cnn, adOpenKeyset, adLockOptimistic
            Do Until rst.EOF
                strNode1Text = StrConv("Level1" & CStr(rst!id), _
                vbLowerCase)
                Set nod = .nodes.Add(Key:=strNode1Text, _
                Text:=rst!parcel)
                nod.Expanded = True
                rst.MoveNext
            Loop
        rst.Close

         'Fill Level 2
         rst.Open strQuery2, cnn, adOpenKeyset, adLockOptimistic

            Do Until rst.EOF
                strNode1Text = StrConv("Level1" & CStr(rst![id]), vbLowerCase)
                strNode2Text = StrConv("Level2" & CStr(rst![id]) & Nz(rst![INFO], "") & Nz(rst![ItemNO], ""), vbLowerCase)
                strVisibleText2 = Nz(rst![INFO], "")
                .nodes.Add relative:=strNode1Text, relationship:=tvwChild, Key:=strNode2Text, Text:=strVisibleText
               rst.MoveNext
             Loop

         rst.Close


    End With
 
I work with treeviews often so I have encapsulated a ton of functionality into a class module. I can load any data into a tree view with only a few lines of code. To make this reuseable I use a common query naming convention and then I union multiple queries together. The key is to create a query with the following fields.

Identifier: a unique identifier for the query
ID: a concatenation of the Identifier and the Primary Key
ParentID: Foreign key to parent ID
NodeText: The text you want to see

Example:

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;

The class module is here

If you figure out the query structure then loading the treeview and working with it is simple.

Look at the query, then the form code for initializing a treeview. Any treeview can be then made with two lines of code.

Set tvw = New TreeviewForm
tvw.Init Me.xTree.Object, "qryCustomers_Orders_OrderDetails", "None"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top