By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,491 Members | 854 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,491 IT Pros & Developers. It's quick & easy.

Don't Want Report to Open if Message Box Appears Saying No Data for Report

P: 2
I'm trying to stop a report from opening after a message box tells user that no data is available and return to the previous form
Dec 8 '17 #1

✓ answered by PhilOfWalton

Try
Expand|Select|Wrap|Line Numbers
  1. Private Sub Report_NoData (Cancel As Integer)
  2.   MsgBox "There are no records", vbExclamation, "No Records"
  3.   Cancel = True
  4. End Sub
  5.  
Phil

Share this Question
Share on Google+
4 Replies


PhilOfWalton
Expert 100+
P: 1,430
Try
Expand|Select|Wrap|Line Numbers
  1. Private Sub Report_NoData (Cancel As Integer)
  2.   MsgBox "There are no records", vbExclamation, "No Records"
  3.   Cancel = True
  4. End Sub
  5.  
Phil
Dec 8 '17 #2

NeoPa
Expert Mod 15k+
P: 31,186
It's worth pointing out that the code that calls the report to open will error when this is used.

It's the right way to do it but just bear in mind you also need to handle the error in the calling code unless you want it to crash.
Dec 8 '17 #3

P: 2
yes this worked but as NeoPa said now I have a error on clicking ok
Dec 13 '17 #4

PhilOfWalton
Expert 100+
P: 1,430
So you need to trap the Error.

Try something like this on the button that opens the report
Expand|Select|Wrap|Line Numbers
  1. Sub MyButton_Click
  2.  
  3.     On Error Goto Report_Open_Err
  4.  
  5.     DoCmd.OpenReport MyReport, acPreview
  6.  
  7. Report_Open_Exit:
  8.     Exit Sub
  9.  
  10.  Report_Open_Err:
  11.     If Err = 2501 Or Err = 3420 Then          ' Open Report Cancelled or form opened
  12.         Exit Sub
  13.     Else
  14.         Msgbox Err & "   " & Err.Description
  15.     End If
  16.  
  17. End Sub
  18.  
Phil
Dec 13 '17 #5

Post your reply

Sign in to post your reply or Sign up for a free account.