Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2003

    Unanswered: Error Trapping on collections

    Ok, I think I have a very simple question here so any help is greatly appreciated.

    I want to add items to a collection.

    Simple right?

    OK, here's the problem. I want the system to gracefully exit if an item is being addeded to the collection that already exists.

    Simple again right?

    Ok, here's where I'm stumped
    Private Sub Add(ctl As Integer, Person As Addressee)
        Dim var As Variant
    On Err GoTo errhandler
        Select Case ctl
        Case 1  'To:
            colTo.Add Person, "P" & Person.ID
        Case 2  'CC:
            colCC.Add Person, "P" & Person.ID
        Case 3  'BCC
            colBC.Add Person, "P" & Person.ID
        End Select
        Exit Sub
        Err.Raise Err.Number
        If Err.Number = "457" Then
            'Address allready added to the collection.  Do not want to create duplicates
            Resume Exit_Here:
            MsgBox "Error Number:" & Err.Number & vbCrLf & "Description:" & Err.Description, vbCritical, MODULE_NAME
        End If
    End Sub
    Now, this looks to me like this should be right and working no problems. Problem is that it never finds the errhandler. I get a message box with the error to end or debug. I'm sure you're all familiar with the window.

    Does anyone have any suggestions?

  2. #2
    Join Date
    Feb 2004
    Rather than "On Err...", try "On Error...".
    Last edited by bmacr; 12-02-05 at 23:50.

  3. #3
    Join Date
    Dec 2002
    Préverenges, Switzerland
    you don't need
    Err.Raise Err.Number
    the error is already raised (or you wouldn't be in errhandler)

    although i haven't used it myself, i believe that .raise inside an error handler causes VBA to search upwards in a nested procedures call list for an earlier error handler. you don't want this behaviour since you are handling the error locally in errhandler:

    currently using SS 2008R2

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts