Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a Collection through assignment

Status
Not open for further replies.

djmc

Programmer
Jun 12, 2002
179
0
0
CA
Hello,

I understand you can create something similar to a hashtable in VBA called a Collection and that you can use the add function to add key,value pairs.

I was wondering if there is a way to create a static Collection object without using the add function. Like
myCollection = {"a:1","b:2"} or whatever.

Thanks.
 
A collection is an object, and so the manipulation of that object is basically limited to those Properties, Methods, and Events that the object exposes.

That being said, you can write a public function in a standard code module which creates the collection, Adds all of the entires defined in the arguement list (the ParamArray would work well here), and then then function returns the built up collection.

Good Luck
--------------
To get the most from your Tek-Tips experience, please read FAQ181-2886
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
I seem to be having a problem, I made a function that returns a collection that i've created. Maybe I am not understanding the usage of Collections but is there a way to get the value given the key?

myCollection.Add "key","value"

now i want somethng like myCollection("key") to get "value"

Can anyone suggest how I would go about doing this.
 
I want somethng like myCollection("key") to get "value"
This is a correct syntax, so where is your problem ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Here is an example:

Dim myCollection As New Collection

myCollection.Add "28", "One"
myCollection.Add "31", "Two"
myCollection.Add "34", "Three"

Debug.Print myCollection("28")

does not work

but

Debug.Print myCollection(1)

prints 28



 
Replace this:
myCollection.Add "key","value"
By this:
myCollection.Add "value", "key"

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Thanks, that helps.

I have another problem now. Let's say I attempt to lookup something that is not in the table, I want to be able to set my variable to "" if it fails to lookup a value from the given key. However, VBA chooses to prompt an error to me. I was wondering how I can catch the error and continue on with my code. Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top