Solved

print report only if contains data

Posted on 2000-02-24
7
231 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
Independent Software Vendors: 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 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

736 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