Solved

Excel - TreeView Control - reading tree items

Posted on 2011-09-25
8
354 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

730 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