Solved

Excel - TreeView Control - reading tree items

Posted on 2011-09-25
8
361 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 34

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 34

Expert Comment

by:Norie
ID: 36595347
No problem.

0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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 34

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
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

695 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