Collection Code

C

Cody

I created a collection based on the code in the thread titled "Class Instance
Identifier". On my Sheet1 I have a command button CommandButtonMainAddaModel
that starts a UserForm UserForm1. UserForm1 has a textbox TextBox1 and two
command buttons CommandButtonAddModel and CommandButtonCancelAddModel. The
CommandButtonAddModel calls AddModeltoCollection from Module1.

Sub AddModeltoCollection()

Dim clsVar As Class1

Set clsVar = New Class1
clsVar.Model = UserForm1.TextBox1.Value
StoreObject clsVar, clsVar.Model

End Sub

Sub StoreObject(cObject As Class1, sName As String)

If modelClass Is Nothing Then
Set modelClass = New Collection
End If

modelClass.Add cObject, sName
End Sub

Class1 has several properties including Model which is the key property.

I am trying to display the list of Model key names in a listbox in a new
form called UserFormModelListDisplay. Thread titled Object Required Error
8/22/05 is related to my problem. I have a type mismatch in the following
code trying to AddItem the Model properties to the ListBox1. See the
following code. Also it appears that the collection is lost when UserForm1
is unloaded. This may or may not be the case as I have not been able to
successfully list the property.

Private Sub UserFormModelListDisplay_Initialize()

Dim mItem As Variant

Load UserFormModelListDisplay
For Each mItem In modelClass
UserFormModelListDisplay.ListBox1.AddItem mItem
Next mItem
UserFormModelListDisplay.Show

End Sub

Any help here would be greatly appreciated.
 
C

Chip Pearson

Since you are adding Class objects to your Collection, you need
to get a property of the class in your code that adds to the list
box.

The line
UserFormModelListDisplay.ListBox1.AddItem mItem
should be
UserFormModelListDisplay.ListBox1.AddItem mItem.property

where 'property' is the desired property of the mItem class
instance.

--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
C

Cody

I have a new related problem. If I try to unload UserForm1 I get an Object
Required Error when I try to load the UserFormModelListDisplay form.
Apparently the objects in the collection are always tied to the UserForm1. I
would like to be able to unload and use the form elsewhere without losing the
collection.

How do I go about this?

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top