473,420 Members | 4,460 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,420 software developers and data experts.

Stop a report from running if query is empty

kcdoell
230 100+
Hello:

What is the best way to stop a report from running if the query is empty? Currently, I have a form that has a command button on it. The user has to make selections from 3 combo boxes on the form and then via the cmdbutton the report opens in preview mode. I want to stop it from executing if the query is empty. I thought I could do this by checking the record count by the following code but I error out with the following message:
Expand|Select|Wrap|Line Numbers
  1. Too few parameters. Expected <number>. (Error 3061)
  2. This parameter query requires the specified number of parameters, which you did not supply. Provide the expected number of parameters, and run the query again. 
  3.  
Here is my code:
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdRptSummary_Click()
  2. 'Checks to see if any of the controls are null
  3.  
  4.     If Nz([CboYear], 0) = 0 Or Nz([CboMonth], 0) = 0 _
  5.     Or Nz([cboWeek], 0) = 0 Then
  6.  
  7.       If Nz([CboYear], 0) = 0 Then MsgBox "Please select the Forecast Year from the" & _
  8.     " drop down menu", 64, "Select Forecast Year"
  9.     If Nz([CboMonth], 0) = 0 Then MsgBox "Please select the Forecast Month from the" & _
  10.     " drop down menu", 64, "Select Forecast Month"
  11.       If Nz([cboWeek], 0) = 0 Then MsgBox "Please select the Forecast Week from the" & _
  12.     " drop down menu", 64, "Select Forecast Week"
  13.  
  14.        Else
  15.  
  16. 'If no null values then check to see if there is data to report on
  17.  
  18. Dim rst As DAO.Recordset
  19. Set rst = CurrentDb.OpenRecordset("QryForecastSummary")
  20.  
  21. If rst.BOF And rst.EOF Then 'If none, then end process and send out MsgBox
  22.  
  23.         MsgBox "Given your selections, there are no records to delete and reload.", 64, "No Records Match"
  24.  
  25.      Else
  26.  
  27. 'If there is data, open the Forecast Summary Report
  28.  
  29.     DoCmd.OpenReport "SumReport", acViewPreview
  30.  
  31.   End If
  32.     End If
  33. End Sub
  34.  
The error focus is on the following line:
Expand|Select|Wrap|Line Numbers
  1. Set rst = CurrentDb.OpenRecordset("QryForecastSummary")
  2.  
I never set a openrecordset to a query before so I am thinking that that is where my problem is.... I now the query works when I test it without the command button...

Any ideas?

Thanks,

