Question

I'm not super well versed or formally educated with code etc. but I need some help working in the Access 2007 module and adding in a line or two to NOT send the email when there are no records returned.

This is pretty much what I'm working with right now:

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

So it will send the report regardless, but what can I add to keep it from sending the email when the report is blank?

Was it helpful?

Solution

Use DCount to check whether the query (Random_Report) returns any rows. And only do the other actions when DCount reports at least one row.

If DCount("*", "Random_Report") > 0 Then
    ' output file and send
End if
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top