Using Treeview checkboxes to determine if a node is 'active'

Toadums

Registered User.
Local time
Yesterday, 22:29
Joined
Feb 22, 2009
Messages
53
I'm assuming that it will be something like

Code:
Private Sub TreeView_Click()
I have a treeview on the left side of my form and a textbox on the right. when a node's checkbox is...checked, I would like it to display the information relating to that cell (that part I can do, but only when the node is CLICKED) ... but how do I make it happen when a box is checked, and then ...the data be removed if the box is unclicked?

I'm assuming it will be something like:

Code:
Private Sub TreeView_Check()
I am just unaware of what the command is >.<

Thanks, hopefully this makes sense!

Paul
 
What i use is
Code:
Private Sub tvwHierarchy_Click()
    FillDetails
End Sub
While filling the treeview, i use the tag in each node to store specific information. So when a node is selected
Code:
strNodeInfo = Me.tvwHierarchy.SelectedItem.Tag
will give me more information on the specific node.

For more information please post a sample database.

HTH:D
 
Hm, I dont quite understand where you use the

Code:
strNodeInfo = Me.tvwHierarchy.SelectedItem.Tag
and what sort of a value it saves...does it just give a unique number to each node like a key?

Here is my database (dont mind the horribly messy, probably half useless code :P, ive only been using VB for like 4 days or so...so Im just learnin!)

Thanks!
Paul
 

Attachments

Hi,

I am trying to learn TreeViews etc. I downloaded you code to see if I could learn anything but unfortunately I've only got Access 2003 - would you be so kind as to let me see a 2003 version of what you're doing - maybe we could learn something from one another?

Regards

JohnAngus
 
Sorry John, but when I downgrade it, the form gets deleted, im guessing they implement a different version of treeView.

I will, however let ya see my code (works in 2007..:P)

So far all that I have done is:

adds nodes CORRECTLY, adds checkboxes, when a node is clicked the information in the data field is shown in the text box (for half the nodes only)

Also, tier, tier2, tier3 ... etc are all different fields.

Hopefully you have some knowledge of VB, cause I have learnt everything I know doing this...which has made it kind of difficult. so excuse any mistakes/not "grammatically correct" things, and there is prolly a lot more code than I need :P

Here is is:

Code:
'===========================================================================================
'@Description: this sub adds the first level of nodes. the 'roots'
'@Author: Paul Demchuk
'@Date: April 23, 2009

'@common variables: a) i is the variable used to keep track of current node's key numbers
'                   b) j is the variable used to obtain the parent's key
'                   c) rst is the recordset containing the data

'@common loops: The do loops loop through the recordset and if the data is
'               in the field which is being added, it will add it
'               and i will be keeping track of the parent's key
'               when an item is added to the tree, then the j value is incremented
'               so that as you go down the tree in each level, the values of
'               keys are increasing by 1
'============================================================================================

Sub addRoots()
 
 Dim rst As DAO.Recordset
  
  Dim i As Integer
  
  Set rst = CurrentDb.TableDefs!sampleSheet.OpenRecordset
  rst.MoveFirst
  
  i = 0
  
  Do Until rst.EOF
  
  
  If (rst!partNum <> Empty) Then
        TreeView.Nodes.Add Text:=rst!partNum, Key:="prt=" & i
        i = i + 1
    End If
    
    rst.MoveNext
    
  Loop

End Sub

'==============================================================================
'@Description: this sub adds the first level of children
'@Author: Paul Demchuk
'@Date: April 23, 2009
'==============================================================================

Sub addChildren()

Dim rst As DAO.Recordset
  
  Dim i As Integer
  Dim j As Integer
    
  Set rst = CurrentDb.TableDefs!sampleSheet.OpenRecordset

    i = -1
    j = 0
    
    
    rst.MoveFirst
    Do Until rst.EOF
    
    If (rst!tier <> Empty) Then '
            TreeView.Nodes.Add relationShip:=tvwChild, Relative:="prt=" & i, _
            Text:=rst!tier, Key:="t1=" & j
            j = j + 1
    ElseIf (rst!partNum <> Empty) Then
        i = i + 1
        End If
         
    rst.MoveNext
    
    Loop

