none
Run-time error '2501' when email is cancelled... RRS feed

  • Question

  • Hi,

    I am fairly new to Access and would really appreciate a bit of help with the following issue.  I have a button to send a group email to selected recipients (see code below).  This all works fine unless I decide to close the email without sending, whereupon I get the following message:

    Run-time error '2501':

    The SendObject action was cancelled.

    This is my code:

    Private Sub Command47_Click()
    Dim rs As Object, recount As Integer, count As Integer, sql As String
    Set rs = CurrentDb.OpenRecordset("group email query")
    'MsgBox rs.RecordCount
    If rs.RecordCount > 0 Then
    rs.MoveLast
    rs.MoveFirst
    'MsgBox rs.RecordCount
    recount = rs.RecordCount
    For count = 1 To recount
    If Not IsNull(rs![Owner 1 E-mail Address]) Then EmailAddress = EmailAddress & rs![Owner 1 E-mail Address] & ";" '
    rs.MoveNext
    Next count
    EmailAddress = Left(EmailAddress, Len(EmailAddress) - 1)
    'MsgBox EmailAddress
    DoCmd.SendObject , , , , , EmailAddress, , , True
    End If
    rs.Close
    Set rs = Nothing

    End Sub


    Any ideas??!

    Many thanks,

    Clare

    Tuesday, September 24, 2019 10:04 AM

Answers

  • I'd personally do something more like

    Private Sub Command47_Click()
        Dim rs                    As DAO.Recordset    'Object
        Dim EmailAddress          As String
    
        On Error GoTo Error_Handler
    
        Set rs = CurrentDb.OpenRecordset("group email query", dbOpenSnapshot)
        With rs
            If .RecordCount <> 0 Then
                Do While Not .EOF
                    If Not IsNull(![Owner 1 E-mail Address]) Then EmailAddress = EmailAddress & _
                       ![Owner 1 E-mail Address] & ";"    '
                    rs.MoveNext
                Loop
                If Len(EmailAddress) <> 0 Then EmailAddress = Left(EmailAddress, Len(EmailAddress) - 1)
            End If
        End With
    
        If Len(EmailAddress) <> 0 Then
            DoCmd.SendObject , , , , , EmailAddress, , , True
        Else
            MsgBox "There is no recipient?"
        End If
    
    Error_Handler_Exit:
        On Error Resume Next
        If Not rs Is Nothing Then
            rs.Close
            Set rs = Nothing
        End If
        Exit Sub
    
    Error_Handler:
        If Err.Number <> 2501 Then
            MsgBox "The following error has occured" & vbCrLf & vbCrLf & _
                   "Error Number: " & Err.Number & vbCrLf & _
                   "Error Source: Command47_Click" & vbCrLf & _
                   "Error Description: " & Err.Description & _
                   Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
                   , vbOKOnly + vbCritical, "An Error has Occured!"
        End If
        Resume Error_Handler_Exit
    End Sub

    and why are you

    If Len(EmailAddress) <> 0 Then EmailAddress = Left(EmailAddress, Len(EmailAddress) - 1)

    What is the point of removing the last ;?


    Daniel Pineault, 2010-2019 Microsoft MVP
    Professional Support: http://www.cardaconsultants.com
    MS Access Tips and Code Samples: http://www.devhut.net


    Tuesday, September 24, 2019 11:56 AM

All replies

  • You need to use Error Handling and error trapping to ignore the error

    Private Sub Command47_Click()
        Dim rs                    As DAO.Recordset 'Object
        Dim recount               As Integer
        Dim count                 As Integer
        Dim sql                   As String
    
        On Error GoTo Error_Handler
    
        Set rs = CurrentDb.OpenRecordset("group email query")
        'MsgBox rs.RecordCount
        If rs.RecordCount > 0 Then
            rs.MoveLast
            rs.MoveFirst
            'MsgBox rs.RecordCount
            recount = rs.RecordCount
            For count = 1 To recount
                If Not IsNull(rs![Owner 1 E-mail Address]) Then EmailAddress = EmailAddress & rs![Owner 1 E-mail Address] & ";"    '
                rs.MoveNext
            Next count
            EmailAddress = Left(EmailAddress, Len(EmailAddress) - 1)
            'MsgBox EmailAddress
            DoCmd.SendObject , , , , , EmailAddress, , , True
        End If
    
    Error_Handler_Exit:
        On Error Resume Next
        If Not rs Is Nothing Then
            rs.Close
            Set rs = Nothing
        End If
        Exit Sub
    
    Error_Handler:
        If Err.Number <> 2501 Then
            MsgBox "The following error has occured" & vbCrLf & vbCrLf & _
                   "Error Number: " & Err.Number & vbCrLf & _
                   "Error Source: Command47_Click" & vbCrLf & _
                   "Error Description: " & Err.Description & _
                   Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
                   , vbOKOnly + vbCritical, "An Error has Occured!"
        End If
        Resume Error_Handler_Exit
    End Sub


    Daniel Pineault, 2010-2019 Microsoft MVP
    Professional Support: http://www.cardaconsultants.com
    MS Access Tips and Code Samples: http://www.devhut.net

    Tuesday, September 24, 2019 11:30 AM
  • I'd personally do something more like

    Private Sub Command47_Click()
        Dim rs                    As DAO.Recordset    'Object
        Dim EmailAddress          As String
    
        On Error GoTo Error_Handler
    
        Set rs = CurrentDb.OpenRecordset("group email query", dbOpenSnapshot)
        With rs
            If .RecordCount <> 0 Then
                Do While Not .EOF
                    If Not IsNull(![Owner 1 E-mail Address]) Then EmailAddress = EmailAddress & _
                       ![Owner 1 E-mail Address] & ";"    '
                    rs.MoveNext
                Loop
                If Len(EmailAddress) <> 0 Then EmailAddress = Left(EmailAddress, Len(EmailAddress) - 1)
            End If
        End With
    
        If Len(EmailAddress) <> 0 Then
            DoCmd.SendObject , , , , , EmailAddress, , , True
        Else
            MsgBox "There is no recipient?"
        End If
    
    Error_Handler_Exit:
        On Error Resume Next
        If Not rs Is Nothing Then
            rs.Close
            Set rs = Nothing
        End If
        Exit Sub
    
    Error_Handler:
        If Err.Number <> 2501 Then
            MsgBox "The following error has occured" & vbCrLf & vbCrLf & _
                   "Error Number: " & Err.Number & vbCrLf & _
                   "Error Source: Command47_Click" & vbCrLf & _
                   "Error Description: " & Err.Description & _
                   Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
                   , vbOKOnly + vbCritical, "An Error has Occured!"
        End If
        Resume Error_Handler_Exit
    End Sub

    and why are you

    If Len(EmailAddress) <> 0 Then EmailAddress = Left(EmailAddress, Len(EmailAddress) - 1)

    What is the point of removing the last ;?


    Daniel Pineault, 2010-2019 Microsoft MVP
    Professional Support: http://www.cardaconsultants.com
    MS Access Tips and Code Samples: http://www.devhut.net


    Tuesday, September 24, 2019 11:56 AM
  • Thank you so much! It works!  I've lost about a day trying to work this out for myself... like I said - I'm new to access! :)
    Tuesday, September 24, 2019 3:51 PM