An error message received after a NoData message

G

Guest

I do not understand why I have an error message on a NoData message based on
a report. I have a form which has a combo box to choose the parameter of a
report. Once you choose the parameter from the list, you click on OK and it
runs the report. If there is no data for the report, you get a message box
stating there are no records. Every time I run the report with no records, I
get the message box, but it also gives me an error.

The coding for the OK button is as follows:
Private Sub OK_btn_Click()
Me.Visible = False
DoCmd.OpenReport "Program Participation for Volunteers - by Program",
acViewPreview
DoCmd.close acForm, "Entry Form for Program Participation by Volunteers
Report"
End Sub

The coding for the NoData message for the report is as follows:
Private Sub Report_NoData(Cancel As Integer)
On Error GoTo ErrHandler
MsgBox "Sorry. There are no records" & vbCrLf & vbCrLf & "to display
or this report.", vbInformation + vbOKOnly, " No Records"
Cancel = True
Exit Sub
DoCmd.close acReport, "Program Participation for Volunteers - by Program"
DoCmd.close acForm, "Entry Form for Program Participation by Volunteers
Report"
ErrHandler:
MsgBox "Error in Report_NoData()" & vbCrLf & "in " & Me.Name & "
report." & vbCrLf & vbCrLf & Err.Number & vbCrLf & Err.Description
End Sub

If you run the report for a parameter with no records, you get a message box.
The heading says “No Recordsâ€
The message reads “Sorry, there are no records to display for this report.â€
and has an OK button.
If you click on the OK button you get another pop-up which says:
“Run-time error ‘2501’:
The OpenReport action was canceled.â€
And has End, Debug, and Help buttons.

If you click on the End button, the message box disappears.
If you click on the Debug button, you are brought to Visual Basic editor
under the OK_btn_Click() Private Sub with the 2nd line of code highlighted
yellow (DoCmd.OpenReport "Program Participation for Volunteers - by Program",
acViewPreview).

I don’t understand why this is happening. I cannot keep having this error
keep popping up, but would like to keep the NoData error message when you run
the report.
 
F

fredg

I do not understand why I have an error message on a NoData message based on
a report. I have a form which has a combo box to choose the parameter of a
report. Once you choose the parameter from the list, you click on OK and it
runs the report. If there is no data for the report, you get a message box
stating there are no records. Every time I run the report with no records, I
get the message box, but it also gives me an error.

The coding for the OK button is as follows:
Private Sub OK_btn_Click()
Me.Visible = False
DoCmd.OpenReport "Program Participation for Volunteers - by Program",
acViewPreview
DoCmd.close acForm, "Entry Form for Program Participation by Volunteers
Report"
End Sub

The coding for the NoData message for the report is as follows:
Private Sub Report_NoData(Cancel As Integer)
On Error GoTo ErrHandler
MsgBox "Sorry. There are no records" & vbCrLf & vbCrLf & "to display
or this report.", vbInformation + vbOKOnly, " No Records"
Cancel = True
Exit Sub
DoCmd.close acReport, "Program Participation for Volunteers - by Program"
DoCmd.close acForm, "Entry Form for Program Participation by Volunteers
Report"
ErrHandler:
MsgBox "Error in Report_NoData()" & vbCrLf & "in " & Me.Name & "
report." & vbCrLf & vbCrLf & Err.Number & vbCrLf & Err.Description
End Sub

If you run the report for a parameter with no records, you get a message box.
The heading says ¡§No Records¡¨
The message reads ¡§Sorry, there are no records to display for this report.¡¨
and has an OK button.
If you click on the OK button you get another pop-up which says:
¡§Run-time error ¡¥2501¡¦:
The OpenReport action was canceled.¡¨
And has End, Debug, and Help buttons.

If you click on the End button, the message box disappears.
If you click on the Debug button, you are brought to Visual Basic editor
under the OK_btn_Click() Private Sub with the 2nd line of code highlighted
yellow (DoCmd.OpenReport "Program Participation for Volunteers - by Program",
acViewPreview).

I don¡¦t understand why this is happening. I cannot keep having this error
keep popping up, but would like to keep the NoData error message when you run
the report.

You need to trap the error in the code that is used to open the
report, not in the report's OnNoData event:

I'm guessing that the name of the form on which this OK_btn code is
run is "Entry Form for Program Participation by Volunteers Report".
If so:

Private Sub OK_btn_Click()
On Error GoTo Err_Handler

Me.Visible = False
DoCmd.OpenReport "Program Participation for Volunteers - by Program",
acViewPreview

Exit_This_Sub:
Exit Sub
Err_Handler:
If Err = 2501 then
Else
MsgBox "Error# " & Err.Number & " " & Err.Description
End If
Resume Exit_This_Sub
End Sub

Then code the report's OnNoData event:

Private Sub Report_NoData(Cancel As Integer)
MsgBox "Sorry. There are no records" & vbCrLf & vbCrLf & "to display
or this report.", vbInformation + vbOKOnly, " No Records"
Cancel = True
Exit Sub

Code the report's Close event:
DoCmd.Close acForm, "Entry Form for Program Participation by
Volunteers Report"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top