Solved

Access freezes when user does not send email

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Familiarize people with the process of utilizing SQL Server stored procedures 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 Micr…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

910 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

17 Experts available now in Live!

Get 1:1 Help Now