Solved

print report only if contains data

Posted on 2000-02-24
7
227 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
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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.

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now