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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Opening report from switchboard when no data 1

Status
Not open for further replies.

TrekBiker

Technical User
Nov 26, 2010
330
GB
This looks like an old chestnut but my searches for an answer have failed.

A report opened from the Switchboard has this for no data

Code:
Private Sub Report_NoData(Cancel As Integer)
    MsgBox "There is no data", , "No Data"
    Cancel = True
End Sub

It flags up the warning but then brings up the standard Microsoft Visual Basic 2501 run-time error message with End and Debug options, not good for users.

If I take out Cancel = True a blank report opens, which is marginally better.

How do I just abort opening the report after the 'There is no data' warning?

Being driven from a Switchboard the HandleButtonClick event covers all sort of possible responses - open query, form, report, etc. On stepping through the code it never seems to get to its error handler for the no data case.
 
You need to trap for this error in the code that opens the report (I assume in your switchboard).

Duane
Minnesota
Hook'D on Access
MS Access MVP 2001-2016
 
Thanks Duane but need some more guidance.

The Switchboard code starts like this

Code:
Private Function HandleButtonClick(intBtn As Integer)
    Const conCmdGotoSwitchboard = 1
    Const conCmdOpenFormAdd = 2
    Const conCmdOpenFormBrowse = 3
    Const conCmdOpenReport = 4
    Const conCmdCustomizeSwitchboard = 5
    Const conCmdExitApplication = 6
    Const conCmdRunMacro = 7
    Const conCmdRunCode = 8
    Const conCmdOpenPage = 9

    Const conErrDoCmdCancelled = 2501
    
    Dim con As Object
    Dim rs As Object
    Dim stSql As String

On Error GoTo HandleButtonClick_Err

It then continues past this for the report in question

Code:
' Open a report.
    Case conCmdOpenReport
    DoCmd.OpenReport rs![Argument], acPreview

... and ends with this

Code:
HandleButtonClick_Exit:
On Error Resume Next
    Set rs = Nothing
    Set con = Nothing
    Exit Function

HandleButtonClick_Err:
    If (Err = conErrDoCmdCancelled) Then
        Resume Next
    Else
        MsgBox "There was an error executing the command.", vbCritical
        Resume HandleButtonClick_Exit
    End If
End Function

When I step through the code it never gets to the error handling section but fails on trying to open the report, which has this for No Data

Code:
Private Sub Report_NoData(Cancel As Integer)
    MsgBox "No records", , "No Data"
    Cancel = True
End Sub

Where should I put the extra trapping you suggest?


 
I would expect your error handling to manage and hide the error. Have you set a break point and stepped through the code?

Duane
Minnesota
Hook'D on Access
MS Access MVP 2001-2016
 
Yes, first thing I did.

On stepping through to the point where it wants to open the report,

Code:
DoCmd.OpenReport rs![Argument], acPreview

the report's No Data message box appears, and I can step through it

Code:
Private Sub Report_NoData(Cancel As Integer)
    MsgBox "There is no data", , "No Data"
    Cancel = True
End Sub

The next5 step after End Sub makes this appear

Fail_r4ti5u.jpg
 
I would check your VBA Tools->Options->General to make sure you haven't set Break on All Errors.

BreakOnError_tcpal8.jpg


Duane
Minnesota
Hook'D on Access
MS Access MVP 2001-2016
 
Thanks for giving this more time. But no, the Error Trapping is like this

Break_fbb7tm.jpg
 
Sorry Duane, you're right, it was set to Break on All errors, as in the screen shot. I'll check later if this fixes it.
 
Can confirm it handles the error properly, so many thanks once again.
 
The "Break on All Errors" has bit me many times. I will set it while testing and forget to change it back.

Duane
Minnesota
Hook'D on Access
MS Access MVP 2001-2016
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top