checking if an item exists in a custom collection

  • Thread starter Thread starter rocco
  • Start date Start date
R

rocco

Hello,
I have a custom collection and I will add few class objects to it.
How can I check if an object exists in the collection?

Thanks,
rocco
 
You can use a Dictionary rather than a Collection.

To use a Dictionary, you have to include a reference to Microsoft Scripting
Runtime ( Tools | References ... )

In VBA, you can refer it with the prefix Scripting, if there is a collision
of names.


Dim aaa As Scripting.Dictionary ' just for illustration of possible
disambiguation
Set aaa = New Dictionary

aaa.Add "hello", Nothing
If Not aaa.Exists("hello") Then aaa.Add "hello", 222.333
If Not aaa.Exists("world") Then aaa.Add "world", Now


Note that a Dictionary contains a list of keys and a list of items. The keys
do not allow duplicated value ( a trappable error will occur if a the key
already exist and you try to add another item with the same key), while it
allows to find an item, given a key, faster than by doing a for each loop
walk trough search.


Vanderghast, Access MVP
 
rocco said:
Hello,
I have a custom collection and I will add few class objects to it.
How can I check if an object exists in the collection?


As an alternative to using a Dictionary object, you can try to retrieve an
item from the collection by its key value, and trap the error that occurs if
the item is not found.
 
Back
Top