VB6  Collection Object

Posted on 2004-11-26
Last Modified: 2007-12-19

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
Question by:ISC
    LVL 5

    Expert Comment


    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
    LVL 5

    Expert Comment


    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)


    LVL 5

    Expert Comment


    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
    LVL 18

    Accepted Solution

    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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    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…
    Article by: Martin
    Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    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…

    779 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

    15 Experts available now in Live!

    Get 1:1 Help Now