Most Valuable Professional


View Jan Karel Pieterse's profile on LinkedIn subscribe to rss feed
Subscribe in a reader

Audit !!!

Check out our RefTreeAnalyser
the ultimate Excel formula auditing tool.

Speed up your file

FastExcel
The best tool to optimise your Excel model!
Home > English site > Articles > Treeview control

An MSForms (all VBA) treeview

Introduction

If you have ever used the Treeview control from the "Additional controls" section, then you know what a versatile control this is to show hierarchically organized data. There are a couple of problems with this Treeview control:

  1. Compile errors due to a difference in how the control libraries are registered in 32 bits Windows' System32 and 64 bit Windows' SysWOW32 folders. If you distribute a file that was saved in 64 bit Windows, containing one of the "Microsoft Windows Common Controls 6.0" (The Treeview control is one of them) and with the reference set to "mscomctl.ocx", people using 32 bit Windows will almost certainly have problems. At best it could entail removing both the control and the reference and replacing both, but at worst the user's Excel can crash when trying to load the file and run the app.
  2. The standard Treeview control, like all non built-in ActiveX controls, cannot be used in 64 bit versions of Office.

Especially the second point convinced me it is time to develop a custom-made Treeview "control", that only uses the native Office forms controls. I started building this a couple of weeks ago and after some time I tricked Peter Thornton into helping me with it

The screenshot below shows both our new Treeview (left) and the Windows one (right) side-by-side in their simplest display mode (read on, there are even prettier screenshots further down the page):

Treeview controls
Two treeviews, left: VBA tree, right: Common controls tree

In the following pages I'll show what our treeview can do and explain how to put it to use in your own VBA project.

MAC Office and 64 bit Office Compatible!

Our treeview was tested (and works) on 64 bit Office. It also works on MAC office. Tested applications include:

Excel 2000
Excel 2003
Excel 2010
Excel 2010 (64 bit)
Excel 2011 (MAC Office)
Excel 2013 (32 bit)

Access 2003
Access 2010 (32 bit)
Access 2010 (64 bit)

Windows screenshot:

Treeview demo on Windows Excel
Treeview demo on Windows Excel

Mac screenshot:

MAC screenshot of our treeview demo form
Screenshot of treeview on Mac Excel 2011

Acknowledgements

The basic plumbing and code structure of this treeview control was devised by me. However, without the help of my friend and fellow MVP Peter Thornton, lots of functionality would not have been available now. For that I sincerely thank Peter!

Furthermore, Access MVP Ben Clothier was kind enough to make the necessary adjustments to incorporate the treeview in an Access form

Also: Fellow Excel MVP Ron De Bruin ensured the treeview also works on MAC Office 2011, Thanks Ron!

Copyright and Licensing

All code in the treeview is (c) JKP Application Development Services and Peter Thornton (the Authors). It remains our sole intellectual property.

However, we're offering this treeview to you at no cost. You get an unrestricted license for use in any VBA project you like. You're free to modify any part of the code at will.

We do have some rules:

We're always interested to see how people have implemented the VBA Treeview. So please feel free to send a screenshot with a brief description or relevant details.

Disclaimer

You use this control at your own risk: The authors accept no liability whatsoever for any damages which may arise due to the use of our treeview.

Donations

Many, many hours were spent developing this treeview. Although we developed it for use in our own projects, we are giving it away for free!

Nevertheless, we would really be pleased if you actually express your appreciation in a more "tangible" form. So here is a paypal donation button at your disposal:

Download

The Excel workbook contains most of the documentation (on the tabs of the workbook), so I recommend you to at least download the Excel version. The Access version has instructions on its main form on how to implement the treeview in your own projects.

Download the treeview sample Excel workbook (including documentation) (build 025, 16 Oct 2013, downloaded 9744 times)

Download the treeview sample Word document (build 025, 16 Oct 2013, downloaded 2535 times)

Download the treeview sample Access database (build 025, 16 Oct 2013, downloaded 8798 times)

Content

 

Other controls

