Solved

Access freezes when user does not send email

Posted on 2016-08-29
3
54 Views
Last Modified: 2016-08-29
Dear Experts,

I use the following code to send a student report to a parent:
Sub SendEmail()
       
        Dim pName As String
         pName = InputBox("Enter the parent's name", "SEND EMAIL TO PARENT")
         If pName = vbNullString Then
           GoTo Exit_SendEmail
         End If
               
        Dim School As String
        School = Nz(DLookup("SchoolName", "tblSchool"), 0)
        
        Dim TeacherName As String
        TeacherName = Nz(DLookup("teacherName", "tblTeacher"), 0)
        DoCmd.SetWarnings (False)
        Dim mailto As String
        Dim ccto As String
        Dim bccto As String
        mailto = Me.studentParentEmail
        ccto = ""
        bccto = ""
        emailmsg = "Hello " & pName & "!" & vbNewLine & vbNewLine & "Please find your child\'s student report from " & TeacherName & " at " & School & " attached!" _
        & vbNewLine & vbNewLine & "Thank you!"
        mailsub = "Recent Student Report from " & TeacherName & "at " & School

        DoCmd.OpenReport "rptStudentReport", acViewPreview, , "[studentID] = '" & [studentID] & "'"
        DoCmd.SendObject acSendReport, , acFormatPDF, mailto, ccto, bccto, mailsub, emailmsg, True
        DoCmd.Close acReport, "rptStudentReport", acSaveNo
        DoCmd.SetWarnings (True)
        

    
Exit_SendEmail:
    Exit Sub
 

 
End Sub


Private Sub cmdEmailParent_Click()
  Call SendEmail
End Sub

Open in new window


The problem that I am having is when a user closes the message dialoge box and does not 'send' the email message the application freezes and you have to hit ctrl+alt+delete to close the application. How can I capture that error and not have the application 'freeze'?

Thanks!
0
Comment
Question by:shogun5
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 34

Assisted Solution

by:ste5an
ste5an earned 300 total points
ID: 41774875
hmm, I don't see a reason for this.

But first of all separate your code. Create a send method and decouple it from the input task:

Public Sub SendEmail(pName As String)

  On Local Error GoTo LocalError
              
  Dim School As String
  Dim mailto As String
  Dim ccto As String
  Dim bccto As String
  Dim TeacherName As String
  Dim mailsub As String 
  
  School = Nz(DLookup("SchoolName", "tblSchool"), 0)
  TeacherName = Nz(DLookup("teacherName", "tblTeacher"), 0)
  mailto = Me.studentParentEmail
  ccto = ""
  bccto = ""
  mailsub = "Recent Student Report from " & TeacherName & "at " & School
  emailmsg = _
    "Hello " & pName & "!" & vbNewLine & vbNewLine & _
    "Please find your child\'s student report from " & TeacherName & " at " & School & " attached!" & vbNewLine & vbNewLine & _
    "Thank you!"
  
  DoCmd.SetWarnings (False)
  DoCmd.OpenReport "rptStudentReport", acViewPreview, , "[studentID] = '" & [studentID] & "'"
  DoCmd.SendObject acSendReport, , acFormatPDF, mailto, ccto, bccto, mailsub, emailmsg, True
  DoCmd.Close acReport, "rptStudentReport", acSaveNo
  DoCmd.SetWarnings (True)
  Exit Sub
  
LocalError:
  MsgBox Err.Description
        
End Sub

Private Sub cmdEmailParent_Click()
  
  Dim pName As String
  
  pName = InputBox("Enter the parent's name", "SEND EMAIL TO PARENT")
  If pName <> vbNullString Then
    SendEmail pName
  End If
         
End Sub

Open in new window


btw, mailsub is undeclared in your sample.
0
 
LVL 47

Accepted Solution

by:
Dale Fye (Access MVP) earned 200 total points
ID: 41774987
when you close the email created by sendobject, without actually sending the email, it returns an error code to Access, and since you did not have an error handler in your code, it was probably attempting to go back to whatever previous error handler you have configured.

I see the Ste5an has added an error handler, I believe that the error for the cancelled send object is 2501, but am not entirely sure.  At any rate, when you combine that with the Docmd.SetWarnings False, you are setting yourself up for all sorts of other errors.

So, I would recommend that you use something like the following at the end of your code:

  DoCmd.SendObject acSendReport, , acFormatPDF, mailto, ccto, bccto, mailsub, emailmsg, True

ProcExit:
  DoCmd.Close acReport, "rptStudentReport", acSaveNo
  DoCmd.SetWarnings (True)
  Exit Sub
  
LocalError:
    if err.number = 2501 then
        Resume Next
    Else
        MsgBox Err.Description
        resume ProcExit
    Endif
        
End Sub

Open in new window

This way, if you send the email, it drops into ProcExit to close the report. If you cancel the email, it will jump into the error handler and if the err.Number = 2501 (I think that is the one you want), it will go to the next line (which is in ProcExit).  And if some other error is raised, it will still enter ProcExit, ensuring that if you get to the point where you SetWarnings True, regardless of what happens.  Otherwise, if you exit this procedure with SetWarnings still set to False, you could end up with a serious surprise down the road.
1
 

Author Closing Comment

by:shogun5
ID: 41775504
Thanks Guys! Cbmbining both codes syntax proved to be exactly what I needed. Cheers!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question