Get Checked Listview Items from Form to populate Treeview in a Different Form

bhalbach

Board Regular
Joined
Mar 15, 2018
Messages
221
Office Version
  1. 2016
Platform
  1. Windows
I am really struggling to understand how to do this.

I have Form1 that has a treeView1. On Form1, I select a Node that I want to add a ChildNode to and then click Button1.
Button1 (on Form1) will open Form2 which contains a Listview1 with checkboxes.
I will then either...
Check items on the Listview1 that I want to add to the Node that I had selected on Form1 (could be 1 item checked or multiple) then click buttonAdd. The buttonAdd would return to form1 and add the selected items to treeview1 previously selected Node.
OR
I could also click buttonCancel (on form2) and select nothing and return to Form1.

I am really grasping to understand the treeview.

Can someone please assist me with this.



VBA Code:
Private Sub buttonAdd_Click()

Form2.show

'This gives me an error Invalid Key'
Me.TreeView1.Nodes.Add Me.TreeView1.SelectedItem.Text, tvwChild, Form2.listview1.SelectedItem.Text, Form2.listview1.SelectedItem.Text

'This gives me an error Invalid Key'
Me.TreeView1.Nodes.Add Me.TreeView1.SelectedItem.Index, tvwChild, Form2.listview1.SelectedItem.Checked, Form2.listview1.SelectedItem.Text

'This gives me an error Index Out of Bounds'
Me.TreeView1.Nodes.Add Me.TreeView1.SelectedItem.Selected, tvwChild, Form2.listview1.SelectedItem.Checked, Form2.listview1.SelectedItem.Text


End Sub
 
You can use the CoCreateGuid API to create unique keys. I have demonstrated it HERE

I have taken a different route and decided to add the items to the ms Sql database and then populate the treeview from a recordset.

I am stuck trying to add the child nodes.

Can you assist?

The following is the code I am trying to use to populate from the recordset. All connections etc are working, and I can get the Parent nodes loaded but not the child nodes.
When I try to load the Treeview I get "Error number = 2147257114 Invalid object"
I am pretty lost with this.



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
 
Upvote 0

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Tenho um exemplo que clicando no Nó da Treeview, os dados são carregados na Listview.
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,223
Members
452,620
Latest member
dsubash

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