End Sub

'==============================================================================
'@Description: this sub adds the next level of children
'@Author: Paul Demchuk
'@Date: April 23, 2009
'==============================================================================

Sub addChildren2()

Dim rst As DAO.Recordset
  
  Dim i As Integer
  Dim j As Integer
    
  Set rst = CurrentDb.TableDefs!sampleSheet.OpenRecordset

    i = -1
    j = 0
    rst.MoveFirst
    
    Do Until rst.EOF
    
    If (rst!tier2 <> Empty) Then
            TreeView.Nodes.Add relationShip:=tvwChild, Relative:="t1=" & i, _
            Text:=rst!tier2, Key:="t2=" & j
            j = j + 1
    ElseIf (rst!tier <> Empty) Then
        i = i + 1
        End If
        
    rst.MoveNext
    
    Loop

End Sub

'==============================================================================
'@Description: this sub adds the next level of children
'@Author: Paul Demchuk
'@Date: April 23, 2009
'==============================================================================

Sub addChildren3()

Dim rst As DAO.Recordset
  
  Dim i As Integer
  Dim j As Integer
    
  Set rst = CurrentDb.TableDefs!sampleSheet.OpenRecordset

    i = -1
    j = 0
    rst.MoveFirst
    
    Do Until rst.EOF
    
    If (rst!tier3 <> Empty) Then
            TreeView.Nodes.Add relationShip:=tvwChild, Relative:="t2=" & i, _
            Text:=rst!tier3, Key:="t3=" & j
            j = j + 1
    ElseIf (rst!tier2 <> Empty) Then
        i = i + 1
        End If
         
    rst.MoveNext
    
    Loop

End Sub

'==============================================================================
'@Description: this sub adds the next level of children
'@Author: Paul Demchuk
'@Date: April 23, 2009
'==============================================================================

Sub addChildren4()

Dim rst As DAO.Recordset
  
  Dim i As Integer
  Dim j As Integer
    
  Set rst = CurrentDb.TableDefs!sampleSheet.OpenRecordset

    i = -1
    j = 0
    rst.MoveFirst
    
    Do Until rst.EOF
    
    If (rst!tier4 <> Empty) Then
            TreeView.Nodes.Add relationShip:=tvwChild, Relative:="t3=" & i, _
            Text:=rst!tier4, Key:="t4=" & j
            j = j + 1
    ElseIf (rst!tier3 <> Empty) Then
        i = i + 1
        End If
         
    rst.MoveNext
    
    Loop
End Sub



'==============================================================================
'@Description: this sub calls all the other subs to populate the tree
'@Author: Paul Demchuk
'@Date: April 23, 2009
'==============================================================================

Private Sub Form_Open(Cancel As Integer)
  SetupTreeview
  
  addRoots
  addChildren
  addChildren2
  addChildren3
  addChildren4
  
End Sub

'==============================================================================
'@Description: this sub gives the tree all it's settings
'@Author: Paul Demchuk
'@Date: April 23, 2009
'==============================================================================

Private Sub SetupTreeview()
  With Me.TreeView
    .Style = tvwTreelinesPlusMinusText
    .LineStyle = tvwRootLines
    .Indentation = 240
    .Appearance = ccFlat
    .HideSelection = False
    .BorderStyle = ccFixedSingle
    .HotTracking = True
    .FullRowSelect = True
    .Checkboxes = True
    .SingleSel = False
    .Sorted = False
    .Scroll = True
    .LabelEdit = tvwManual
    .Font.Name = "Verdana"
    .Font.Size = 7
  End With
