Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

print report only if contains data

Posted on 2000-02-24
7
Medium Priority
?
240 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 31

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Accepted Solution

by:
bclark100898 earned 200 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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

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.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

721 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