Error Handling (1 Viewer)

Endojoe

Registered User.
Local time
Today, 09:34
Joined
Apr 7, 2009
Messages
20
I have 3 reports I want to print from a single command button. I have achieved this using the following:

Code:
Private Sub All_Reports_Button_Click()
On Error GoTo Err_All_Reports_Button_Click

    Dim stDocName As String

    stDocName = "CofC_SPU_ALL"
    DoCmd.OpenReport stDocName, acNormal
    stDocName = "8130_Domestic"
    DoCmd.OpenReport stDocName, acNormal
    stDocName = "8130_Export"
    DoCmd.OpenReport stDocName, acNormal

Exit_All_Reports_Button_Click:
    Exit Sub

Err_All_Reports_Button_Click:
    MsgBox Err.DESCRIPTION
    Resume Exit_All_Reports_Button_Click
    
End Sub


I have configured each report to not print if there's no data in its corresponding Print Query using the following code in the 'On No Data' Event of each report.

Code:
Option Compare Database

Private Sub Report_NoData(Cancel As Integer)

MsgBox "There Are No Domestic 8130s Checked To Print", _
    vbInformation, "You Gone And Screwed Up Now..."

Cancel = True

End Sub


All is well so long as there's actually a report in each of the 3 corresponding Print Querys. The MsgBox Error Message is only there because there are currently 3 seperate command buttons for printing. It is not critical that the user be notified in the event that any one of the 3 reports has no records to be printed. If the first or second report has no records to print, the error message prevents the rest of the code from running. So, basically, I'm looking for a way to supress or eliminate the error message from effecting the code, most likely with some kind of error handling statement between each print command...but as simple as what I've done is, I'm already in over my head. I've found this website to be immense repository of information over the last several months, and learned a great deal from it, but for this specific instance I couldn't seem to find a previously discussed simple solution.

Thank You All In Advance,

-Will-
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 07:34
Joined
Aug 30, 2003
Messages
36,124
What I would do is add a Select/Case statement to your error trap. If the error is 2501, resume next, otherwise message box and resume at the exit point as you have now.
 

Endojoe

Registered User.
Local time
Today, 09:34
Joined
Apr 7, 2009
Messages
20
Paul, thank you so much for setting me on the right path!! Worked like a charm. :D Like your title too... ;) Great Book.

For anyone who may find this helpful later...here's the code that worked (the part Paul kindly directed me to is in Bold)...this also allowed me to keep the Message that there were no reports to print in the instances that there aren't, which I kind of hoped I'd be able to keep.

Code:
Private Sub All_Reports_Button_Click()
On Error GoTo Err_All_Reports_Button_Click

    Dim stDocName As String

    stDocName = "CofC_SPU_ALL"
    DoCmd.OpenReport stDocName, acNormal
    stDocName = "8130_Domestic"
    DoCmd.OpenReport stDocName, acNormal
    stDocName = "8130_Export"
    DoCmd.OpenReport stDocName, acNormal

Exit_All_Reports_Button_Click:
    Exit Sub

[B]Err_All_Reports_Button_Click:
Select Case Err.Number
Case 2501
Resume Next
Case Else
    MsgBox Err.DESCRIPTION
    Resume Exit_All_Reports_Button_Click
End Select[/B]
    
End Sub
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 07:34
Joined
Aug 30, 2003
Messages
36,124
Glad it helped, Will, and welcome to the site by the way!
 

Users who are viewing this thread

Top Bottom