Collections and Userforms

jaybee3

Active Member
Joined
Jun 28, 2010
Messages
307
Hi All,

I have a module that is trying to store classes in a collection based off selections in a userform and I'm a little stuck. For illustrative purposes I've simplified it.

MainTest is my main module

Code:
Sub MainTest()

Dim colTest As New Collection
    fTest.Show

   ..Does stuff with the elements in the collection..
 
End Sub

fTest is the userform (that contains a text box (tbName) and and two command buttons (cbRun and cbClose))

Code:
Private Sub cbRun_Click()
    Dim cPerson as New cTest
    cPerson.Name = fTest.tbName.value
    colTest.add cPerson
End Sub

Private Sub cbClose_Click()
   fTest.hide
End Sub

cTest is the class

Code:
Private pName As String

Property Let Name(value As String)
    pName = value
End Property

Property Get Name() As String
    Name = pName
End Property

The problem I face is that the classes aren't added to the collection as it's not 'in' the userform. Can you pass the collection to the userform as I would a function? Or any other help would be greatly appreciated.

Thanks in advance.
 

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.
Yes - or you could simply use a public variable
Code:
Public colTest As Collection
Sub MainTest()
Set colTest = New Collection
    fTest.Show   ..Does stuff with the elements in the collection.. 
End Sub
 
Upvote 0
I'd prefer:
Code:
Sub MainTest()


Dim colTest As collection
Dim myForm As fTest
Set colTest = New collection
Set myForm = New fTest


colTest.Add "MyItem"


MsgBox colTest(1)


Set fTest.collection = colTest
fTest.Show


MsgBox colTest(2)
 
End Sub

userform:
Code:
Private p_collection As collection


Public Property Set collection(ByRef coll As collection)
    Set p_collection = coll
End Property




Private Sub UserForm_Click()
    p_collection.Add "another item"
End Sub

But it's horses for courses ;)
 
Upvote 0
Took me a couple of seconds to spot the ByRef...I really like it.

Also taught me something else too...I didn't know you could instance forms like that :oops:

Thanks a lot for your input on the alternative, Kyle, much appreciated :)
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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