Mike, you already helped me on this one; I'm trying to close a report (or not even run it) if there is no data. The problem is that in my Header section there is always the From and Until fields which prevent the On No data function from being used.
Here's what you posted last time;
Private Sub cmdPrintReport_Click()
Dim Db As Database
Dim rstTmp As Recordset
Dim strSQl As String
Set Db = CurrentDb()
strSQl = "[NB the same as the recordsource of your report]"
Set rstTmp = Db.OpenRecordset(SqlStr, dbOpenSnapshot)
'get an accurate recordcount:
On Error Resume Next
rstTmp.MoveLast
rstTmp.MoveFirst
On Error GoTo 0
If rstTmp.RecordCount > 0 Then
DoCmd.OpenReport "YourReportName", acViewNormal
MsgBox "Report printed"
Else
MsgBox "No data to report"
End If
Set Db = Nothing
End Sub
If the recordsource for your report is a saved query (as opposed to SQL pasted directly into the Recordsource property), you may be able to simplify it right down to:
Private Sub cmdPrintReport_Click()
If DCount("*", "[YourQueryName]") > 0 Then
DoCmd.OpenReport "YourReportName", acViewNormal
MsgBox "Report printed"
Else
MsgBox "No data to report"
End If
End Sub
(I'm assuming for both of these examples that you are running the report from a command button)
_________
To further complicate matters I only want the report to run if BOTH From and Until dates have been selected. Here's what I used;
Private Sub SDG_Report_1_Click()
If IsNull(Me.From_Date) Or IsNull(Me.Until_Date) Then
MsgBox "Both dates are required"
Else
DoCmd.RunMacro ("Run SDG Report 1 - MASTER")
End If
End Sub
_________
How would I incorporate this into your code above?
Thanks for any further help,
Regards
Franky.
Here's what you posted last time;
Private Sub cmdPrintReport_Click()
Dim Db As Database
Dim rstTmp As Recordset
Dim strSQl As String
Set Db = CurrentDb()
strSQl = "[NB the same as the recordsource of your report]"
Set rstTmp = Db.OpenRecordset(SqlStr, dbOpenSnapshot)
'get an accurate recordcount:
On Error Resume Next
rstTmp.MoveLast
rstTmp.MoveFirst
On Error GoTo 0
If rstTmp.RecordCount > 0 Then
DoCmd.OpenReport "YourReportName", acViewNormal
MsgBox "Report printed"
Else
MsgBox "No data to report"
End If
Set Db = Nothing
End Sub
If the recordsource for your report is a saved query (as opposed to SQL pasted directly into the Recordsource property), you may be able to simplify it right down to:
Private Sub cmdPrintReport_Click()
If DCount("*", "[YourQueryName]") > 0 Then
DoCmd.OpenReport "YourReportName", acViewNormal
MsgBox "Report printed"
Else
MsgBox "No data to report"
End If
End Sub
(I'm assuming for both of these examples that you are running the report from a command button)
_________
To further complicate matters I only want the report to run if BOTH From and Until dates have been selected. Here's what I used;
Private Sub SDG_Report_1_Click()
If IsNull(Me.From_Date) Or IsNull(Me.Until_Date) Then
MsgBox "Both dates are required"
Else
DoCmd.RunMacro ("Run SDG Report 1 - MASTER")
End If
End Sub
_________
How would I incorporate this into your code above?
Thanks for any further help,
Regards
Franky.