If a key exists in a Collection

Posted on 2005-04-11
Last Modified: 2010-05-02
Hi everyone,

The VB Collection object allows the user to add an item and a key associated with it (like a HashTable). How can I check if a key already exists in a collection object before I add another object to the collection? If I try to add a key that already exists, I would get a run time error.

Question by:xargon_123
    LVL 24

    Expert Comment

    Hi xargon_123,

    add an error handler.

    On Error Resume Next
    MyCollection.Add 1, CStr(n)
    On Error GoTo 0

    to check if a key exists, i guess you will have to loop through all the items and check each key...

    LVL 10

    Expert Comment

    To check if a key exists, simply try to return the oject with that key. If it doesn't exist, you will receive a run time error 5 - Invalid procedure call or arguement:

    On Error Resume Next

      if Err.Number=5 then
       'Key doesn't exist
       'Key exists
      end if
    On Error GoTo 0
    LVL 19

    Accepted Solution

    You can do it while you add, as in:

    Private Sub Command1_Click()
    Dim myCollection As Collection, addItem As String, addKey As String
    Set myCollection = New Collection
    addItem = "xargon_123"
    addKey = "Key_123"

    On Error GoTo errorhandling
    myCollection.Add addItem, addKey
    myCollection.Add addItem, addKey

    Set myCollection = Nothing
    Exit Sub
    If Err.Number = 457 Then
        MsgBox "Key '" & addKey & "' already exist"
        Resume Next
    End If
    End Sub

    LVL 5

    Expert Comment

    Alternatively you can use the Dictionary Object of the Windows Scripting Runtime instead of VBs collection object, similar to a collection but a lot more friendly. Also has a builtin method to check if a key exits e.g.

    Dim doCol As Dictionary

        Set doCol = New Dictionary
        doCol.Add "Key1", "Item1"
        doCol.Add "Key2", "Item2"
        doCol.Add "Key3", "Item3"

        If doCol.Exists("Key2") = True Then
            ' Do something
            ' Do something else
        End If

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    Suggested Solutions

    Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
    When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
    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…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    734 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

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now