473,804 Members | 2,986 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

if report query is empty, close report

tuxalot
200 New Member
The easy way is to put cancel = True in the On No Data event of the report. But why replicate code across all reports that way? My app selects reports from a listbox, so I would prefer to check the record count of the query, and if 0, never get to the DoCmd.OpenRepor t line. Here's the code (case 2 is where I would like to check if the report query has data, and my DCount attempt is not working):
Expand|Select|Wrap|Line Numbers
  1. ...
  2.  
  3. Dim stDocRS As String
  4.  
  5.     'store report name in variable
  6.     stDocName = Me.lstReportName.Column(1)
  7.     stDocRS = stDocName & "." & RecordSource
  8.  
  9.     'check print options
  10.     Select Case Me.grpPrintOptions.Value
  11.  
  12.         Case 2        'print preview
  13.             If DCount("*", stDocRS) = 0 Then
  14.                 MsgBox "No records match the current criteria"
  15.                 Exit Sub
  16.             Else
  17.                 DoCmd.OpenReport stDocName, acViewPreview
  18. ...
  19.  
The DCount is producing a 3078 error, and the text reads "...Access cannot find the input table or query '[correct report name is shown here].[but here shows one of the underlying tables that feed the query?]'...". This is strange since the report's Record Source is the query.

Thanks for looking,

Tux
Mar 28 '09 #1
11 17268
ADezii
8,834 Recognized Expert Expert
@tuxalot
I think you may require a little trickery to get the desired results:
  1. Define the desired Report in some manner, in your case a List Box:
    Expand|Select|Wrap|Line Numbers
    1. strReportName = "Invoice"
  2. Open the Report in 'Hidden' Mode:
    Expand|Select|Wrap|Line Numbers
    1. DoCmd.OpenReport strReportName, acViewDesign, , , acHidden
  3. Retrieve the RecordSource for the Report:
    Expand|Select|Wrap|Line Numbers
    1. strRecordSource = Reports(strReportName).RecordSource
  4. Use DCount() to see if any Records exist in the RecordSource, then take the appropriate action:
    Expand|Select|Wrap|Line Numbers
    1. If DCount("*", strRecordSource) > 0 Then
    2.   DoCmd.OpenReport strReportName, acViewPreview
    3. Else
    4.   DoCmd.Close acReport, strReportName, acSaveNo
    5. End If
  5. All together now:
Expand|Select|Wrap|Line Numbers
  1. Dim strReportName As String
  2. Dim strRecordSource As String
  3.  
  4. strReportName = "Invoice"
  5.  
  6. DoCmd.OpenReport strReportName, acViewDesign, , , acHidden
  7.  
  8. strRecordSource = Reports(strReportName).RecordSource
  9.  
  10. If DCount("*", strRecordSource) > 0 Then
  11.   DoCmd.OpenReport strReportName, acViewPreview
  12. Else
  13.   DoCmd.Close acReport, strReportName, acSaveNo
  14. End If
[/list]
Mar 29 '09 #2
tuxalot
200 New Member
fantastic. Thanks again ADezii.
Mar 29 '09 #3
ADezii
8,834 Recognized Expert Expert
@tuxalot
You are quite welcome, tuxalot.
Mar 29 '09 #4
NeoPa
32,579 Recognized Expert Moderator MVP
@tuxalot
Tux, can I try to convince you that, while this is a working method, it is not a good idea.

I hear what you're saying about repeating the code for every report, but consider the effect of testing the data source before running the query every time. This may be negligible for extremely quick reports, but I'm sure you will have reports that take much longer to run (if not currently, then surely you will in future if you stay in the field). What you are considering is effectively to run the queries twice for every time the report is run.

Is this really what you want as your standard approach when running reports? Is the alternative really so annoying? Only you can decide for you, but I would certainly advise setting Cancel to False instead.
Apr 4 '09 #5
tuxalot
200 New Member
Thanks NeoPa for the advice. I understand how this approach could potentially cause issues. As they say there are many ways to skin the proverbial cat. Developers new to Access like me truly rely on folks like you and the other experts on this forum to help steer us in the right direction.

Thanks to you and ADezii for your insight and expertise.

