Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Treeview - collapse all nodes when open

Status
Not open for further replies.

UHNSTrust

Technical User
Dec 2, 2003
262
GB
I have a treeview control that I am loading with data. When opening the form it always expands the second node of the first level. I am confused as why it is doing this.

I want the form to open with all nodes collapsed and the first node having focus (but not expanded).

Can somebody help with where I am going wrong?

I have tried the following code which works on a button to collapse all the nodes but am unsure how to do this on opening the form (have tried the open and load events).
Code:
    Dim i As Integer
    'Purpose   Collapse all nodes in the Tree
    If tvwTest.Nodes.count > 0 Then
        
        For i = 1 To tvwTest.Nodes.count
            tvwTest.Nodes(i).Expanded = False
        Next
        tvwTest.Nodes(1).Selected = True
        
    End If

Thanks in advance.

Jonathan
 
How is this treeview populated ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
By using code from an example by Helen Feddema

Code:
Function tvwTest_Fill()
'Created by Helen Feddema 2-10-2002

'============================================================
'Modified from a procedure generated by the Access 97
'Treeview Control Wizard

'PURPOSE: Fill the ActiveX Treeview Control 'tvwTest' data

'CALLED FROM: Form Load event
'============================================================

On Error GoTo ErrorHandler

   Dim strMessage As String
   Dim dbs As DAO.Database
   Dim rst As DAO.Recordset
   Dim intVBMsg As Integer
   Dim strQuery1 As String, strQuery2 As String, strQuery3 As String, strQuery4 As String, strQuery5 As String
   Dim nod As Object
   Dim strNode1Text As String, strNode2Text As String, strNode3Text As String, strNode4Text As String, strNode5Text As String
   Dim strVisibleText As String
   
   Set dbs = CurrentDb()
   strQuery1 = "vwClusterNodes"
   strQuery2 = "vwTeamNodes"
   strQuery3 = "vwClinicianNodes"
   strQuery4 = "vwPatientNodes"
   strQuery5 = "vwAssessmentNodes"
   
   With Me![tvwTest]
      'Fill Level 1 - Clusters
      Set rst = dbs.OpenRecordset(strQuery1, dbOpenForwardOnly)

      Do Until rst.EOF
         strNode1Text = StrConv("Cluster¦" & rst![ClusterID] & "¦" & proper(rst![ClusterDesc]), vbLowerCase)
         strVisibleText = rst![ClusterDesc]
         Set nod = .Nodes.Add(Key:=strNode1Text, _
            Text:=strVisibleText, _
            Image:=5, _
            SelectedImage:=1)
         rst.MoveNext
      Loop
      rst.Close
      
      'Fill Level 2 - Teams
      Set rst = dbs.OpenRecordset(strQuery2, dbOpenForwardOnly)

      Do Until rst.EOF
         strNode1Text = StrConv("Cluster¦" & rst![ClusterID] & "¦" & proper(rst![ClusterDesc]), vbLowerCase)
         strNode2Text = StrConv("Team¦" & rst![TeamID] & "¦" & rst![TeamDesc], vbLowerCase)
         strVisibleText = rst![TeamDesc]
         .Nodes.Add relative:=strNode1Text, _
            relationship:=tvwChild, _
            Key:=strNode2Text, _
            Text:=strVisibleText, _
            Image:=6
         rst.MoveNext
      Loop
      rst.Close
      
      'Fill Level 3 - Clinicians
      Set rst = dbs.OpenRecordset(strQuery3, dbOpenForwardOnly)

      Do Until rst.EOF
         strNode2Text = StrConv("Team¦" & rst![TeamID] & "¦" & rst![TeamDesc], vbLowerCase)
         strNode3Text = StrConv("Clinician¦" & rst![ClinicianID] & "¦" & rst![Clinician] & "¦" & rst![ProfRoleDesc], vbLowerCase)
         strVisibleText = rst![Clinician] & " (" & rst![ProfRoleDesc] & ")"
         .Nodes.Add relative:=strNode2Text, _
            relationship:=tvwChild, _
            Key:=strNode3Text, _
            Text:=strVisibleText, _
            Image:=7
         rst.MoveNext
      Loop
      rst.Close
      
      'Fill Level 4 - Patients
      Set rst = dbs.OpenRecordset(strQuery4, dbOpenForwardOnly)

      Do Until rst.EOF
         strNode3Text = StrConv("Clinician¦" & rst![ClinicianID] & "¦" & rst![Clinician] & "¦" & rst![ProfRoleDesc], vbLowerCase)
         strNode4Text = StrConv("Patient¦" & rst![PatientID] & "¦" & rst![Patient] & "¦" & rst![NHSNo] & "¦" & rst![DOB], vbLowerCase)
         strVisibleText = rst![Patient] & " (" & rst![NHSNo] & " - " & rst![DOB] & ")"
         .Nodes.Add relative:=strNode3Text, _
            relationship:=tvwChild, _
            Key:=strNode4Text, _
            Text:=strVisibleText, _
            Image:=8
         rst.MoveNext
      Loop
      rst.Close
      
      'Fill Level 5 - Assessments
      Set rst = dbs.OpenRecordset(strQuery5, dbOpenForwardOnly)

      Do Until rst.EOF
         strNode4Text = StrConv("Patient¦" & rst![PatientID] & "¦" & rst![Patient] & "¦" & rst![NHSNo] & "¦" & rst![DOB], vbLowerCase)
         strNode5Text = StrConv("Assessment¦" & rst![AssessmentID] & "¦" & rst![AssessmentDate], vbLowerCase)
         strVisibleText = rst![AssessmentDate] & " (Score: " & rst![Score] & ")"
         .Nodes.Add relative:=strNode4Text, _
            relationship:=tvwChild, _
            Key:=strNode5Text, _
            Text:=strVisibleText, _
            Image:=2, _
            SelectedImage:=4
         rst.MoveNext
      Loop
      rst.Close
   
   End With

