Using OERN is pretty standard fare when it comes to determining if a collection contains a given item, however life is a little easier if you also employ the Key field when using the Add method.
Code:Option Explicit Private Sub Command1_Click() Dim col As New Collection Dim Search4 As String col.Add "Henry", "Henry" col.Add "John", "John" Search4 = "Henry" On Error Resume Next col.Add Search4, Search4 If Err = 457 Then MsgBox Search4 & " is already in the collection" ElseIf Err Then MsgBox "Unanticipated error " & Err & " " & Err.Description Else 'if we do not want Search4 to be added col.Remove (Search4) MsgBox Search4 & " is NOT already in the collection" End If On Error GoTo 0 End Sub




Reply With Quote