[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3952
  • Last Modified:

VB6 Collection Object

Hello,

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
0
ISC
Asked:
ISC
  • 3
1 Solution
 
RogueSolutionsCommented:

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
0
 
RogueSolutionsCommented:

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)

---



0
 
RogueSolutionsCommented:

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)

strGetItemExit:
    Exit Function

strGetItemErr:
    strGetItem = "Item Not Found"
    Resume strGetItemExit:
   
End Function
0
 
JR2003Commented:
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.

JR


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"
    Else
        Debug.Print "Key1 doesn't exist"
    End If
    If ExistsInCollection(MyColl, "Key3") Then
        Debug.Print "Key3 exists"
    Else
        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
Trap:

End Function


0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now