Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help on trapping error 2501 2

Status
Not open for further replies.

NeilT123

Technical User
Jan 6, 2005
302
GB
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:
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
 
Read this for help

You need to trap the error and if it is 2501 then resume next (try to print the next one)

Code:
Public Sub someSub
  'Need to add a statement to go to a label 
  On Error GoTo ErrHandler
      ' Your code here
     'Need to add exit so you do not enter the errhandler code
       Exit sub 
ErrHandler:
  If Err.Number = 2501 Then
    Resume Next
  Else
     MsgBox Err.Number & " " & Err.Description
  end if
end sub
 
Also instead of trapping an error you always want to avoid the error even if it is simpler to just trap it. If the reports are based off of stored queries then you can do a dcount on the stored query to check if the record count is greater than zero. If the report recordsource is only in the report then you may be able to create some queries that you can check ahead of time for records prior to calling the report.
 
That works. When I get a chance I will have a look at MajP's suggestion to avoid the error rather than trap it.

Thank you both for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top