Adding node to TreeView

Erick

Active Member
Joined
Feb 26, 2003
Messages
360
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
 

Some videos you may like

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.

HiTechCoach

Well-known Member
Joined
Aug 29, 2010
Messages
1,008
NOTE: All KEYS must start with a character.

Does MyRS!Parent and MyRS!ID start with a character?
 

CharlesChuckieCharles

Well-known Member
Joined
May 10, 2011
Messages
2,153
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!
 

HiTechCoach

Well-known Member
Joined
Aug 29, 2010
Messages
1,008
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
 

HiTechCoach

Well-known Member
Joined
Aug 29, 2010
Messages
1,008
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?
 

TheSmileyCoder

New Member
Joined
Dec 12, 2013
Messages
1
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.
 

Erick

Active Member
Joined
Feb 26, 2003
Messages
360
Thanks TheSmileyCoder for the YouTube video. It was very helpful. Great work.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,632
Messages
5,597,287
Members
414,134
Latest member
Tiyas44

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
Top