Open report if no data

GetReel

Registered User.
Local time
Today, 09:04
Joined
May 22, 2003
Messages
67
I have a button that opens a report and in the report I have added a message if there is no records.

How do turn off the message that is produced by access. Is an error handler involved, if so, how and where is it placed to stop the message popping up after the On_Nodata is run.

'button on Main form
On Error GoTo Err_Letters_Click
Dim stDocName As String
stDocName = "rpt4Week"
DoCmd.OpenReport stDocName, acPreview
Exit_Letters_Click:
Exit Sub
Err_Letters_Click:
MsgBox Err.Description
Resume Exit_Letters_Click

Private Sub Report_NoData(Cancel As Integer)
MsgBox "There are no Tenants that require the 4 week letter", vbOKOnly
Cancel = True
End Sub
 
I'm thinking you mean the message from Access which says something along the lines of 'The OpenReport action was cancelled'.

If so, it's because in your Button_Click event you've got an Error Handler. Either comment the MsgBox Err.Description or trap the error number of the error you're receiving, 2501, I think!
 
Yes thats exactly the error message im getting.

Im not sure how to trap the error message.

I'll have quick look thru some post to see if I have a go myself first. If I cant figure it out, l'll get back to the forum.
 
You need to trap for the runtime error # 2501 in the on click event of your button that is calling the opening of your report. Like this...
Code:
Private Sub YourButton_Click()
On Error GoTo YourButton_Click_Err
    
    'your code here
    
YourButton_Click_Exit:
    Exit Sub
    
YourButton_Click_Err:
    If Err = 2501 Then 'The RunCommand action was canceled
        Exit Sub
    Else
        MsgBox Err.Number & " - " & Err.Description
        Resume YourButton_Click_Exit
    End If
    
End Sub
 
You should use error trapping in all your subs and functions. Your reports NoDate event should look like this...
Code:
Private Sub Report_NoData(Cancel As Integer)
On Error GoTo Report_NoData_Err
    
    MsgBox "Your request has no data to print." & vbCrLf & vbLf & "Your print request has been canceled.", vbOKOnly + vbInformation
    Cancel = True
    
Report_NoData_Exit:
    Exit Sub
    
Report_NoData_Err:
    MsgBox Err.Number & " - " & Err.Description
    Resume Report_NoData_Exit
    
End Sub
Your method will work but this more efficient error trapping [for the unexpected]. IMHO
 
Thanks for that Hudson

Im getting a label not defined error

Private Sub Command118_Click()
On Error GoTo Command118_Click_Err

Dim stDocName As String

stDocName = "rpt4Week"
DoCmd.OpenReport stDocName, acPreview

Exit_Command118_Click:
Exit Sub


Command118_Click_Err:
If Err = 2501 Then 'The RunCommand action was canceled
Exit Sub
Else
MsgBox Err.Number & " - " & Err.Description
Resume Command118_Click_Exit
End If

Am I missing something ?
 
Last edited:
You have this Exit_Command118_Click: yet you are calling it with Resume Command118_Click_Exit. See the difference?

Tip of the day...use the code tags and tabs and line spaces. It makes posted code so much easier to read.
 
Thanks Hudson,

Works now!

it now reads Resume Command118_Click_Err


Thanks so much. :-)
 

Users who are viewing this thread

Back
Top Bottom