Solved

Excel - TreeView Control - reading tree items

Posted on 2011-09-25
8
342 Views
Last Modified: 2012-05-12
I have a TreeView control on a UserForm which is populated from a 'map structure' in a database table.

I can make it possible to drag and drop between nodes.

Now I want to be able to 'read' the modified TreeView so that I can upload the map back to the database.

Does anybody know how to 'read' the TreeView?
ie. from the Root read each node and child nodes etc. loop through all.
te. Extract ParentID, Name, ThisItemID.

Thanks.
0
Comment
Question by:hindersaliva
  • 3
  • 3
8 Comments
 
LVL 33

Accepted Solution

by:
Norie earned 500 total points
ID: 36595332
Where do you want to 'read' it to?

eg an array, onto a worksheet, straight into the database

What you can do is use a recursive sub like this:
Public Sub TraverseTree(objNode As Node)
Dim objSiblingNode As Node
Dim strResults As String


    Set objSiblingNode = objNode

    Do
        strResults = strResults & objSiblingNode.Text & vbCrLf

        Debug.Print objSiblingNode.Text

        If Not objSiblingNode.Child Is Nothing Then
            Call TraverseTree(objSiblingNode.Child)
        End If

        Set objSiblingNode = objSiblingNode.Next

    Loop While Not objSiblingNode Is Nothing

End Sub

Open in new window

That's a very simple example that can be adapted for other purpose.

For example, I've used it in the past to populate an array.
0
 

Author Comment

by:hindersaliva
ID: 36595340
imnorie,
That's exactly what I was after. I shall load it into an array (and then have several selectable choices).

Thanks!
0
 
LVL 33

Expert Comment

by:Norie
ID: 36595347
No problem.

0
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

Author Comment

by:hindersaliva
ID: 36595575
Imnorie,
I'm struggling with how to start it off.
Do you have a code sample that calls the Sub? ie. with the initial value of objNode
For me it will be the Root.

(This is exciting)

Thanks
0
 
LVL 33

Expert Comment

by:Norie
ID: 36595590
Something like this perhaps.
Dim ptnode As Node

    Set ptnode = TreeView1.Nodes("Root")
    Call TraverseTree(ptnode)

Open in new window

This will only work if your 'root' node has the key value 'Root'.

If it has something different use that.
0
 

Author Comment

by:hindersaliva
ID: 36596267
imnorie
Yes that worked great.
Thanks
0
 
LVL 50

Expert Comment

by:Ingeborg Hawighorst
ID: 37412263
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question