I have created a Collection of objects in VBA and each object has a unique ID. I understand I can access the ‘items’ in a collection which is an inbuilt function of VBA. Is there a way I can access the object’s id? (i’m not sure how to set up the loop..
Currently I am doing something like:
For each objectName in CollectionName
objectName.Item(index)
Next objectName
This is correct syntax for accessing the collection’s Item, but I am unable to delve into the collection to get the object’s/item’s ID…is this possible?
fyi, the object’s id is set up as mID and is an integer.
Unfortunately, the
Collectionclass does not offer an iterator for the IDs. 🙁If you want this, use a
Dictionaryobject instead! It is part of the `Microsoft Scripting Runtime’ (to be selected in Tools->References) – and works like a Collection – but with a lot of additional functionality.Here’s an example:
Public Sub TestDictionary() Dim dict As New Dictionary Dim var As Variant dict.Add "Key1", 1 dict.Add 2, "Item 2" dict.Add "Another key", "blabla" For Each var In dict.Keys Debug.Print var, dict(var) Next var End Sub