I have a form from which I print or PDF my reports and one button on the form I use creates a whole load of PDF reports using the code below. Most of the reports have code in the NoData which is fine for when the report is opened individually but this means that if I run the full code and a report has no data then runtime error '2501' is triggered. I have tried to set up error trapping but can't seem to get it right so wondered if anyone might be kind enough to point me in the right direction.
Thank you Neil
Code in reports for NoData
Code used to generate a full PDF file
Thank you Neil
Code in reports for NoData
Code:
Private Sub Report_NoData(Cancel As Integer)
MsgBox "There are currently no records available for this report."
Cancel = True
Code used to generate a full PDF file
Code:
Private Sub cmdFullPlanPDF_Click()
DoCmd.OpenForm "frmPleaseWait"
'Before running report count records and if no records cancel print
Dim intstore As Integer
'Count of fields selected
intstore = DCount("[FieldCode]", "[qryrpt10plan]")
'If count of fields is zero then display message and cancel print else create PDF
If intstore = 0 Then
MsgBox "There are no records for this report"
Exit Sub
Else
'Then determine whether there are any manure applications and
' select which set of reports to print
Dim blRet As Boolean
Dim intstore2 As Integer
'Count of fields selected
intstore2 = DCount("[CroppingYear]", "[qryOMApplicationCharts]")
'If there are no manures in this cropping year then don't print manure charts
If intstore2 = 0 Then
'Header
DoCmd.OutputTo acOutputReport, "rptMainNPlanHeader", "PDF Format (*.pdf)", "" _
& "x:\Plan Holding Folder\" & Me.[AccountNameLbl] + (Format(Now(), " ddmmyy")) + " - " & "1 - Header.pdf"
'PLAN
DoCmd.OutputTo acOutputReport, "rpt10-Plan09", "PDF Format (*.pdf)", "" _
& "x:\Plan Holding Folder\" & Me.[AccountNameLbl] + (Format(Now(), " ddmmyy")) + " - " & "2 - PLAN.pdf"
'Cropping Totals
DoCmd.OutputTo acOutputReport, "rptCropTotalsWithYieldsCT1", "PDF Format (*.pdf)", "" _
& "x:\Plan Holding Folder\" & Me.[AccountNameLbl] + (Format(Now(), " ddmmyy")) + " - " & "3 - Cropping Totals Report.pdf"
'Fertiliser Requirement with Field Details
DoCmd.OutputTo acOutputReport, "rptFertReqWithFieldDetail", "PDF Format (*.pdf)", "" _
& "x:\Plan Holding Folder\" & Me.[AccountNameLbl] + (Format(Now(), " ddmmyy")) + " - " & "4 - Fertiliser Requirements with Field Details.pdf"
'NVZ Nitrogen Planning Report
DoCmd.OutputTo acOutputReport, "rptPlanningNUse", "PDF Format (*.pdf)", "" _
& "x:\Plan Holding Folder\" & Me.[AccountNameLbl] + (Format(Now(), " ddmmyy")) + " - " & "5 - NVZ Nitrogen Planning Report.pdf"
'Fertiliser Application Charts
DoCmd.OutputTo acOutputReport, "rptFertApplnChartsJK1", "PDF Format (*.pdf)", "" _
& "x:\Plan Holding Folder\" & Me.[AccountNameLbl] + (Format(Now(), " ddmmyy")) + " - " & "6 - Fertiliser Application Charts.pdf"
'NVZ Soil Type Assessment
DoCmd.OutputTo acOutputReport, "rptNVZClosedPeriod", "PDF Format (*.pdf)", "" _
& "x:\Plan Holding Folder\" & Me.[AccountNameLbl] + (Format(Now(), " ddmmyy")) + " - " & "9 - NVZ Soil Type Assessment.pdf"
'N Max Farm Summary (Report 5)
DoCmd.OutputTo acOutputReport, "rptSummaryforNMaxCalcs", "PDF Format (*.pdf)", "" _
& "x:\Plan Holding Folder\" & Me.[AccountNameLbl] + (Format(Now(), " ddmmyy")) + " - " & "10 - N Max Farm Summary (Report 5).pdf"
'N Max Crop Group Summary (Report 4)
DoCmd.OutputTo acOutputReport, "rptNMaxComplianceByCropGroup", "PDF Format (*.pdf)", "" _
& "x:\Plan Holding Folder\" & Me.[AccountNameLbl] + (Format(Now(), " ddmmyy")) + " - " & "11 - N Max Crop Group Summary (Report 4).pdf"
'N Max N From Fertiliser Appln (Report 3)
DoCmd.OutputTo acOutputReport, "rptNFromFertforNMaxCalcs", "PDF Format (*.pdf)", "" _
& "x:\Plan Holding Folder\" & Me.[AccountNameLbl] + (Format(Now(), " ddmmyy")) + " - " & "12 - N Max N From Fertiliser Appln (Report 3).pdf"
'N Max N From Manure Applications (Report 2)
DoCmd.OutputTo acOutputReport, "rptCropNFromOMforNMaxCalcs", "PDF Format (*.pdf)", "" _
& "x:\Plan Holding Folder\" & Me.[AccountNameLbl] + (Format(Now(), " ddmmyy")) + " - " & "13 - N Max N From Manure Applications (Report 2).pdf"
'N Max Field Limits (Report 1)
DoCmd.OutputTo acOutputReport, "rptNMaxfieldlimits", "PDF Format (*.pdf)", "" _
& "x:\Plan Holding Folder\" & Me.[AccountNameLbl] + (Format(Now(), " ddmmyy")) + " - " & "14 - N Max Field Limits (Report 1).pdf"
'Fertiliser Stock Sheets
DoCmd.OutputTo acOutputReport, "rptStockSheet", "PDF Format (*.pdf)", "" _
& "x:\Plan Holding Folder\" & Me.[AccountNameLbl] + (Format(Now(), " ddmmyy")) + " - " & "15 - Fertiliser Stock Sheets.pdf"
'Soil Analysis Results for Plan
DoCmd.OutputTo acOutputReport, "rptSoilAnalRsltsForPlan", "PDF Format (*.pdf)", "" _
& "x:\Plan Holding Folder\" & Me.[AccountNameLbl] + (Format(Now(), " ddmmyy")) + " - " & "16 - Soil Analysis Results for Plan.pdf"
DoEvents
DoCmd.OpenForm "frmPleaseWait2"
Else
'Header
DoCmd.OutputTo acOutputReport, "rptMainNPlanHeader", "PDF Format (*.pdf)", "" _
& "x:\Plan Holding Folder\" & Me.[AccountNameLbl] + (Format(Now(), " ddmmyy")) + " - " & "1 - Header.pdf"
'PLAN
DoCmd.OutputTo acOutputReport, "rpt10-Plan09", "PDF Format (*.pdf)", "" _
& "x:\Plan Holding Folder\" & Me.[AccountNameLbl] + (Format(Now(), " ddmmyy")) + " - " & "2 - PLAN.pdf"
'Cropping Totals
DoCmd.OutputTo acOutputReport, "rptCropTotalsWithYieldsCT1", "PDF Format (*.pdf)", "" _
& "x:\Plan Holding Folder\" & Me.[AccountNameLbl] + (Format(Now(), " ddmmyy")) + " - " & "3 - Cropping Totals Report.pdf"
'Fertiliser Requirement with Field Details
DoCmd.OutputTo acOutputReport, "rptFertReqWithFieldDetail", "PDF Format (*.pdf)", "" _
& "x:\Plan Holding Folder\" & Me.[AccountNameLbl] + (Format(Now(), " ddmmyy")) + " - " & "4 - Fertiliser Requirements with Field Details.pdf"
'NVZ Nitrogen Planning Report
DoCmd.OutputTo acOutputReport, "rptPlanningNUse", "PDF Format (*.pdf)", "" _
& "x:\Plan Holding Folder\" & Me.[AccountNameLbl] + (Format(Now(), " ddmmyy")) + " - " & "5 - NVZ Nitrogen Planning Report.pdf"
'Fertiliser Application Charts
DoCmd.OutputTo acOutputReport, "rptFertApplnChartsJK1", "PDF Format (*.pdf)", "" _
& "x:\Plan Holding Folder\" & Me.[AccountNameLbl] + (Format(Now(), " ddmmyy")) + " - " & "6 - Fertiliser Application Charts.pdf"
'Manure Application Charts
DoCmd.OutputTo acOutputReport, "RptOMApplicationCharts", "PDF Format (*.pdf)", "" _
& "x:\Plan Holding Folder\" & Me.[AccountNameLbl] + (Format(Now(), " ddmmyy")) + " - " & "7 - Manure Application Charts.pdf"
'Total N from Organic Manure Applications
DoCmd.OutputTo acOutputReport, "rptTotalNFromOMApplns", "PDF Format (*.pdf)", "" _
& "x:\Plan Holding Folder\" & Me.[AccountNameLbl] + (Format(Now(), " ddmmyy")) + " - " & "8 - Total N from Organic Manure Applications.pdf"
'NVZ Soil Type Assessment
DoCmd.OutputTo acOutputReport, "rptNVZClosedPeriod", "PDF Format (*.pdf)", "" _
& "x:\Plan Holding Folder\" & Me.[AccountNameLbl] + (Format(Now(), " ddmmyy")) + " - " & "9 - NVZ Soil Type Assessment.pdf"
'N Max Farm Summary (Report 5)
DoCmd.OutputTo acOutputReport, "rptSummaryforNMaxCalcs", "PDF Format (*.pdf)", "" _
& "x:\Plan Holding Folder\" & Me.[AccountNameLbl] + (Format(Now(), " ddmmyy")) + " - " & "10 - N Max Farm Summary (Report 5).pdf"
'N Max Crop Group Summary (Report 4)
DoCmd.OutputTo acOutputReport, "rptNMaxComplianceByCropGroup", "PDF Format (*.pdf)", "" _
& "x:\Plan Holding Folder\" & Me.[AccountNameLbl] + (Format(Now(), " ddmmyy")) + " - " & "11 - N Max Crop Group Summary (Report 4).pdf"
'N Max N From Fertiliser Appln (Report 3)
DoCmd.OutputTo acOutputReport, "rptNFromFertforNMaxCalcs", "PDF Format (*.pdf)", "" _
& "x:\Plan Holding Folder\" & Me.[AccountNameLbl] + (Format(Now(), " ddmmyy")) + " - " & "12 - N Max N From Fertiliser Appln (Report 3).pdf"
'N Max N From Manure Applications (Report 2)
DoCmd.OutputTo acOutputReport, "rptCropNFromOMforNMaxCalcs", "PDF Format (*.pdf)", "" _
& "x:\Plan Holding Folder\" & Me.[AccountNameLbl] + (Format(Now(), " ddmmyy")) + " - " & "13 - N Max N From Manure Applications (Report 2).pdf"
'N Max Field Limits (Report 1)
DoCmd.OutputTo acOutputReport, "rptNMaxfieldlimits", "PDF Format (*.pdf)", "" _
& "x:\Plan Holding Folder\" & Me.[AccountNameLbl] + (Format(Now(), " ddmmyy")) + " - " & "14 - N Max Field Limits (Report 1).pdf"
'Fertiliser Stock Sheets
DoCmd.OutputTo acOutputReport, "rptStockSheet", "PDF Format (*.pdf)", "" _
& "x:\Plan Holding Folder\" & Me.[AccountNameLbl] + (Format(Now(), " ddmmyy")) + " - " & "15 - Fertiliser Stock Sheets.pdf"
'Soil Analysis Results for Plan
DoCmd.OutputTo acOutputReport, "rptSoilAnalRsltsForNutriPlan", "PDF Format (*.pdf)", "" _
& "x:\Plan Holding Folder\" & Me.[AccountNameLbl] + (Format(Now(), " ddmmyy")) + " - " & "16 - Soil Analysis Results for Plan.pdf"
DoEvents
DoCmd.OpenForm "frmPleaseWait2"
End If
End If
End Sub