ErrorHandlerExit:
   dbs.Close
   Exit Function

ErrorHandler:
   Select Case err.Number
      Case 35601
         'Element not found
         strMessage = "Possible Causes: You selected a table/query" _
            & " for a child level which does not correspond to a value" _
            & " from its parent level."
         intVBMsg = MsgBox(Error$ & strMessage, vbOKOnly + _
            vbExclamation, "Run-time Error: " & err.Number)
      Case 35602
         'Key is not unique in collection
         strMessage = "Possible Causes: You selected a non-unique" _
            & " field to link levels."
         intVBMsg = MsgBox(Error$ & strMessage, vbOKOnly + _
            vbExclamation, "Run-time Error: " & err.Number)
      Case Else
         intVBMsg = MsgBox(Error$ & "@@", vbOKOnly + _
            vbExclamation, "Run-time Error: " & err.Number)
   End Select
   Resume ErrorHandlerExit

End Function

Jonathan
 
Have you tried to call your collapse routine just after the call to tvwTest_Fill and then set a breakpoint ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I run the tvwTest_Fill in the On_Load event of the form. If I try to use the collapse code in the same event it runs but seems to make no difference. If I do the collapse on the Activate event of the form it works but it then causes problems when opening other forms and reports from the first form because it collapses the treeview when it gets focus back.

Jonathan
 
Did you put a BreakPoint to be sure at which time the second node of the first level is expanded ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I don't know where to put the breakpoint. I only have the On_Open and On_Load events with code in.

On_Open hides a previous form and disables the cross for the Access Application.

The On_Load event maximizes the form, fills the treeview and then tries to collapse the nodes.

I have run a breakpoint whilst trying to collapse the nodes and it runs (loops). When the form shows it is always the second node of the first level.

Jonathan
 
I use these
Code:
Private Sub cmdExpandTree_Click()
On Error GoTo Err_cmdExpandTree_Click
    Dim myTree As TreeView
    Dim myNodes As Nodes
    Dim myNode As Node
    Set myTree = Me.xTree.Object
    Set myNodes = myTree.Nodes
    For Each myNode In myNodes
       myNode.Expanded = True
    Next myNode
Exit_cmdExpandTree_Click:
    Exit Sub
Err_cmdExpandTree_Click:
    MsgBox Err.Description
    Resume Exit_cmdExpandTree_Click
End Sub



Private Sub cmdCollapseTree_Click()
On Error GoTo Err_cmdCollapseTree_Click
    Dim myTree As TreeView
    Dim myNodes As Nodes
    Dim myNode As Node
    Set myTree = Me.xTree.Object
    Set myNodes = myTree.Nodes
    For Each myNode In myNodes
      myNode.Expanded = False
    Next myNode
Exit_cmdCollapseTree_Click:
    Exit Sub
Err_cmdCollapseTree_Click:
    MsgBox Err.Description
    Resume Exit_cmdCollapseTree_Click

However, your code looks like it should work. However, the node collection is zero based I believe so by selecting nodes(1) you are selecting the second node of the first level as you describe. I think you want nodes(0). If you select the node first, then collapse the tree I think you will be OK or you could select the node then collapse it.
tvwTest.Nodes(0).Selected = True
tvwTest.Nodes(0).Expanded = False


so the
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top