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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Selecting a node in Treeview by Code

Status
Not open for further replies.

popper

Programmer
Dec 19, 2002
103
AU
I am trying to get an Access VBA to select a given node (usually the root) in a treeview control on entry to the form.

Can anyone help. With thanks

 
Here's a sample of how to load the treeview and select/expand the root node:
Code:
Private Sub Form_Load()
  Dim pnodRoot As Node
  Dim pnod As Node
  Dim nodChild As Node
  Dim lngCtr As Long
  Dim tvw As TreeView
  
  Set tvw = TreeView1.Object
  
    With tvw
      .Nodes.Clear
      
      Set pnodRoot = .Nodes.Add(, , "Root", "Sample", "Closed", "Open")
      
      For lngCtr = 1 To 10
        Set pnod = .Nodes.Add(pnodRoot, tvwChild)
        With pnod
          .Text = "Hello World " & lngCtr
          .Tag = "BaseNode" & lngCtr
          .Image = "Closed"
          .ExpandedImage = "Open"
          Set nodChild = tvwTabbed.Nodes.Add(pnod, tvwChild)
          With nodChild
            .Text = "Hello Child " & lngCtr
            .Tag = "ChildNode" & lngCtr
            .Image = "Closed"
            .ExpandedImage = "Open"
            Set nodChild = tvwTabbed.Nodes.Add(pnod, tvwChild)
          With nodChild
            .Text = "Hello Child " & lngCtr
            .Tag = "ChildNode" & lngCtr
            .Image = "Closed"
            .ExpandedImage = "Open"
          End With
          End With
        End With
      Next lngCtr
      
      With pnodRoot
        .Selected = True    [green]'<--- select[/green]
        .Expanded = True
      End With
      
    End With
End Sub
The selected node may not appear selected until the control gets the focus, so the treeview has to be first in the tab order for the selection to appear at form load.

VBSlammer
redinvader3walking.gif

[sleeping]Unemployed in Houston, Texas
 
VBSlammer

I just wanted to thank you for the usefull post. I was struggeling with trying the set the "selected" property of the TreeView to a node value. I modified the code to...

With TreeView.Nodes(strKey)
.selected = true
.expanded = true
End With

so I can reference any node and it works like a charm!

Thanks,

sabloomer


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top