Adding node to TreeView

Erick

Active Member
Joined
Feb 26, 2003
Messages
362
I have a simple form with an ActiveX TreeView Control (TreeView0) and a Command Button (Command1). I also have a table (Table1) with 2 fields (Id & Parent).

The code below should simply populate the treeview control. It is tripping up on the 'Set nodX = objTree.Nodes.Add(Parent, tvwChild, MyRS!Id)'. It comes up with 'Run-time error 35601: Element not found'.

I just can't seem to get the add node on this tree view to work.

Code:
Option Compare Database
Private Sub Command1_Click()
    Dim nodX As Node
    Dim objTree As TreeView
    Dim MyDB As DAO.Database
    Dim MyRS As DAO.Recordset
    Dim MyRSChild As DAO.Recordset
    Dim strSQL As String
    Dim Parent As String
    
    Set MyDB = CurrentDb()
    Set MyRS = MyDB.OpenRecordset("Table1", dbOpenDynaset)
    Set objTree = Me!TreeView0.Object
    objTree.Nodes.Add , , , "Hierarchy"
    Do While Not MyRS.EOF
        If IsNull(MyRS!Parent) Then Parent = "Hierarchy" Else Parent = MyRS!Parent
        Set nodX = objTree.Nodes.Add(Parent, tvwChild, MyRS!Id)
        nodX.EnsureVisible
      MyRS.MoveNext
    Loop
End Sub
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
NOTE: All KEYS must start with a character.

Does MyRS!Parent and MyRS!ID start with a character?
 
Upvote 0
Doesn't the parent have to be an existing node in the tree? ie you have to add all the parents as children of "Hierarchy" ...... before adding your ID's, if your parents form their own Hierarchy... well then its not as straightforward as your first thought!
 
Upvote 0
The treeview is complex. This is probably due to being a generic control.

I am still learning how to use the Treeview also. I learned a lot from this:

Access Treeview Example

The attachment in the above link has a code module with a lots of functions for working with a Treeview control.

Here are some other links I found helpful:

http://thesmileycoder.com/category/treeview/

How to fill a Treeview control recursively in Access 2000

Access 2000 How To's: Adding a Tree View Control and Filter — DatabaseJournal.com
 
Upvote 0
Thanks for the update. Good job getting it working.

Glad to hear that The Smiley Coder's video tutorial was helpful.

Curious, what version of Access are you using?
 
Upvote 0
Hi Erick

Glad to hear you found it usefull. And thanks to HiTechCoach for listing my site as a reference.

While there is only 3 videos on my site, I made 6 videos on the treeview in total. The last 3 are only on youtube, since I have never found the time to make matching blogposts for them.
 
Upvote 0

Forum statistics

Threads
1,214,565
Messages
6,120,254
Members
448,952
Latest member
kjurney

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top