End Sub
'==============================================================================
'@Description: this sub makes wonderful things happen when nodes are clicked
'@Author: Paul Demchuk
'@Date: April 23, 2009
'==============================================================================
Private Sub TreeView_Click()

Dim rst As DAO.Recordset
Dim nodSelected As MSComctlLib.Node ' a variable for the currently selected node
Dim nodeText As String
Dim num As String
Dim counter As Integer
Dim check As Boolean
Dim display As String

    
    Set rst = CurrentDb.TableDefs!sampleSheet.OpenRecordset
    Set nodSelected = Me.TreeView.SelectedItem ' get the currently selected node
    check = False
            
        nodeText = findRoot(nodSelected).Text

        num = Mid(nodSelected.Key, 4)
        rst.MoveFirst
          
        If (nodSelected.Key Like "t1=*") Then
       
            If (num <> 0) Then
            
            Do Until rst.EOF Or check = True
                If (rst!tier <> 0) Then
                
                    counter = counter + 1
                    
                End If
             
                If (counter = num) Then
                    check = True
                    
                Else
            
            rst.MoveNext
            
            End If
            Loop
        Else
        
        Do Until rst!tier <> Empty
        rst.MoveNext
        Loop
        End If
        
        display = rst!Data
        
        
        ElseIf (nodSelected.Key Like "t2=*") Then
            
          If (num <> 0) Then
          
            Do Until rst.EOF Or check = True
                If (rst!tier2 <> 0) Then
                
                    counter = counter + 1
                    
                End If
            
                If (counter = num) Then
                    check = True
                    
                Else
            
            rst.MoveNext
            
            End If
            
            Loop
            
        Else
        
        Do Until rst!tier2 <> Empty
        rst.MoveNext
        Loop
        End I
        
        display = rst!Data
        
        End If
        
        Text = Text & display & vbCrLf
    
        End Sub

'==============================================================================
'@Description: this sub finds the ROOT node of any given node parameter
'@Author: Paul Demchuk
'@Date: April 23, 2009
'==============================================================================
Private Function findRoot(n As MSComctlLib.Node) As MSComctlLib.Node

Dim root As MSComctlLib.Node
Dim check As Boolean

    Set root = n
    check = True
        
        If (root.Key Like "prt=*" = False) Then
          
        While check = True
        
        Set root = root.parent
             
            If (root.Key Like "prt=*") Then
                     
                check = False
                
            End If
        
        Wend
        
        End If

    Set findRoot = root

End Function
Here is a link i found somewhat helpful, although I did find it easier to just read it and then make my own treeView from the start..

http://mymsaccessblog.blogspot.com/2008/02/my-treeview-project-episode-1-hello.html

Hope this helps!
Paul
 
Thanks Paul - I will have a study of your code and see if I can learn something - I attach herewith ( if I can get the attachment aspect of this site working) the MS Northwind Database in which I have included a a Tree View Form - I got the code off MS site and seems to work - I was hoping to bring beyond what I have here - but what I've done is good in my opinion.

Thanks

JohnAngus
 

Attachments

Last edited:
I still am wondering about this, no one got back to me on it :(

post #3 is where the most recent question is :)

Thanks
 
Hi Toadums,

Here's a extended copy of your sample database.
I have copied my Treeview form to it. Ofcourse it doesn't work in this environment but i hope that you can learn from it or give me pointers in how to improve the form.

Most of the comments are english and most of the messages to the users are dutch. If you can't figure it out, let me know.

I will help but it might take a while.

HTH:D
 

Attachments

hehe thanks, but you looked at the wrong post >.< !! :P the file you got isnt mine at all hehe..

I already know how to add everything to the treeview, its just making the treeview realize that once a CHECKBOX is checked, then a node is active (instead of saying Treeview_Click() for the sub name, i want it to be like (Treeview_ifCheck() or something >.<)
 

Users who are viewing this thread

Back
Top Bottom