TreeView

Magoo
NewLounger
Posts: 9
Joined: 17 May 2013, 21:53

TreeView

Post by Magoo »

I'm currently in the process of writing a "fuel" program in VBA for my company using Excel & Access 2003 (XP Pro).

I came accross a "TreeView" routine written by Hans Vogelaar. I would like to use this to display a listing of equipment by classification for selection purposes.

My question is; is there a way to either double-click on an "extended box" or highlight an "extended box" then click on a "selection command button" and capture the selection in the TreeView to pass back to the program for further processing?

P.S. I noticed that the "extended boxes" have "tags", not sure if I can utilize or not.

User avatar
HansV
Administrator
Posts: 78549
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: TreeView

Post by HansV »

Welcome to Eileen's Lounge!

The currently selected node in a TreeView control is stored in its SelectedItem property.
The nodes themselves don't have a double-click event, but the treeview control as a whole has a double-click event. You can inspect the SelectedItem property in this event.
For example, for a treeview named TreeView1:

Code: Select all

Private Sub TreeView1_DblClick()
    With Me.TreeView1
        If .SelectedItem Is Nothing Then
            MsgBox "No node selected", vbInformation
        Else
            ' Do something with the selected item, for example
            MsgBox .SelectedItem.Text, vbInformation
        End If
    End With
End Sub
You could also look at the SelectedItem property in the click event of a command button.
Best wishes,
Hans

Magoo
NewLounger
Posts: 9
Joined: 17 May 2013, 21:53

Re: TreeView

Post by Magoo »

Keep getting message "Object does not support proprty or method"?

User avatar
HansV
Administrator
Posts: 78549
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: TreeView

Post by HansV »

Which version of the TreeView control did you insert?
S0100.png
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

Magoo
NewLounger
Posts: 9
Joined: 17 May 2013, 21:53

Re: TreeView

Post by Magoo »

I do not have "Microsoft TreeView Control" listed?
I am using Excel 2003 VBA to pull from Access tables to populate Tree
I should mention that I modified the code to utilize ADO vs. DAO (not sure if this is what's causing the problem)

User avatar
HansV
Administrator
Posts: 78549
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: TreeView

Post by HansV »

How did you get the TreeView control then?
I don't think DAO or ADO should matter.
Best wishes,
Hans

Magoo
NewLounger
Posts: 9
Joined: 17 May 2013, 21:53

Re: TreeView

Post by Magoo »

I believe this is one that you created for use on Excel VBA forms.
The code came with Class Modules clsNode and clsTreeView.

User avatar
HansV
Administrator
Posts: 78549
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: TreeView

Post by HansV »

Ah - are you referring to the treeview created by Jan Karel Pieterse with Peter Thornton, mentioned in All-VBA treeview?
Best wishes,
Hans

Magoo
NewLounger
Posts: 9
Joined: 17 May 2013, 21:53

Re: TreeView

Post by Magoo »

Yes, that is the one

User avatar
HansV
Administrator
Posts: 78549
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: TreeView

Post by HansV »

Thanks - I thought you were using the ActiveX treeview.

The treeview developed by Pieterse and Thornton has a Click event that passes the node clicked in its event procedure:

Code: Select all

Event Click(cNode As clsNode)
This is used in the demo userform to display information about the node in the caption of a label below the treeview:

Code: Select all

'This gets fired when a node is clicked
Private Sub mcTree_Click(cNode As clsNode)
    Dim s As String
    With cNode
        s = .Caption & IIf(mcTree.CheckBoxes, "   Checked:" & .Checked, "") & vbCr & _
            .Key & "     Index:" & .Index & "  VisIndex:" & .VisIndex & "   Level:" & .Level

    End With
    Me.labInfo.Caption = s
End Sub
No DoubleClick event has been defined, as far as I can tell.

The treeview has a property ActiveNode (of type clsNode) that represents the selected (active) node. For example you could create a command button cmdTest on the userform, with the following Click event procedure:

Code: Select all

Private Sub cmdTest_Click()
    ' Do something with the active node, e.g. display its caption
    MsgBox mcTree.ActiveNode.Caption, vbInformation
End Sub
where mcTree is the name of the treeview (I used the name from the demo form here).
Best wishes,
Hans

Magoo
NewLounger
Posts: 9
Joined: 17 May 2013, 21:53

Re: TreeView

Post by Magoo »

I can't thank you enough, we are making progress.
ActiveNode works, just need to explore other properties of ActiveNode's.
Going to try Event Click next.
All of our fixed assets are assigned a fixed asset ID # (FAID).
My intent is to incorporate this into the Node somehow, then retrieve it when the Node is selected.
Any ideas.
Thanks

Magoo
NewLounger
Posts: 9
Joined: 17 May 2013, 21:53

Re: TreeView

Post by Magoo »

I think I have it.
I assigned the FAID to the Key property.
I can retrieve the Key base on Node selected.
Thanks again for all your help.

User avatar
HansV
Administrator
Posts: 78549
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: TreeView

Post by HansV »

That sounds like a good plan!
Best wishes,
Hans

Magoo
NewLounger
Posts: 9
Joined: 17 May 2013, 21:53

Re: TreeView

Post by Magoo »

Hans, I have one last question before I wrap this up.
Is there a way to collapse the tree prior to viewing?
The reason I ask is;
1) This will make the tree fit into the window (nice presentation)
2) My managers will not have to look through an expanded tree to find their equipment. If collapsed they can go directly to the appropriate node, expand, then select their equipment.
Thanks again

User avatar
HansV
Administrator
Posts: 78549
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: TreeView

Post by HansV »

In the code where the tree is populated, there is a line

Code: Select all

        .PopulateTree
Below this, insert the lines

Code: Select all

        .ExpandToLevel 0
        .Refresh
ExpandToLevel 0 will display the treeview completely collapsed, i.e. only the root node will be visible.
ExpandToLevel 1 will display the treeview collapsed to level 1, i.e. the root node and its immediate descendants will be visible.
Etc.
Best wishes,
Hans

Magoo
NewLounger
Posts: 9
Joined: 17 May 2013, 21:53

Re: TreeView

Post by Magoo »

Level 2 is the ticket.
Since all the equipment is at Level 3, I also check to make sure that Node.Level = 3
If Node.Level=3, capture the Key (FAID) and complete the form for that FAID.
Works exactly the way I was hoping it would.
Back to wrting the rest of the code.
Thanks again Hans for all your help.

User avatar
Jan Karel Pieterse
Microsoft MVP
Posts: 656
Joined: 24 Jan 2010, 17:51
Status: Microsoft MVP
Location: Weert, The Netherlands

Re: TreeView

Post by Jan Karel Pieterse »

Thanks for helping Magoo implement my treeview Hans!
Regards,

Jan Karel Pieterse
Excel MVP jkp-ads.com

User avatar
HansV
Administrator
Posts: 78549
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: TreeView

Post by HansV »

You're welcome, Jan Karel! Fortunately, it's well-documented.
Best wishes,
Hans