Another often used control is the calendar control. This control has the added problem that it has been deprecated with Office 2010 (where we're supposed to use the date picker control). Frankens Team created an all-vba alternative using very similar techniques to what we've done here.

Ron de Bruin created a Date Picker control for MAC Excel.


Comments

Showing last 8 comments of 283 in total (Show All Comments):

 


Comment by: Barry C (10/17/2014 12:30:46 AM)

First of all - Amazing piece of work!

I'm in the middle of working this into a project and I'm loving it.

But... I'm having the same issue as Davide (a couple of months back).

Using Access (tried it in versions 2k, 2k3 and 2010) I can not add a new image or change an existing image in the image frame.

If I select an existing image or add a new one, the "Picture" property in the property sheet is read only.
Other properties (name, size etc) are fine.

Works fine in Excel (all versions), but not in Access.
Any ideas?

In the meantime, I'll try a work around - do that work in Excel, then export it to Access.

Thanks
BC

 


Comment by: Peter Thornton (10/19/2014 6:22:47 PM)

Hi Barry,
Strange, I can't load a new picture to an image control in MSForm frame container either, thought I could!

OK, rather than "export" you can add the image controls to a form in Excel (or Word) and simply copy the control with it's picture to the Frame container in Access (can copy/paste multiple image controls at the same time). The Frame in the demo was added as an ActiveX "Microsoft Forms 2.0 Frame".

Note you can also pass a Collection of picture 'handles' as an alternative to passing the Frame control if more convenient to store your images elsewhere.

You say you've tried it in Access 2000 - interesting, does the demo actually work for you? In light testing although in 2k it all compiles OK trying to load in resulted in a bunch of "Object of class does not support the set of events" errors, no idea why!

Untested in 2002. 2003 should be fine but it seems only if compiled in 2003 (may get similar error in 2003 if compiled in 2007 or later), again no idea why. (For anyone else reading this concerns Access only.)

 


Comment by: Dale Fye (10/21/2014 2:09:37 PM)

Have been playing with the treeview for several days now. Was able to add code on the MouseUp event to display a popup menu, that worked great. One of the items on my popup menu is to expand or collapse the currently selected node and all of its children.

The default, when you collapse the root node appears to collapse that node, but to leave all other expanded nodes expanded. Since my tree can 4 or 5 levels deep, I want to be able to right click on the node and select Expand All or Collapse all. Right now, my Collapse code looks similar to:

set nd = forms("frmMain).mctree.activenode
IF CommandBars.ActionControl.Parameter = "Collapse" Then
    CollapseBranch nd
Else
    ExpandBranch nd
endif

Sub CollapseBranch(cNode As clsNode)

    Dim cChild As clsNode
    
    cNode.Expanded = False
    If Not cNode.ChildNodes Is Nothing Then
        For Each cChild In cNode.ChildNodes
            CollapseBranch cChild
        Next
    End If
    
End Sub
Sub ExpandBranch(cNode As clsNode)

    Dim cChild As clsNode
    
    cNode.Expanded = True
    If Not cNode.ChildNodes Is Nothing Then
        For Each cChild In cNode.ChildNodes
            ExpandBranch cChild
        Next
    End If
    
End Sub


But all this is doing is changing the icon in front of the node from - to + (or + to -), it does not actually collapse or expand the node.

 


Comment by: Dale Fye (10/21/2014 2:51:30 PM)

Is there a way to implement the MouseUp event in the main form? I notice in the frmDemo that there are Click, KeyDown, and NodeCheck events listed, but don't see the MouseUp event.

I've added code to the mctlControl_MouseUp subroutine in clsNode, but would like to leave that code unchanged and simply be able to refer to the mcTree_MouseUp event in my main forms code module.

Is there something special that I need to do to make that event visible in the main form?

 


Comment by: Jan Karel Pieterse (10/21/2014 3:30:58 PM)

Hi Dale,

The easiest way is to call the mcTree.NodeClick method with the Node's Expander control as the first argument. Like so:

        nd.Expanded = Not nd.Expanded
        mcTree.NodeClick nd.Expander, nd

 


Comment by: Dale Fye (10/21/2014 4:10:34 PM)

That seems to have done it. Thanks.

 


Comment by: Dale Fye (10/22/2014 4:05:38 PM)

Guys,

I'm trying to add some images to the image frImages in Access 2007. I was able to drill down to the image control on the form, and then select properties, but am unable to change the actual image.

I noticed a thread above that you cannot currently edit the images in Access, but I'm the first to admit that I'm an idiot when it comes to working with Excel UserForms.

I added a user form to an Excel file, added an image control to it, but cannot figure out how to actually assign an image (bmp, png, ico) to that image control.

 


Comment by: Jan Karel Pieterse (10/23/2014 7:07:56 AM)

Hi Dale,

There are various ways to add an image to an image control on a VBA Userform:

- At runtime you can use the LoadPicture method
- At design time:
Copy the picture and paste it into the picture property of the image control
Click the three-dotted button next to the image property and select an image file

 


Have a question, comment or suggestion? Then please use this form.

If your question is not directly related to this web page, but rather a more general "How do I do this" Excel question, then I advise you to ask your question here: www.eileenslounge.com.

Please enter your name (required):

Your e-mail address (optional but if you want me to respond it helps!; will not be shown, nor be used to send you unsolicited information):

Your request or comment:

To post VBA code in your comment, use [VB] tags, like this: [VB]Code goes here[/VB].