Hi Rob,
Many thanks for your informative suggestions.
I have tried setting the Expanded property to False for each node when
they are created. This had no effect. When the treeview was
completely populated I tried looping through each node and setting the
Expanded property to False for each top level node. Again, this had no
effect. Finally I tried to collapse the first node directly
(tv.Nodes(1).Expanded = False) but it still, sadly, appears expanded.
The highlighted child node is not necessarily the same child each time
the form is opened. (Note that there is only one instance of the form
- there are not several instances existing at the same time.)
My treeview displays countries, cities & the offices in each city. For
example, the first time I open the form I get:
-Afghanistan
+Bamyan
+Herat
-Jalalabad <--- This node is highlighted (but
its selected property is false)
Sub-Office
+Kabul
+Kandahar
+Zaranj
+Albania
+Algeria
The next time I open the form I might get:
-Afghanistan <--- This node is highlighted (but its
selected property is false)
+Bamyan
+Herat
+Jalalabad
+Kabul
+Kandahar
+Zaranj
+Albania
+Algeria
Another time I get:
-Afghanistan
+Bamyan
+Herat
+Jalalabad
-Kabul <--- This node is highlighted (but its
selected property is false)
Branch Office
+Kandahar
+Zaranj
+Albania
+Algeria
In conclusion, the first node (representing Afghanistan) always appears
expanded and more often than not a child (city-level) node is
highlighted. I have never seen an office highlighted.
I have included my code (without the error trapping) that controls the
population of the treeview and that adds the top level (country) nodes.
The data is a table of offices, some of which are now closed, hence
the filter to include/exclude inactive offices.
Hopefully you might find the cause of this eratic behaviour.
Thanks,
Andy
Private Sub PopulateTreeView(ByVal blnHideInactiveOffices As Boolean)
'
'This procedure populates the TreeView control with countries, cities
and offices. It is called from the Form_Load event (after the image
list is populated).
'
Dim rs As ADODB.Recordset
Dim tv As MSComctlLib.TreeView
Dim strCountryName As String
Dim strCountryCode As String
Dim strCountryPK As String
Dim strCityName As String
Dim strCityPK As String
Dim strOfficePK As String
Dim strOfficeType As String
Dim strEvent As String
'
On Error Resume Next
'
'Ensure that the currently selected node is not stored in the global
node variable.
Set gChosenNode = Nothing
'
'Reference the treeview control and ensure it is cleared.
Set tv = Me.tvCCO.Object
tv.Nodes.Clear
If Err.Number <> 0 Then
End If
'
'Set the filter to exclude/include inactive offices.
If blnHideInactiveOffices = True Then
Me.ServerFilter = "Office_Status = 'Active'"
Else
Me.ServerFilter = ""
End If
'
'Refresh the form to implement the change in filter.
Me.Refresh
'
'Create a recordset object for the data.
Set rs = Me.Recordset
If Err.Number <> 0 Then
End If
'
'Ensure that at least one record exists in the data.
If (rs.BOF = True) And (rs.EOF = True) Then
End If
'
'Loop through all the data.
Do While rs.EOF <> True
'
'Read the office data from the record.
strCountryCode =
LCase(Trim(rs.Fields("ISO_Two_Letter_Code").Value))
strCountryName = Trim(rs.Fields("Country_Name").Value)
strCountryPK = Trim(rs.Fields("Country_PK").Value)
strCityName = Trim(rs.Fields("City_Name").Value)
strCityPK = Trim(rs.Fields("City_PK").Value)
strOfficePK = Trim(rs.Fields("Office_PK").Value)
strOfficeType = Trim(rs.Fields("Office_Type").Value)
'
' Set a node for the country (Create it if necessary).
If blnAddCountryNode(strCountryCode, strCountryName, strCountryPK,
strCityName, tv) = False Then GoTo Cleanup
'
' Set a node for the city (Create it if necessary).
If blnAddCityNode(strCountryName, strCountryPK, strCityName,
strCityPK, tv) = False Then GoTo Cleanup
'
' Set a node for the office (Create it if necessary).
If blnAddOfficeNode(strCountryName, strCityName, strCityPK,
strOfficeType, strOfficePK, tv) = False Then GoTo Cleanup
'
If Err.Number <> 0 Then
End If
'
rs.MoveNext
Loop
'
Cleanup:
rs.Close
Set rs = Nothing
Set tv = Nothing
'
End Sub
============================================================
Private Function blnAddCountryNode(ByVal strCountryCode As String, _
ByVal strCountryName As String, _
ByVal strCountryPK As String, _
ByVal strCityName As String, _
ByRef tv As MSComctlLib.TreeView) As
Boolean
'
'This procedure returns a treeview node corresponding to the received
country.
'If the node does not already exist it is created.
'
Dim nodTest As MSComctlLib.Node
Dim strEvent As String
'
On Error Resume Next
'
'Assume the worst: The node representing a country could not be
created.
blnAddCountryNode = False
'
'Does a node representing the country already exist?
If blnNodeExists(tv.Nodes, strCountryPK) = True Then
'The node has effectively been created since it already exists.
blnAddCountryNode = True
Exit Function
End If
'
'Add a new node to represent the country.
Set nodTest = tv.Nodes.Add(, , Key:=strCountryPK,
Text:=strCountryName, Image:=strCountryCode)
nodTest.Tag = cstrCountry
nodTest.Expanded = False
Select Case Err.Number
Case 0
blnAddCountryNode = True
Case 35601 ' (Element not found) The flag image file does not
exist.
' Add a node with an alternative flag.
Case Else
End Select
'
Set nodTest = Nothing
'
End Function
============================================================
Rob said:
Hi Andy,
When you are populating the treeview, you can set the expanded state of
each
node after it is added, in this fashion:
...
Set MyNode = MyTreeViewControlName.Nodes.Add(....)
MyNode.expanded = True 'or false
...
You can make the expanded state dependent on level; eg. to show only
top-level nodes (which have no entry in the Relationship parameter), you
could use something like:
MyNode.expanded = isnull(MyRelationVariable)
The exact coding you'll need will depend on how you're populating your
treeview.
I'm not sure why you are getting a child node highlighted. However, you
can
force a particular node to be selected by:
MyTreeViewControlName.Nodes("KeyValue").Selected = True
You can also refer to a node by index, so:
MyTreeViewControlName.Nodes(1).Selected = True
will force the first node to be selected. [Note: the nodes index values
start at 1, rather than 0]
HTH,
Rob
Hi,
I have a Tree View (ActiveX control version 6.0 SP6) control on a form.
(Environment: MS Access Project (2002, SP3).
During the Form_Load event the treeview is populated successfully.
What bothers me is that when the form is displayed, the first node in
the tree view is expanded and a child node is highlighted (though not
actually selected).
Does anyone know a way of stopping this?
I would like to see just a list of the top level parent nodes, with no
node highlighted.
Many thanks in advance,
Andy