I use code below to create mail with attachment.
How can I run DoCmd.RunSQL SQL after mail is send and cancel DoCmd.RunSQL SQL if mail is NOT send?
Gerrit
How can I run DoCmd.RunSQL SQL after mail is send and cancel DoCmd.RunSQL SQL if mail is NOT send?
Code:
Private Sub Mailpo2_DblClick(Cancel As Integer)
On Error GoTo ErrorHandler
Dim strMsg As String
Dim SD As String
Dim SQL As String
Dim objOutlook As Outlook.Application
Dim objEmail As Outlook.MailItem
Dim strAttach1 As String
Set objOutlook = CreateObject("Outlook.application")
Set objEmail = objOutlook.CreateItem(olMailItem)
If Me.TransporttationID = 2 Then
SD = "Shipping Marks: " & [Shipping Marks] & Chr(13) + Chr(10) & "Vessel: " & [Vessel] & Chr(13) + Chr(10) & "ETD: " & [DepartureDate] & Chr(13) + Chr(10) & Chr(13) + Chr(10)
Else
SD = "Shipping Marks: " & [Shipping Marks] & Chr(13) + Chr(10) & "Flight: " & [Vessel] & Chr(13) + Chr(10) & "ETD: " & [DepartureDate] & Chr(13) + Chr(10) & "AWB: " & [AWB] & Chr(13) + Chr(10)
End If
strMsg = "Dear " & [FirstName] & "," & Chr(13) + Chr(10) & Chr(13) + Chr(10) & [HandlingAddress] & Chr(13) + Chr(10) & Chr(13) + Chr(10) & Nz([Clausulep1], "") & Chr(13) + Chr(10) & Chr(13) + Chr(10) & Nz([ClausuleP3], "") & Chr(13) + Chr(10) & Chr(13) + Chr(10) & Nz([ClausuleP2], "") & Chr(13) + Chr(10) & Chr(13) + Chr(10) & [DocTo] & Chr(13) + Chr(10) & Chr(13) + Chr(10) & SD & Chr(13) + Chr(10) & [TextOnBL] & Chr(13) + Chr(10) & [Customs] & Chr(13) + Chr(10) & Chr(13) + Chr(10)
strAttach1 = sDefaultPath & [Path] & "/" & [OrderNumber] & "A.pdf"
If Me.Check = -1 Then
With objEmail
.To = [email]
.CC = Nz([emailCC], "")
.Subject = "Ordernumber " & [OrderNumber]
.Body = strMsg
.Display
.Attachments.Add strAttach1
End With
SQL = "UPDATE tblShipmentDetails SET tblShipmentDetails.Status = [tblShipmentDetails]![Status]+1 WHERE (((tblShipmentDetails.DetailID)=[Forms]![frmSendPO]![subMailBodyOrder].[Form]![DetailID]));"
DoCmd.RunSQL SQL
Else
DoCmd.CancelEvent
MsgBox "File to Add Does Not Exist"
GoTo Repeat
End If
ErrorHandler:
If Err.Number = 3059 Then
MsgBox "No Records Updated!"
GoTo Repeat
Else
MsgBox "An error occurred:" & vbCrLf & _
"Error " & Err.Number & ": " & Err.Description
GoTo Repeat
Repeat:
End If
End Sub
Gerrit