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
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
sorry I had a mistake in the code.

The below is what I tried.

VBA Code:
Private Sub button1_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

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

'I have also tried it this way

Private Sub button1_Click()

    Form2.show

End Sub

' I get error Method or data member not found.  It looks like the listview1 Node is getting unchecked when I click on button1

Private Sub buttonAdd_Click()

       Form1.Nodes.Add Form1.SelectedItem.Text, tvwChild, Me.Listview1.Name, Me.Listview1.SelectedItem.Text 
End Sub
 
Upvote 0
sorry I had a mistake in the code.

The below is what I tried.

VBA Code:
Private Sub button1_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

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

'I have also tried it this way

Private Sub button1_Click()

    Form2.show

End Sub

' I get error Method or data member not found.  It looks like the listview1 Node is getting unchecked when I click on button1

Private Sub buttonAdd_Click()

       Form1.Nodes.Add Form1.SelectedItem.Text, tvwChild, Me.Listview1.Name, Me.Listview1.SelectedItem.Text
End Sub


I have read something about using the Tag for the Node you want to add to when going to another Form. Im not sure what that is.
 
Upvote 0
To understand how this works, follow this logic
  1. Load the Form1 in modeless mode
  2. Select item from Treeview
  3. Click on the button in Form1
  4. Load Form2 in modeless mode
  5. Select the item in listview and then click the button
DISCLAIMER :
  1. I have not done any error handling.
  2. I have not handled multiple selected items from listview. I have showed you how do it it for 1. I am sure you can replicate it for others

Sample Code

MODULE 1


VBA Code:
Option Explicit

Sub Sample()
    UserForm1.Show vbModeless
End Sub

USERFORM1

VBA Code:
Private Sub CommandButton1_Click()
    UserForm2.Show vbModeless
End Sub

USERFORM2

VBA Code:
Private Sub CommandButton1_Click()
    UserForm1.TreeView1.Nodes.Add UserForm1.TreeView1.SelectedItem.Text, tvwChild, ListView1.SelectedItem.Text, ListView1.SelectedItem.Text
    Unload Me
End Sub

SCREENSHOTS

1643319281397.png
 
Upvote 0
Quick testing. You can ignore the vbModeless part. The code works as expected without making it modeless.
 
Upvote 0
To understand how this works, follow this logic
  1. Load the Form1 in modeless mode
  2. Select item from Treeview
  3. Click on the button in Form1
  4. Load Form2 in modeless mode
  5. Select the item in listview and then click the button
DISCLAIMER :
  1. I have not done any error handling.
  2. I have not handled multiple selected items from listview. I have showed you how do it it for 1. I am sure you can replicate it for others

Sample Code

MODULE 1


VBA Code:
Option Explicit

Sub Sample()
    UserForm1.Show vbModeless
End Sub

USERFORM1

VBA Code:
Private Sub CommandButton1_Click()
    UserForm2.Show vbModeless
End Sub

USERFORM2

VBA Code:
Private Sub CommandButton1_Click()
    UserForm1.TreeView1.Nodes.Add UserForm1.TreeView1.SelectedItem.Text, tvwChild, ListView1.SelectedItem.Text, ListView1.SelectedItem.Text
    Unload Me
End Sub

SCREENSHOTS

View attachment 56304
I am getting an Invalid Key error.

How does the Module1 sub Sample work in the code? I dont see it used in the rest of the code.
Sub Sample()
Userform2.show vbmodeless
End Sub
 
Upvote 0
I am getting an Invalid Key error.

How does the Module1 sub Sample work in the code? I dont see it used in the rest of the code.
Sub Sample()
Userform2.show vbmodeless
End Sub
If I input the following code I can get a number 1 added to the node. Something with mu key it doesn't like. I added the "NewItem" test in place of Me.listview1.SelectedItem.Text.

Any suggestions? Can something else be used as the Key?

VBA Code:
Private Sub cmdAddSelectedActivities_Click()

        Form1.TreeView1.Nodes.Add Form1.TreeView1.SelectedItem.Text, tvwChild, "NewItem", Me.listview1.SelectedItem.Text
        
        Unload Me
        
End Sub
 
Upvote 0
I am using data from a Microsoft sql tables to populate the treeview. The nodes that I want to add can very likely to be duplicate in the me.listview1.selecteditem.text being used as the key.

Can another Unique key be a combination of something in the Treeview like the root, index or something?
Quick testing. You can ignore the vbModeless part. The code works as expected without making it modeless.
 
Upvote 0
Maybe it would be easier to insert the selected items on the listview to my database table and then reload the Treeview? But I can’t find this on googling either.
 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,121
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