No data report

T

Tony Williams

I have a macro that runs if there is no data for a report that is based on a
macro. The macro is on the OnNodata property of the report and pops up a
message to the user that there sre no records, but when I click on the OK
button of the message I get an error message that says
Runtime error 2501
The OpenReportaction was cancelled

On pressing debug it takes me to the Docmd OpenReport line at the end of the
code which is behind a command button on my report prompt form. Here is my
code
Private Sub cmdOK_Click()
Dim strReport As String 'Name of report to open.
Dim strField As String 'Name of your date field.
Dim strWhere As String 'Where condition for OpenReport.

Const conDateFormat = "\#mmmm\/yyyy\#"

strReport = "rptsense3"
strField = "txtMonthlabel"


If Not IsNull(txtMonthlabel) Then
If IsNull(Me.txtstartdate) Then
MsgBox "You must enter a start date", vbOKOnly, "Missing Start Date"
Me.txtstartdate.SetFocus
Else

If IsNull(Me.txtenddate) Then
MsgBox "You must enter an end date", vbOKOnly, "Missing End Date"
Me.txtenddate.SetFocus
Else
strWhere = strField & " Between " & Format(Me.txtstartdate,
conDateFormat) _
& " And " & Format(Me.txtenddate, conDateFormat)


If Not IsNull(Me.cmbselectcompany) Then
strWhere = strWhere & " AND cmbCompany = """ & Me.cmbselectcompany & """"
End If


DoCmd.OpenReport strReport, acViewPreview, , strWhere
End If
End If
End If

End Sub

How can I change my code so that I don't get this message
 
D

Duane Hookom

Try something like:

Private Sub cmdOK_Click()
On Error GoTo errCmdOK
Dim strReport As String 'Name of report to open.
Dim strField As String 'Name of your date field.
Dim strWhere As String 'Where condition for OpenReport.
' other lines cut

DoCmd.OpenReport strReport, acViewPreview, , strWhere
'more lines cut
exitCmdOK:
Exit Sub

errCmdOK:
Select Case Err.Number
Case 2501
' Do nothing
Case Else
MsgBox "Error Number: " & err.Number & vbCrLf & _
"Description: " & err.Description
End Select
Resume exitCmdOK
End Sub
 
T

Tony Williams

Thanks Duane
Tony
Duane Hookom said:
Try something like:

Private Sub cmdOK_Click()
On Error GoTo errCmdOK
Dim strReport As String 'Name of report to open.
Dim strField As String 'Name of your date field.
Dim strWhere As String 'Where condition for OpenReport.
' other lines cut

DoCmd.OpenReport strReport, acViewPreview, , strWhere
'more lines cut
exitCmdOK:
Exit Sub

errCmdOK:
Select Case Err.Number
Case 2501
' Do nothing
Case Else
MsgBox "Error Number: " & err.Number & vbCrLf & _
"Description: " & err.Description
End Select
Resume exitCmdOK
End Sub
 

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