Keith.
Apr 17 '08 #1
4 8930
Stewart Ross
2,545 Expert Mod 2GB
Hi Keith. The OpenRecordset method generally works fine with queries. I have checked that it works without error on a query returning no records (Query2 in the code below, deliberately set so that the WHERE clause won't return any records), and it works as expected - the recordcount property is 0 indicating no records.

I reckon the parameter error is resulting from the underlying query itself - I have seen such errors occur when a query refers to a form control (in the criteria of the query) which may work fine in the query editor and fail when opened in other contexts. There is a known bug in Access over the use of form field references in queries.

Could you check the underlying query to see whether it refers to a form control in its criteria somewhere?

Cheers

Stewart
Expand|Select|Wrap|Line Numbers
  1.     Dim RS As DAO.Recordset
  2.     Set RS = CurrentDb.OpenRecordset("Query2")
  3.     If RS.RecordCount = 0 Then
  4.         MsgBox ("Empty")
  5.     Else
  6.         MsgBox ("Not Empty: " & RS.RecordCount)
  7.     End If
  8.     RS.Close
  9.  
Apr 17 '08 #2
kcdoell
230 100+
Stewart:

Thanks for the reply, and yes my query does refer to my combo boxes that are located on my form. Let me try your idea and see what I come up with...

Thanks,

Keith
Apr 17 '08 #3
ADezii
8,834 Expert 8TB
Hello:

What is the best way to stop a report from running if the query is empty? Currently, I have a form that has a command button on it. The user has to make selections from 3 combo boxes on the form and then via the cmdbutton the report opens in preview mode. I want to stop it from executing if the query is empty. I thought I could do this by checking the record count by the following code but I error out with the following message:
Expand|Select|Wrap|Line Numbers
  1. Too few parameters. Expected <number>. (Error 3061)
  2. This parameter query requires the specified number of parameters, which you did not supply. Provide the expected number of parameters, and run the query again. 
  3.  
Here is my code:
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdRptSummary_Click()
  2. 'Checks to see if any of the controls are null
  3.  
  4.     If Nz([CboYear], 0) = 0 Or Nz([CboMonth], 0) = 0 _
  5.     Or Nz([cboWeek], 0) = 0 Then
  6.  
  7.       If Nz([CboYear], 0) = 0 Then MsgBox "Please select the Forecast Year from the" & _
  8.     " drop down menu", 64, "Select Forecast Year"
  9.     If Nz([CboMonth], 0) = 0 Then MsgBox "Please select the Forecast Month from the" & _
  10.     " drop down menu", 64, "Select Forecast Month"
  11.       If Nz([cboWeek], 0) = 0 Then MsgBox "Please select the Forecast Week from the" & _
  12.     " drop down menu", 64, "Select Forecast Week"
  13.  
  14.        Else
  15.  
  16. 'If no null values then check to see if there is data to report on
  17.  
  18. Dim rst As DAO.Recordset
  19. Set rst = CurrentDb.OpenRecordset("QryForecastSummary")
  20.  
  21. If rst.BOF And rst.EOF Then 'If none, then end process and send out MsgBox
  22.  
  23.         MsgBox "Given your selections, there are no records to delete and reload.", 64, "No Records Match"
  24.  
  25.      Else
  26.  
  27. 'If there is data, open the Forecast Summary Report
  28.  
  29.     DoCmd.OpenReport "SumReport", acViewPreview
  30.  
  31.   End If
  32.     End If
  33. End Sub
  34.  
The error focus is on the following line:
Expand|Select|Wrap|Line Numbers
  1. Set rst = CurrentDb.OpenRecordset("QryForecastSummary")
  2.  
I never set a openrecordset to a query before so I am thinking that that is where my problem is.... I now the query works when I test it without the command button...

Any ideas?

Thanks,

Keith.
Keith, there is no need to go through all this unnecessary work, Why not use the Report Event that was specifically designed for this situation?
  1. In your Report's NoData() Event:
    Expand|Select|Wrap|Line Numbers
    1. Private Sub Report_NoData(Cancel As Integer)
    2.   Cancel = True
    3. End Sub
  2. In the Click() Event of your Command Button:
    Expand|Select|Wrap|Line Numbers
    1. Private Sub cmdReportSummary_Click()
    2. On Error GoTo Err_cmdRptSummary
    3. Const conCANCEL_REPORT_OPEN As Integer = 2501
    4.  
    5.    DoCmd.OpenReport "SumReport", acViewPreview
    6.  
    7. Exit_cmdRptSummary:
    8.   Exit Sub
    9.  
    10. Err_cmdRptSummary:
    11.   If Err.Number = 2501 Then     'Report Open was Canceled
    12.     MsgBox "Report contains no valid data!", vbExclamation, "No Data to Report"
    13.   Else
    14.     MsgBox Err.Description, vbExclamation, "Error in Opening Report"
    15.   End If
    16.   Resume Exit_cmdRptSummary
    17. End Sub
  3. Let me know how you make out.
Apr 17 '08 #4
kcdoell
230 100+
ADezii:

Using the Report's NoData Event worked perfectly. Below was my end solution for those inquiring minds:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdRptSummary_Click()
  2. 'Checks to see if any of the controls are null
  3.  
  4.     If Nz([CboYear], 0) = 0 Or Nz([CboMonth], 0) = 0 _
  5.     Or Nz([cboWeek], 0) = 0 Then
  6.  
  7.       If Nz([CboYear], 0) = 0 Then MsgBox "Please select the Forecast Year from the" & _
  8.     " drop down menu", 64, "Select Forecast Year"
  9.     If Nz([CboMonth], 0) = 0 Then MsgBox "Please select the Forecast Month from the" & _
  10.     " drop down menu", 64, "Select Forecast Month"
  11.       If Nz([cboWeek], 0) = 0 Then MsgBox "Please select the Forecast Week from the" & _
  12.     " drop down menu", 64, "Select Forecast Week"
  13.  
  14.        Else
  15.  
  16. 'If there is no data to report on, stop the report from running and display
  17. 'the error message, otherwise open the report in preview mode.
  18.  
  19. On Error GoTo Err_cmdRptSummary
  20. Const conCANCEL_REPORT_OPEN As Integer = 2501
  21.  
  22.    DoCmd.OpenReport "SumReport", acViewPreview
  23.  
  24. Exit_cmdRptSummary:
  25.   Exit Sub
  26.  
  27. Err_cmdRptSummary:
  28.     If Err.Number = 2501 Then     'Report Open was Canceled
  29.         MsgBox "Given the selections there is no data to report on," & _
  30.         " please select different parameters and try again.", vbExclamation, "No Data to Report"
  31.   Else
  32.  
  33.     MsgBox Err.Description, vbExclamation, "Error in Opening Report"
  34.  
  35.     End If
  36.         Resume Exit_cmdRptSummary
  37.             End If
  38. End Sub
I never used that event before. It will sure come in handy!

Thanks to both of you for getting back to me. It is comforting to know I can get some ideas when I feel like I am running out of them. Unfortunately, My 1000 + page Access 2003 book was not doing the trick, though I have benn using it lately to bang my head a few times.... Your solution did the trick.

Take care,

Keith. ;-)
Apr 18 '08 #5

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

Similar topics

2
by: Mal | last post by:
Greetings. I have a perplexing problem....please help. I am having a problem with an insert query. (SQL below) When I run the query via code (executing the SQL string) it crashes Access and...
1
by: Nathan Bloomfield | last post by:
Does anyone know if there is any documentation which relates to Access2k + ? or can anyone help adjust the code? I am having trouble converting the DAO references. TITLE :INF: How to...
2
by: Robert | last post by:
I have a report that runs on a parameter query. I run the report, enter the parameter, and it works as long as there is at least one record returned by the query. But if it comes up empty, I get...
1
by: Intrepid_Yellow | last post by:
Hi, I have the following code that runs my report generator. The user selects a table from a combo box, then whatever fields they want from a list box. (This part all works and the report runs...
15
by: sara | last post by:
I am stuck. I have a report that I use in multiple places, so I call it with varying parameters (using the Where Clause in the code). I preview the report, send it to snap, then close the...
6
by: Phil Stanton | last post by:
I am running a query that calls a function used to format addresses depending on the width of a control on a report that shows that address. The same query is used as the RecordSource of lots of...
3
by: creative1 | last post by:
Here is how you create a complex data report that involves parent and child commands and you can update information at runtime. Its pretty straight forward to work with simple queries; however,...
3
by: jambonjamasb | last post by:
Hi I have two tables: email_tbl Data_table Data table is is used to create a Form Data_form
1
by: pupilstuff | last post by:
hi guys i wan to make dyanmic crystal report according to values which i checked from check box thats all i did 1. I made data set having data table name "Customer" 2 i put four columm id,name...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.