当查询未返回任何记录时,MS Access需要取消SendObject

时间:2014-04-18 17:46:22

标签: sql ms-access module ms-access-2007 access-vba

我不是非常精通或正式接受过代码等方面的教育,但我需要一些在Access 2007模块中工作的帮助,并添加一行或两行,以便在没有返回记录时不发送电子邮件。

这就是我现在正在使用的内容:

Function Random_Report()
homepath = "\\server\folder\folder2\folder3\"
strFileName = "Random_Report” & ".xls"
DoCmd.OutputTo acOutputQuery, "Random_Report", acFormatXLS, homepath & strFileName, False

DoCmd.SendObject acSendQuery, " Random_Report ", acFormatXLS, "Someone2@company.com", , , _
" Random_Report ", "Good Afternoon Someone," _
& vbNewLine & "The Random_Report is ready for review at "\\server\folder\folder2\folder3\ and is attached to this email." _
& vbNewLine & "" & vbNewLine & "Thanks," _
& vbNewLine & "" & vbNewLine & "Someone Else", False

所以它会发送报告,但是我可以添加什么来阻止它在报告空白时发送电子邮件?

1 个答案:

答案 0 :(得分:0)

使用DCount检查查询( Random_Report )是否返回任何行。只有当DCount报告至少一行时,才会执行其他操作。

If DCount("*", "Random_Report") > 0 Then
    ' output file and send
End if