VB6 Collection Object


I have a 'VB Collection' and call the function 'Collection.Item(key)', if there is no item that matches the key passed in
I want to just carry on executing the next line of code, (currently my program crashes when it does not find an item matching the key).

How can I achieve the above.............    

Regards Ian
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


Well, its not terribly good practise but you can put On Error Resume Next before the call that fetches the item.  You then need to put On Error Goto 0 after it.

Better to make a function and handle the 'fetch' from that

More fully ...

On Error Resume Next

strMyItem = MyCollection.Item("MyKey")
If Err <> 0 Then strMyItem = "Item Not Found"

On Error Goto 0   'if you already have error handling in the sub then you'd do On Error Goto ErrorHandler: (or whatever your handler label is)



And the better (IMO) way ...

Public Sub ProcessItems()

    strMyItem = strGetItem(colMyCollection, "MyKey")
End Sub

Private Function strGetItem(ByRef colMyCol As Collection, ByVal strKey As String) As String

    On Error GoTo strGetItemErr:
    strGetItem = colMyCol.Item(strKey)

    Exit Function

    strGetItem = "Item Not Found"
    Resume strGetItemExit:
End Function
You could write an exists function for use on collections as given below.
Another better method is to use the Scripting Dictionary object instead of a collection object as this has an inbuild 'Exists' fucntion. To use the Scripting.Dictionary just set a reference to "Microsfit Scripting Runtime" in the project references and declare a variable as:
Dim MyDictionary as New Scripting.Dictionary
usage is very similar to a collection but some of the parameters are a different way round.


Option Explicit

Private Sub Command1_Click()

    Dim MyColl As Collection
    Set MyColl = New Collection
    MyColl.Add "Item1", "Key1"
    MyColl.Add "Item2", "Key2"
    If ExistsInCollection(MyColl, "Key1") Then
        Debug.Print "Key1 exists"
        Debug.Print "Key1 doesn't exist"
    End If
    If ExistsInCollection(MyColl, "Key3") Then
        Debug.Print "Key3 exists"
        Debug.Print "Key3 doesn't exist"
    End If

End Sub

Private Function ExistsInCollection(MyCollection As Collection, sKey As String) As Boolean

    On Error GoTo Trap

    Dim vTmp As Variant
    vTmp = MyCollection.Item(sKey)
    ExistsInCollection = True

End Function


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.