A.D.TEJPAL
adtp at hotmail.com
Thu May 24 00:58:46 CDT 2007
Debbie,
Sample subroutine P_OpenReport(), as given below, called from command button's click event in form's module, can be used universally for opening all your reports. It also takes care of NoData situation, providing a friendly message. It does away with the need for separate code (for message generation) in report's module. Simply put the following statement (nothing else) in report's NoData event:
Cancel = True
Best wishes,
A.D.Tejpal
---------------
Sample subroutine for opening reports
======================================
Sub P_OpenReport(ByVal RepName As Variant, _
Optional StrCriteria As Variant)
On Error GoTo ErrTrap
If Len(RepName) > 0 Then
If IsMissing(StrCriteria) Then
DoCmd.OpenReport RepName, _
acViewPreview
Else
If Len(StrCriteria) > 0 Then
DoCmd.OpenReport RepName, _
acViewPreview, , StrCriteria
Else
DoCmd.OpenReport RepName, _
acViewPreview
End If
End If
DoCmd.Maximize ' (A)
DoCmd.RunCommand acCmdZoom100 ' (B)
End If
ExitPoint:
On Error GoTo 0
Exit Sub
ErrTrap:
' Spl message for NoData (Error 2501)
' Otherwise, normal error message
If Err.Number = 2501 Then
MsgBox "No Matching Record For " & _
"Report " & RepName, vbOKOnly, _
"No Matching Record"
Else
MsgBox Err.Number & " - " & _
Err.Description, vbCritical + vbOKOnly, _
"Error Encountered"
End If
Resume ExitPoint
' Note - (a) In case of NoData, statements (A) &
' (B) do not get executed. Hence existing
' restored status of the calling form remains
' undisturbed.
' (b) Data type of RepName argument has been
' kept as Variant, to provide for values gathered
' from form controls (text / combo boxes)
End Sub
======================================
----- Original Message -----
From: Elam, Debbie
To: 'Access Developers discussion and problem solving'
Sent: Wednesday, May 23, 2007 22:05
Subject: [AccessD] OnNoData error
I am running a series of reports off of one command button. Some of these may not have data, so I have used the OnNoData event to cancel that particular report and send a message box to the user that there was no data in that one.
The problem I am encountering is the standard 2501 error will simply not be suppressed. I have used
If err.number = 2501 then
resume next
Else
msgbox err.desctiption
resume ExitTheSub
End if
In the on error of the button that runs the reports. Heck I have even set all errors to resume next, but I still get the error and no resume next.
I could even live with a harmless error message, but once this happens, all subsequent reports do not run. That is the real problem.
Debbie