Why 2501? Why (1 Viewer)

Chillendw

Registered User.
Local time
Yesterday, 22:14
Joined
Mar 1, 2017
Messages
38
So I've searched and tried different codes to prevent this error from popping up. I was able to get the pop-up msg to display, but then I keep getting 2501.

Basically: I have a report that I call upon via a form that is used by a query to populate the report.

On the report NoData Event:
Code:
Private Sub Report_NoData(Cancel As Integer)
MsgBox "No data to Display"
Cancel = True
End Sub

I think that's fine. The issue comes on the button in the form. I've tried 2 different codes:

Code:
Private Sub btnReport_Click()
   DoCmd.OpenReport "rptByLocations", acViewPreview
If Err.Number <> 2501 Then
   MsgBox Err.Description, vbExclamation, "Error: " & Err.Number
   Resume Next
End If

That didn't work so I tried:

Code:
Private Sub btnReport_Click()
   DoCmd.OpenReport "rptByLocations", acViewPreview
On Error GoTo ErrorHandler

ErrorHandler:
  Select Case Err  
    Case 2501       
      MsgBox "No data to display"
      DoCmd.Hourglass False
    Case Else
      MsgBox Err.Description
      DoCmd.Hourglass False
  End Select
End Sub

Both of them still show the 2501 error after the msg pop-up.

Where is the issue and which one would be better to use?

Thanks in advance.
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 19:14
Joined
Aug 30, 2003
Messages
36,124
In the second, delete the message box line for the 2501 case.
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 19:14
Joined
Aug 30, 2003
Messages
36,124
Oh, and the on error line should be first.
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 19:14
Joined
Aug 30, 2003
Messages
36,124
Happy to help!
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 03:14
Joined
Sep 12, 2006
Messages
15,634
I can't see why the first error handler didn't work.

If the report doesn't open after the button click, you should get a 2501 error, which will then be dismissed.
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 19:14
Joined
Aug 30, 2003
Messages
36,124
It's not really an error handler, there's no "on error..." line. The OpenReport line would error and then it would move to the test, too late.
 

Chillendw

Registered User.
Local time
Yesterday, 22:14
Joined
Mar 1, 2017
Messages
38
So there's no more 2501 error pop-up. But now there is a blank pop-up that comes up even when I do have a report available. I've attached a pic so you know what I mean.
And here's the code on the button to get the report:
Code:
Private Sub btnReport_Click()
On Error GoTo ErrorHandler
DoCmd.OpenReport "rptByPatient", acViewPreview

ErrorHandler:
  Select Case Err
    Case 2501
      DoCmd.Hourglass False
    Case Else
      MsgBox Err.Description
      DoCmd.Hourglass False
  End Select
End Sub

Is it the Case Else with the MsgBox?

Also, if I want to close the form, I believe it would be
Code:
DoCmd.Close acForm, frmRptByPatients
Correct?

But would it be possible to only close the form if there is data, but keep it open in the case that there is no data? And is there a specific place in the code that I'd put it?

Thanks in advance.
 

Attachments

  • Screenshot (5883).png
    Screenshot (5883).png
    68.8 KB · Views: 106
Last edited:

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 19:14
Joined
Aug 30, 2003
Messages
36,124
You need the exit handler:

http://www.baldyweb.com/ErrorTrap.htm

without it, code just drops into the error handler. If you put the close form code after the open report, it should only fire when the report has records.
 

Chillendw

Registered User.
Local time
Yesterday, 22:14
Joined
Mar 1, 2017
Messages
38
Thanks a lot pbaldy.

I was hoping you could explain what exactly that little bit of code does. That way I actually understand it, and know why I'm doing it.
Code:
ExitHandler:  
  Set rs = Nothing  
  Set db = Nothing 
  Exit Sub

Thanks!
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 19:14
Joined
Aug 30, 2003
Messages
36,124
Code goes where it's told to go, otherwise it just marches down the sub. In your original code, there was nothing to stop it from proceeding into the error handler, and since there was no error it hit the Else and gave you the empty message box. You could have just put:

Exit Sub

after opening the report, but normally error handling includes the exit handler portion. Though in your case there was none, often there is "clean up" code that is put in the exit handler, as in my example.
 

Users who are viewing this thread

Top Bottom