Helping you survive the dirty world of data analysis

 

Training Videos
Excel Page
Access Page
My Freeware

 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
Want an explorer style TreeView in your  application like this one?

 

Step 1. 

In a new form, open the toolbox and select the "More Tools" Option.  This will open a list of Active X Controls that are available to you.   From this list, Select the latest version of Microsoft TreeView Control you see.  In most cases that will be 6.0

 

Step 2.

Now that you have the TreeView on your form, you need to fill it with something.

 

Let's say we have a table that is basically a geographic hierarchy of our locations.  We want to show our Regions and Markets in our TreeView.  This table holds all the data we need to populate our TreeView.

 

Let's take it one step at a time and fill in TreeView with the Regions for now.  To do this, open the Form Properties and put the following code in the On Load Event 

  

'****************************************************************
'Make sure you add the 'Microsoft DAO 3.6 Library' reference and
'the 'Microsoft Windows Common Controls' reference before running.

We named our treeview TreeView0
'****************************************************************


Dim mydb As DAO.Database
Dim myset As DAO.Recordset, sqltext

'Declare your Node Levels one by one
Dim tempnode As node
Dim Regionnode As node

'Create a container for your index identifiers
Dim intindex As Integer

'define your SQL Statement to fill in the Region Level
sqltext = "SELECT Branchlist.Region FROM Branchlist GROUP BY Branchlist.Region ORDER BY Branchlist.Region;"

Set mydb = CurrentDb
Set myset = CurrentDb.OpenRecordset(sqltext)

'Add your top most node and name it
Set tempnode = Forms!Form1.TreeView0.Nodes.Add()
tempnode.Text = "ALL"

'The .Tag property will help us identify which level of the TreeView were are on.
tempnode.Tag = "All"

'Now add Regions to the top most node until all Regions have been added
Do Until myset.EOF
     Set Regionnode = Forms!Form1.TreeView0.Nodes.Add(1,   vwChild)
     Regionnode.Text = myset!REGION
     Regionnode.Tag = "Region"
     'Make sure you index each Region for later use
     intindex = Regionnode.Index

     myset.MoveNext
Loop

'Add this code to ensure that your treeview opens expanded to regions.
With tempnode
     .Expanded = True
     .Bold = True
     .Selected = False
End With

'Clean up
Set mydb = Nothing
Set myset = Nothing
Set tempnode = Nothing
Set Regionnode = Nothing

End Sub

Here is what we get

Step 3.

Now Lets fill in the Markets

Let's go back to the On Load event of our form and add another level to our code

Dim mydb As DAO.Database
Dim myset As DAO.Recordset, sqltext
Dim myset2 As DAO.Recordset, sqltext2
Dim tempnode As node
Dim Regionnode As node

'declared a Market Node
Dim Marketnode As node
Dim intindex As Integer
'declared a container for the Market index
Dim intindex2 As Integer

sqltext = "SELECT [Branchlist].[Region] FROM [Branchlist] GROUP BY [Branchlist].[Region] ORDER BY [Branchlist].[Region];"

'Added an SQL Statement to bring in Market info.  NOTE:  we include the Region to used later to establish a relationship
sqltext2 = "SELECT [Branchlist].[Region], [Branchlist].[Market] FROM [Branchlist] GROUP BY [Branchlist].[Region], [Branchlist].[Market] "  & _ "ORDER BY [Branchlist].[Market];"

Set mydb = CurrentDb
Set myset = CurrentDb.OpenRecordset(sqltext)
Set myset2 = CurrentDb.OpenRecordset(sqltext2)

Set tempnode = Forms!form1.TreeView0.Nodes.Add()

tempnode.Text = "ALL"
tempnode.Tag = "All"
 

'First Loop through the Regions adding them one by one
Do Until myset.EOF
   Set Regionnode = Forms!form1.TreeView0.Nodes.Add(1, tvwChild)
   Regionnode.Text = myset!REGION
   Regionnode.Tag = "Region"
   intindex = Regionnode.Index
 

'Now Loop through the Markets and check to see if the Region attached to the market belongs to the Region you just added to the TreeView
      Do Until myset2.EOF
         If myset2!REGION = myset!REGION Then
         Set Marketnode = Forms!form1.TreeView0.Nodes.Add(intindex, tvwChild)
         Marketnode.Text = myset2!MARKET
         Marketnode.Tag = "Market"
         intindex2 = Marketnode.Index
         End If
         myset2.MoveNext
         Loop
         myset2.MoveFirst


myset.MoveNext
Loop

With tempnode
.Expanded = True
.Bold = True
.Selected = False
End With

Set mydb = Nothing
Set myset = Nothing
Set myset2 = Nothing
Set tempnode = Nothing
Set Regionnode = Nothing
Set Marketnode = Nothing

  

As you can see we can now drill down to a Market. Obviously you can add as many layers as you need.

Step 4.

Making the TreeView do something simple

Well now we have a TreeView, but how do we use it in our applications. Let's look one simple example.

Let's place a textbox named 'txtselected' on our form and add a button (name it Get Selected).  In the On Click event of 'Get Selected' type the following code: Me.txtselected.Value = Me.TreeView0.SelectedItem

Now Click on Buffalo and press the Button.

 

 

Step 5.

Let's Get Fancy

Now Let's place two textboxes on our form.  1 named 'txtregion'  and 1 named 'txtmarket'. Let's also add a button (name it Get Selected).  In the On Click event of 'Get Selected' type the following code:

'This is where those tags come in handy

Select Case Me.TreeView0.SelectedItem.Tag
Case Is = "Region"
Me.txtregion.Value = Me.TreeView0.SelectedItem
Me.txtmarket.Value = ""


Case Is = "Market"
Me.txtmarket.Value = Me.TreeView0.SelectedItem
Me.txtregion.Value = Me.TreeView0.SelectedItem.Parent

End Select
 

Now if you select just  "South" and click the get selected button:

If you select West and Denver you will see:

Although this is just a basic look at TreeViews, this lesson should help you get started adding both functionality and a professional look to your applications with TreeViews.

 
 
© 2004 DataPig Technologies, LLC. All rights reserved.