Solved

print report only if contains data

Posted on 2000-02-24
7
232 Views
Last Modified: 2009-01-13
I would like to print a report only if
it contains data. if there are no data
available, it should generate a msg-box.

How to do as VBA?
0
Comment
Question by:Kongta
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 2

Expert Comment

by:mrt1
ID: 2554169
Use the OnNOData event of the report.

Your code might look something like this:

Private Sub Report_NoData(Cancel As Integer)
  msgBox "Report is empty!",vbExclamation,"No Data"
  Cancel = true
End Sub
0
 

Author Comment

by:Kongta
ID: 2554550
I have used this already. But because I use a makro printing the report, this routine will not work. It will print me a paper without data and gives me an error report on executing a query I have instructed while printing. But the query does and can not to be executed, if there are no datas. So I need to tell the system in "On opening report" if there are data do this, if not do this....  
0
 
LVL 30

Expert Comment

by:hnasr
ID: 2554984
You may need to find the recordset for the records you want.  Check the record count, if 0 then cancel your macro.
0
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
LVL 2

Accepted Solution

by:
bclark100898 earned 50 total points
ID: 2555325
Method One:  Look for data before report opens.

Public Function PrintClass()
   
    Dim WhereString As String
   
    WhereString = "nz([Time Stamp]) > 0 And nz([Time Stamp]) < #December 31, 9999 23:59:59# And nz([Class])=""" & Nz(SubForm![Class]) & """"

    If DCount("[Record Number]", "[Slalom]", WhereString) = 0 Then
        MsgBox ("No Data Found")
        Exit Function
    End If
   
    DoCmd.RunCommand acCmdSaveRecord
   
    With DoCmd
        .OpenReport "Slalom Report", acPreview, , WhereString
        .RunCommand acCmdFitToWindow
        .Maximize
    End With

End Function

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *


Method Two:  Use the “NoData” subroutine in the report.  You have to trap the NoData error (number 2501) in the function that calls the report.

‘This sub is part of the report.

Private Sub Report_NoData(Cancel As Integer)

    MsgBox "The report has no data." _
        & Chr(13) & "Printing is canceled.", _
        vbOKOnly + vbInformation, _
        Report.Name
       
    Cancel = True

End Sub

‘This is the function that opens the report.  You can call it from a macro.  I add

Public Function OpenReport(ReportName As String, Optional WhereClause) AS Boolean
   
    'if the report is cancelled because of no data, an error results in the OpenReport method
    On Error GoTo ReportError
    With DoCmd
        .OpenReport ReportName, acPreview, , WhereClause
        .RunCommand acCmdFitToWindow
        .Maximize
    End With

    OpenReport = True

FunctionExit:
    Exit Function

ReportError:
    'Error 2501 means that the OpenReport action was cancelled.
    'Ignore it.
    If Err.Number <> 2501 Then

        MsgBox "Run Time Error '" & Err.Number & "':" & Chr(13) & Chr(13) & _
               Err.Description & Chr(13), , _
               "Microsoft Visual Basic"
               
    End If

    OpenReport = False

    Resume FunctionExit

End Function


0
 
LVL 1

Expert Comment

by:EvanL
ID: 2558052
I use this code from Dev Ashish that works well:

--- Originally posted by Dev Ashish ---

Close report automatically if no data found

(Q)    How can I close a report automatically if there's no data returned by the underlying query?

(A)    You can use the Report's OnNoData event for this.  For example, the following code

     '************* Code Start *************
     Private Sub Report_NoData(Cancel As Integer)
         MsgBox "No data found! Closing report."
         Cancel = True
     End Sub
     '************* Code End *************

    will automatically close the report if there are no records in the underlying source.

 

    However, if you're opening the report from code behind a form,   you need to handle the error that's generated as a result.

     '*********** Code Start ************
     Private Sub TestNoData_Click()
         On Error Resume Next
         DoCmd.OpenReport "SomeReport", acViewPreview
         If Err = 2501 Then Err.Clear
     End Sub
     '*********** Code End   ************
0
 
LVL 1

Expert Comment

by:EvanL
ID: 2558062
Doh!  I see that bclark posted the same code in his second half.  Kongta, if you end up using this code, the points would go to bclark.

Sorry!
0
 

Author Comment

by:Kongta
ID: 2559599
thank you everybody helping me to solve the problem
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question