Solved

Access freezes when user does not send email

Posted on 2016-08-29
3
30 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 32

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Familiarize people with the process of utilizing SQL Server views 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 Access…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now