D
Dave
I've read many threads on this error, and after hours of trying, I
can't seem to trap this run time error. Please excuse the length of
the following, but I think it's necessary under the circumstances.
This is the process...
A button on a report selector form opens a date picker form whose tag
property is set to the report name. Choose date parameters then click
'open report' button and pass report name to public function to open
that report. The report's 'no data' event passes the report name and
report caption to another public function that creates msgbox "There's
no data in report caption (or report name if there's no caption)".
This function has a docmd.CancelEvent line, otherwise the msgbox runs
twice because the nodata event runs twice. I have put 2501 error traps
in the sub that calls the open report function, the open report
function itself, the nodata event and the msgbox function, yet none of
them will catch it. Stepping through the code, I see the message is
generated at the last executed line of the nodata event. If I choose
'debug' it goes to the open report function and highlights the first
line. No doubt this cannot be resolved without a look at the code
(line spaces removed within to shorten). I'm so tired now that this is
bound to be something stupid I've overlooked. TIA!
Private Sub cmdOpenReport_Click()
Dim rpt As String
On Error GoTo errHandler 'does not trap cancel error
rpt = Me.tag
OpenReport (rpt)
errHandler:
If Err = 2501 Then Exit Sub
MsgBox "Error Number " & Err.Number & ": " & Err.Description
End Sub
Public Function OpenReport(rpt As String)
On Error GoTo errHandler 'does not trap cancel error
DoCmd.OpenReport (rpt), acViewPreview
Exit Function
errHandler:
If Err = 2501 Then Exit Function
MsgBox "Error " & Err.Number & ": " & Err.Description
End Function
Private Sub Report_Open(cancel As Integer)
DoCmd.Maximize
End Sub
Private Sub Report_NoData(cancel As Integer)
On Error GoTo errHandler 'does not trap cancel error
rptName = Me.Name
rptCaption = Me.Caption
NoData rptName, rptCaption
Exit Sub 'THIS IS THE LAST LINE EXECUTED BEFORE SYSTEM MESSAGE
errHandler:
If Err = 2501 Then Exit Sub
MsgBox "Error Number " & Err.Number & ": " & Err.Description
End Sub
Public Function NoData(rptName, rptCaption)
On Error GoTo errHandler 'does not trap cancel error
If rptCaption = vbNullString Then
rptCaption = rptName
End If
DoCmd.CancelEvent
MsgBox "There are no records in report """ & rptCaption & """.",
vbOKOnly, "No Records"
Exit Function
errHandler:
If Err = 2501 Then Exit Function
MsgBox "Error Number " & Err.Number & ": " & Err.Description
End Function
can't seem to trap this run time error. Please excuse the length of
the following, but I think it's necessary under the circumstances.
This is the process...
A button on a report selector form opens a date picker form whose tag
property is set to the report name. Choose date parameters then click
'open report' button and pass report name to public function to open
that report. The report's 'no data' event passes the report name and
report caption to another public function that creates msgbox "There's
no data in report caption (or report name if there's no caption)".
This function has a docmd.CancelEvent line, otherwise the msgbox runs
twice because the nodata event runs twice. I have put 2501 error traps
in the sub that calls the open report function, the open report
function itself, the nodata event and the msgbox function, yet none of
them will catch it. Stepping through the code, I see the message is
generated at the last executed line of the nodata event. If I choose
'debug' it goes to the open report function and highlights the first
line. No doubt this cannot be resolved without a look at the code
(line spaces removed within to shorten). I'm so tired now that this is
bound to be something stupid I've overlooked. TIA!
Private Sub cmdOpenReport_Click()
Dim rpt As String
On Error GoTo errHandler 'does not trap cancel error
rpt = Me.tag
OpenReport (rpt)
errHandler:
If Err = 2501 Then Exit Sub
MsgBox "Error Number " & Err.Number & ": " & Err.Description
End Sub
Public Function OpenReport(rpt As String)
On Error GoTo errHandler 'does not trap cancel error
DoCmd.OpenReport (rpt), acViewPreview
Exit Function
errHandler:
If Err = 2501 Then Exit Function
MsgBox "Error " & Err.Number & ": " & Err.Description
End Function
Private Sub Report_Open(cancel As Integer)
DoCmd.Maximize
End Sub
Private Sub Report_NoData(cancel As Integer)
On Error GoTo errHandler 'does not trap cancel error
rptName = Me.Name
rptCaption = Me.Caption
NoData rptName, rptCaption
Exit Sub 'THIS IS THE LAST LINE EXECUTED BEFORE SYSTEM MESSAGE
errHandler:
If Err = 2501 Then Exit Sub
MsgBox "Error Number " & Err.Number & ": " & Err.Description
End Sub
Public Function NoData(rptName, rptCaption)
On Error GoTo errHandler 'does not trap cancel error
If rptCaption = vbNullString Then
rptCaption = rptName
End If
DoCmd.CancelEvent
MsgBox "There are no records in report """ & rptCaption & """.",
vbOKOnly, "No Records"
Exit Function
errHandler:
If Err = 2501 Then Exit Function
MsgBox "Error Number " & Err.Number & ": " & Err.Description
End Function