Tux
Apr 4 '09 #6
NeoPa
32,579 Recognized Expert Moderator MVP
I see you're also trying to post answers where you can Tux, so you're especially welcome :)
Apr 4 '09 #7
bikermo
1 New Member
fantastic code, worked like a charm for me. thank you very much!
Apr 10 '14 #8
mcupito
294 Contributor
On No Data
Expand|Select|Wrap|Line Numbers
  1. Cancel = True
Apr 10 '14 #9
NeoPa
32,579 Recognized Expert Moderator MVP
An important issue to bear in mind, when Cancel is set to True in the OnNoData event procedure, is that the code that calls for the report to open returns an error. This needs to be handled if you don't want the user to see it crash every time.

To be clear, the two bits of code would look something like :
Expand|Select|Wrap|Line Numbers
  1. On Error Resume Next
  2. Call DoCmd.OpenReport(ReportName:="ReportName", View:=acPreview)
  3. On Error GoTo 0
Expand|Select|Wrap|Line Numbers
  1. Private Sub Report_NoData(Cancel As Integer)
  2.     Dim strMsg As String
  3.  
  4.     Cancel = True
  5.     strMsg = "There is no data available for the selected parameters"
  6.     Call MsgBox(Prompt:=strMsg, _
  7.                 Buttons:=vbExclamation Or vbOKOnly, _
  8.                 TITLE:=Me.Name)
  9. End Sub
Notice there is also the possibility of notifying the operator if you choose to. Otherwise, only line #4 is required within the procedure wrapper.
Apr 10 '14 #10

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

Similar topics

7
2431
by: Mike | last post by:
I have to pass a date from a form to a report query. I have written a query that works fine when I execute from SQL view, But I dont know how to pass a value from the form to this query. SELECT Production.Production FROM Production WHERE (((Production.Date)=Forms!ReportForm!reportDate) And ((Production.ShiftName)="Shift1"));
2
1580
by: Tom | last post by:
Hi all: I've run into a problem that is confounding me. Hope you can help. I have the following query (having removed alot of stuff for clarity: SELECT tblComponent.TagNumber, First(nz(tblcomponent.CompStream,"N/A")) AS CompStream, ... FROM ... WHERE ...
1
2959
by: momo | last post by:
Hello, How do I pass an sql query to the crystal report? For example "Select * from products where productid = 215" how can i build a crystal report in vb.net based on this query which returns only 1 row from the table>? I'd be thankful for any suggestions
1
1541
by: z.ghulam | last post by:
I am designing an order database at work and am having problems creating a specific report I'm after. Basically, each order has an 'order type' and what I would like is a report which lists the order types and simply gives a number as to the total amounts of that order type over a certain time period. I've managed to build a query which gives me details of every order and their order-type over a certain time, but I cant seem to work...
2
1324
by: Mike Abbott | last post by:
Hello folks, I am fairly new to access after using dBase III for the last 10 years. I have successfully imported dBase tales into access. I have created a Parameter Query with a start date and end date and produced a report of income or expenses for the period.
6
4327
by: icony | last post by:
Hi everyone, Here's my problem. I have a report that i want to use with the command openreport. The report open and close in a fraction of a second. I cant see why i cant view the thing. Cause i can print it and all... here's my code Private Sub Command4_Click() Dim accObj As Access.Application
0
1352
by: anamika | last post by:
Hi We all know its very easy to pass a sql query to Crystal Report from vb.net However, my requirement is that I want something which is exactly opposite to this. My Cyrstal Report has generated its SQL Query at design time. Now I want to retrieve this query at run time from the front end using visual studio 2005. Any suggestion? Any code? Thanks in advance
2
5426
by: mudman04 | last post by:
Hi, I searched online for some similar issues that I am facing but was not able come up with anything. I am fairly new with Access (2 months experience) and I am trying to remove a message stating, ENTER PARAMETER VALUE when I am generating a report. I know that the culprit lies in the query stored in the report. I have entered data in the CRITERIA (entered forms!frmproducts!lngproductID) window and OR (entered...
20
2231
by: raddrummer | last post by:
Hi there, I'm woking on a function that takes the input from a form (including Payroll Contact), uses it as a query parameter, runs the query, and then emaills out a custom .xls file using the sendObject method to the Payroll Contact specified in a list. After perfroming this function it loops to the next person on the list and requeries to send them a custom list. The problem: After sending the first list the first query will not...
0
9706
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10332
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10077
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6853
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5522
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2991
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.