VBA to Populate Excel Userform Treeview from MS SQL

bhalbach

Board Regular
Joined
Mar 15, 2018
Messages
221
Office Version
  1. 2016
Platform
  1. Windows
I can not seem to figure this out, can someone assist please.

I have a userfrom in excel that has a treeview, I am trying to populate the treeview with a recordset from ms sql using ADO.

I have managed to populate the Parent Nodes but have not been able to figure out how to populate the child nodes. Everything with the connection to the database and getting a recordset returned seems to be working as I do get the parent nodes to populate.

I am a rookie in vba and have been able to eventually figure things out with google and youtube, but I am stuck on this one...cant find much online related to what I am trying to do.

I have tried the following code and I get a error " Error number = 2147257114 Invalid object"

This is the first time I have tried using treeview.

All assistance would be greatly appreciated.

VBA Code:
    Me.TreeView1.Nodes.Clear
    
    'load Treeview
    Do While Not rs.EOF
    
        Me.TreeView1.Nodes.Add = rs.Fields.Item("BidItemNo")
        Me.TreeView1.Nodes.Add rs.Fields.Item("BidItemNo"), tvwChild, rs.Fields.Item("BidItemDescription"), rs.Fields.Item("BidItemDescription")

        
    rs.MoveNext
    Loop
 
I think so.

When the code error happens, what are the values in tagArray, and what are you expecting them to be?
I’m away from my computer now. I’ll check later.

I’m expecting to have a list of the recordset values separated by a comma. Then use the tagArray to extract which value I want.
 
Upvote 0

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Yes, but what values specifically when the code breaks?
I should be getting numerical values for BidItemID & BidItemNo. When I step through the code everything seems to step through fine but the tagArray(0) for example says subscript out of range when I hover over them.
 
Upvote 0
What is the value of the n.Tag that is supposed to go into the tagArray? If there is nothing in the n.Tag, find out why nothing gets put there when the node tree is created.
 
Upvote 0
What is the value of the n.Tag that is supposed to go into the tagArray? If there is nothing in the n.Tag, find out why nothing gets put there when the node tree is created.
I checked that, it is getting populated correctly. If I step through it is looping as well and populating the n.tag
 
Upvote 0
When you get to this line when stepping through, is the Tag value of the SelectedItem what you expect?
VBA Code:
tagArray = Split(Me.TreeView1.SelectedItem.Tag, ",")
 
Upvote 0
When you get to this line when stepping through, is the Tag value of the SelectedItem what you expect?
VBA Code:
tagArray = Split(Me.TreeView1.SelectedItem.Tag, ",")
It says tagArray=Empty
 
Upvote 0
When you put the cursor over Tag (in the same line) what does it say?
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,312
Members
452,634
Latest member
cpostell

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