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
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
So then it is not getting the Tag from the currently selected node. What happens if you put Me.Treeview.selecteditem.Text instead? Step through the code and don't let it run past that line, but see if the selected node's text comes through. If not, are you sure there is a selected node?
 
Upvote 0
So then it is not getting the Tag from the currently selected node. What happens if you put Me.Treeview.selecteditem.Text instead? Step through the code and don't let it run past that line, but see if the selected node's text comes through. If not, are you sure there is a selected node?
It gives me the text of the item I selected. The selection seems to be working
 
Upvote 0
Step through the tree generation again. After the line executes for setting the n.Tag of the node with the issue, put the cursor over n.Tag to see if it got assigned the value you expect.
 
Upvote 0
Step through the tree generation again. After the line executes for setting the n.Tag of the node with the issue, put the cursor over n.Tag to see if it got assigned the value you expect.
N.tag has 6 values as I expect separated by a , comma
 
Upvote 0
Well, without being able to go through the code and data myself, I'm out of ideas. It seems like the tag is getting set correctly and that the selecteditem.Text is correct, but the selecteditem.Tag is blank, which it shouldn't be because you just confirmed it is assigned correctly.
 
Upvote 0
Step through the tree generation again. After the line executes for setting the n.Tag of the node with the issue, put the cursor over n.Tag to see if it got assigned the value you expect.
Would it have something to do with “BIN”
Well, without being able to go through the code and data myself, I'm out of ideas. It seems like the tag is getting set correctly and that the selecteditem.Text is correct, but the selecteditem.Tag is blank, which it shouldn't be because you just confirmed it is assigned correctly.


Do you want me to send the file and database to you.
 
Upvote 0

Forum statistics

Threads
1,223,276
Messages
6,171,140
Members
452,381
Latest member
Nova88

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