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
 
Yes, it still exists. If you want to populate other information if you didn't grab it the first time through while populating the treeview, you'll have to go back in and get it every time unless you can find a way to store it somewhere.
So I could run the stored procedure on click of a node and have other fields populated at the same time as this last code you wrote?
 
Upvote 0

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
What is an example of the other fields you want populated?
 
Upvote 0
Hmm…I referenced a recordset, but this was for


Works like it should awesome.

I had mentioned Recordset in the last message and then I thought about it a bit....the recordset was related to populating the treeview. At this point does the recordset still exist or is it gone when the Sub Procedure to populate the treeview was completed? If it still exists, there are other fields that could be populate when the node is clicked.


There are also other fields that I can have in the same recordset that can populate at the same time as the node click are
Yes, it still exists. If you want to populate other information if you didn't grab it the first time through while populating the treeview, you'll have to go back in and get it every time unless you can find a way to store it somewhere.
I'm thinking I could potentially run a stored procedure in the TreeView1_NodeClick and populate the fields that I have in Blue Text at the same time as the rest of the NodeClick code you wrote.

The incomplete information I gave you...
-If the Node that is Selected is a BidItem then Textbox35 is to populate with BidItemCode and Label163 is to populate with BidItemDescription. ActivityCode is to be blank and ActivityItemDescription is to be Blank. There are also other fields that I can have in the same recordset that can populate at the same time...Label164 is to populate with TakeOffQuantity, Label165 is to populate with BidItemUOM, Label168 populate with BidItemQuantity

-If the Node that is selected is an ActivityItem then Textbox35 is to populate with the BidItemCode of its Parent and Label163 is to populate with BidItemDescription of its Parent...AND TextBox49 is to populate with the selected Node ActivityItemCode and Textbox48 is to populate with the selected Node ActivityItemDescription. There are also other fields that I can have in the same recordset that can populate at the same time...TextBox47 is to populate with ActivityItemQuantity, textBox46 is to populate with ActivityItemUOM
 
Upvote 0
How will the code determine which recordset to get the extra data from? If there are many records with ActivityCode 10, which one has the TakeOffQuantity you want to use?
 
Upvote 0
How will the code determine which recordset to get the extra data from? If there are many records with ActivityCode 10, which one has the TakeOffQuantity you want to use?
The recordset I am use has a parameter that is input that only creates a recordset relative to the current “Estimate” I am working on.
 
Upvote 0
When you click on a node, the code can go back into the data to get the missing fields. As the code is looping through the records (like when populating the treeview), how will the code know when the record that belongs to the data you want is found?
 
Upvote 0
When you click on a node, the code can go back into the data to get the missing fields. As the code is looping through the records (like when populating the treeview), how will the code know when the record that belongs to the data you want is

I was hoping that the BidItemNo and ActivityItemCode could be used on the selected Node. But, the ActivityItemCode will be repeated unfortunately.

How about a change event on the populated textboxes from the Treeview1_NodeClick. Textbox35 has the BidItemNo and TextBox49 has the ActivityItemCode.

If it is a BidItem node TextBox35 will have a value and the BidItemQuantity/BitItemUOM could be found in the recordset that way. Same with if it is a ActivityItem node, both the TextBox35(BidItemNo) and TextBox49(ActivityItemCode) would be there, then find in the recordset these values and bring back ActivityQuantity to TextBox47 and ActivityUOM to Textbox46.

I’m not really sure how this can be achieved, just thinking.
 
Upvote 0
Please provide another sample dataset with the extra values included. In your earlier workbook, Bid and Activity were included. To help out the analysis, the extra data would be helpful to see where it is located in the data in relation to Bid and Activity values.
 
Upvote 0
Please provide another sample dataset with the extra values included. In your earlier workbook, Bid and Activity were included. To help out the analysis, the extra data would be helpful to see where it is located in the data in relation to Bid and Activity values.

See attached link for the dataset.

I also setup the form so it currently works as it should with the newest code.

Thanks for your assistance, really appreciated.
 
Upvote 0

Forum statistics

Threads
1,215,737
Messages
6,126,559
Members
449,318
Latest member
Son Raphon

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