我有一个按钮,电子邮件名为EngdetailRpt的过滤报告,如果实际发送电子邮件,它的效果很好。 但是,如果由于某种原因从outlook取消了电子邮件,我收到错误,下次我尝试发送过滤报告时,它会发送与上次相同的过滤器或完全锁定数据库。 任何人都可以建议处理程序代码,以防止这些错误。
下面是我目前的代码,
Private Sub cmdEmail_Click()
On Error GoTo cmdEmail_Click_Err
'opens report (hidden) based on Current Form view
DoCmd.OpenReport "EngDetailRpt", acViewPreview, "", "[TAG_NAME]='" & Me!TAG_NAME & "'", acHidden
DoCmd.SendObject acSendReport, "EngDetailRpt", acFormatPDF
DoCmd.Close acReport, "EngDetailRpt", acSaveNo
cmdEmail_Click_Exit:
Exit Sub
cmdEmail_Click_Err:
MsgBox Error$
DoCmd.Close acForm, "EngDetailsFrm", acSavePrompt
Resume cmdEmail_Click_Exit
End Sub
答案 0 :(得分:0)
您需要确保清理代码(关闭隐藏报告)仍然运行,即使在出错之后也是如此。
在这种情况下,我更喜欢使用“内联”错误处理,如下所示:
'opens report (hidden) based on Current Form view
DoCmd.OpenReport "EngDetailRpt", acViewPreview, "", "[TAG_NAME]='" & Me!TAG_NAME & "'", acHidden
' inline error handling
On Error Resume Next
DoCmd.SendObject acSendReport, "EngDetailRpt", acFormatPDF
If Err.Number <> 0 Then
MsgBox Err.Description, vbExclamation, "Runtime-Error " & Err.Number & " on SendObject"
End If
On Error GoTo cmdEmail_Click_Err
DoCmd.Close acReport, "EngDetailRpt", acSaveNo