CodeItBetter Programming Another VB Programming Blog

How to check whether an item exists in a collection

Posted on January 4, 2009
1
2
3
4
5
6
7
8
9
10
11
12
13
'Arrays, Collections, Lists - How to check whether an item exists in a collection
'If you pass a numeric index that's either negative or greater than the number 
'of items currently in the collection, you get an error code 9-"Subscript out 
'of range" (exactly as if you were acting on a standard array); if you pass a 
'nonexistent string key, you get error code 5-"Invalid procedure call or 
'argument."

Function ItemExists(col As Collection, Key As String) As Boolean
    Dim dummy As Variant
    On Error Resume Next
    dummy = col.Item(Key)
    ItemExists = (Err <> 5)
End Function
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.