Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 244
  • Last Modified:

print report only if contains data

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
Kongta
Asked:
Kongta
1 Solution
 
mrt1Commented:
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
 
KongtaAuthor Commented:
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
 
hnasrCommented:
You may need to find the recordset for the records you want.  Check the record count, if 0 then cancel your macro.
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
bclark100898Commented:
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
 
EvanLCommented:
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
 
EvanLCommented:
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
 
KongtaAuthor Commented:
thank you everybody helping me to solve the problem
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Tackle projects and never again get stuck behind a technical roadblock.
Join Now