How To Output access Report with Condition

前端 未结 2 1113
花落未央
花落未央 2021-01-24 01:26

Good Day Dears

I Have a Form To Run Report With Condition When I Run The Form The Filter(Condition) Work Correctly For Report

But When I Create PDF( DoCmd.Output

相关标签:
2条回答
  • 2021-01-24 01:41

    Whenever you open the report, its filters the data based on query and shows the result as desired. The problem is, to execute the Docmd.OutputTo you first have to close the report and as the reports closes the filter effects gets cleared and output pdf will contain all the records.

    Solution is if you don't want to preview the report then you can use acHidden instead of acDialog, otherwise if you really want to preview the report and then want to export as pdf then on the form put a button and code that button to export the report as PDF.

    0 讨论(0)
  • 2021-01-24 01:59

    I would base the report on a query (if it's not so already) and since you know the SQL you need for the report at runtime, you can replace the SQL in the QueryDef entirely (with the proper WHERE filter) and just output the report. You do not need to open in beforehand. Omit the OpenReport call and just do something like this:

    CurrentDB.QueryDefs("qryMyReportBase").SQL = "SELECT * FROM main WHERE ID LIKE '" & Nz(Me.cmrtxt, "*") & "' AND place_of_discharge_ar LIKE '" & Nz(Me.dischargecombo, "*") & "' AND border LIKE '" & Nz(Me.bordercombo, "*") & "'  AND a_date Between " & _
                                                 Format(Nz(Me.statrdatetxt, "01/01/1900"), "\#mm\/dd\/yyyy\#") & " And " & _
                                                 Format(Nz(Me.enddatetxt, "01/01/2900"), "\#mm\/dd\/yyyy\#")
    
    DoCmd.OutputTo acOutputReport, "Report1", acFormatPDF, "C:\MySavePath\Report1.pdf", False
    

    Obviously, "qryMyReportBase" should be the report's source query and "C:\MySavePath\" should be your save path.

    Also, I didn't see anywhere in OutputTo where you were specifying the type as PDF. Since that's what you mentioned above, I've added that in.

    0 讨论(0)
提交回复
热议问题