Solved

Access freezes when user does not send email

Posted on 2016-08-29
3
42 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
3 Comments
 
LVL 33

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

786 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