Solved

Declare collection at runtime

Posted on 2002-05-23
8
185 Views
Last Modified: 2010-05-02
Hi I am new to Visual Basic.

I can declare a collection in Visual Basic by

Dim mybooks As New Collection

however at run time I would like to create new collections
which a user may type in.

so I woudl like to

dim ("whatever usertypes in") as new collection

and use

"whatever usertypes in" as a normal object name.

Thanks for any help.

Ber
0
Comment
Question by:bgumble
8 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 7029336
Collections are compiled so you can create them at run-time. If you are using collections to store regular data (integer, date, string, ...), you can use disconnected ADO Recordset instead of collection. Recordsets can exists without a database and you can add them at run-time.
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 7029427
There is no way that you are going to get something like this. You could create an array of collections so that you could reference an element in a collection using something like:

aryCollections(arrayElement)(CollectionElement).Property = "Hello"
0
 

Author Comment

by:bgumble
ID: 7029531
Hi Tim,

Thank you for your suggestion.

Could you please give me and example on how to do this.

Thanks
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:bgumble
ID: 7029541
Hi Tim,

Thank you for your suggestion.

Could you please give me and example on how to do this.

Thanks
0
 
LVL 6

Expert Comment

by:holli
ID: 7029689
Since a collection can hold all types, it can also handle other collections, so:

'A collection to hold the collections
dim myCollectionOfCollections as new collection

'The member to add
dim myCollectionMember as Collection
....


'Adding a collection
s="Name"
set myCollectionMember = new collection
myCollectionMember.add "Value", "Key"
myCollectionOfCollections.add myCollectionMember,s


'Dereferencing a collection
v=myCollectionOfCollections.Item("Name").Item("Key") 'v is now "value"


regards, holli
0
 

Author Comment

by:bgumble
ID: 7030881
Hi Holli

Thanks for the help..


That all works

however I can't seem to easly add an item to the collection that is within the collection..

I've tried

myCollectionOfCollections.Item("Name").Item("Key") = newvalue

0
 

Author Comment

by:bgumble
ID: 7030918
Actually, what I really need to know is how to add an element to the collection within the collection

thanks..
0
 
LVL 6

Accepted Solution

by:
holli earned 50 total points
ID: 7031444
just as if the collection would be single:

myCollectionOfCollections.Item("Name").Add "Key2", NewValue
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question