Need help with No Data event

G

Guest

My report is not reacting the way it used to when asked for data that isn't
there. The no data event used to work, but now it is tossing an error 2051.
this is what i have for my command button on my form that opens the report

Private Sub cmdOpen_Click()
On Error GoTo Err_cmdOpen_Click
Dim stDocName As String
stDocName = "rptDailyProductionNew"
DoCmd.OpenReport stDocName, acViewPreview
Exit_cmdOpen_Click:
Exit Sub
Err_cmdOpen_Click:
MsgBox Err.Description
Resume Exit_cmdOpen_Click
End Sub

and this is what i have for my no data event in my report

Private Sub Report_NoData(Cancel As Integer)
On Error GoTo ErrHandler
MsgBox "Sorry. There are no records" & vbCrLf & vbCrLf & _
"to display for this report.", vbInformation + vbOKOnly, _
" No Records"
Cancel = True
Exit Sub
ErrHandler:
MsgBox "Error in Report_NoData( )" & vbCrLf & _
"in " & Me.Name & " report." & vbCrLf & vbCrLf & _
Err.Number & vbCrLf & Err.Description
End Sub

like i said this used to work, and i use this for other reports in my
database and they work without problems, so i'm not seeing what the problem
is. any help would be greatly appreciated.
 
G

Guest

Sorry, been out of the office for awhile.

was wondering if this makes any difference, as i have entered your code and
even downloaded and looked at the the code from the example on your website
and still having problems. anyways, the code that i have follows,

Private Sub cmdOpen_Click()
On Error GoTo Err_cmdOpen_Click
Dim stDocName As String
stDocName = "rptDailyProductionNew"
DoCmd.OpenReport stDocName, acViewPreview
Exit_cmdOpen_Click:
Exit Sub
Err_cmdOpen_Click:
If Err.Number = 2501 Then
Resume Next
Else
MsgBox Err.Description
Resume Exit_cmdOpen_Click
End If
End Sub

and for the on no data event of the report

Private Sub Report_NoData(Cancel As Integer)
MsgBox "There is no data for this report"
Cancel = True
End Sub

it's probably something simple that i am missing, but i'm just not seeing
it. any help would be appreciated. thanks.
 
R

Roger Carlson

The error number in the code is 2501, but the error that's being thrown is
2051. Change the code to trap error 2051. That should do it.

(The really bizzare thing is that I have 2501 in my sample and it still
works. I'll have to track down what the deal is. Sorry.)

--
--Roger Carlson
Access Database Samples: www.rogersaccesslibrary.com
Want answers to your Access questions in your Email?
Free subscription:
http://peach.ease.lsoft.com/scripts/wa.exe?SUBED1=ACCESS-L
 
G

Guest

I've double checked it and the exact error is:

Run-time error '2501':
The OpenReport action was canceled.

with 'End' and 'Debug' buttons available.

when i step through the code, the error is being thrown on the 'End Sub'
line below:

Private Sub Report_NoData(Cancel As Integer)
MsgBox "There is no data for this report"
Cancel = True
End Sub

when i press the 'Debug' button, i am taken to the
DoCmd.OpenReport stDocName, acViewPreview
line in the cmdOpen_click event.

i did try changing the error code to trap for, but the error is never
entering the trapping section of the cmdOpen_click event.
 
G

Guest

yes, should this be set to 'Break on unhandled errors'? or 'Break in class
module'?
 
G

Guest

nevermind my previous post, i changed it to the 'Break on unhandled errors'
and now it works. thanks duane and Roger for your help.
 

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