Save to PDF without opening (1 Viewer)

xyba

Registered User.
Local time
Today, 14:04
Joined
Jan 28, 2016
Messages
189
Hi

I have the below code to save a report to pdf. However, I don't want the report to open so could someone suggest what I need to change in the code, please:

Code:
Private Sub CreateReportBtn_Click()
On Error GoTo cmdPrintMapArea1_Click_Err
DoCmd.OpenReport Me.RepNameBox, acViewReport, , IIf(Me.DepNameBox = "All", "", "dept='" & Me.DepNameBox & "'")
DoCmd.OutputTo acOutputReport, , acFormatPDF
DoCmd.Close acReport, Me.RepNameBox

Exit Sub
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 21:04
Joined
May 7, 2009
Messages
19,233
you can open the report as Hidden:
Code:
DoCmd.OpenReport Me.RepNameBox, acViewReport, , _
IIf(Me.DepNameBox = "All", "", "dept='" & Me.DepNameBox & "'"), acHidden
 

xyba

Registered User.
Local time
Today, 14:04
Joined
Jan 28, 2016
Messages
189
you can open the report as Hidden:
Code:
DoCmd.OpenReport Me.RepNameBox, acViewReport, , _
IIf(Me.DepNameBox = "All", "", "dept='" & Me.DepNameBox & "'"), acHidden

Hi arnel

I tried adding acHidden as you suggested but this also seems to hide the save dialog box. As there is no default "save to" filepath this needs to be selected by the user.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 21:04
Joined
May 7, 2009
Messages
19,233
the button click was from the Form?
then add another textbox to that form where to save the report.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 09:04
Joined
Feb 19, 2002
Messages
43,223
There is no need to open the report first if you use criteria in the query. The problem with OutputTo is that it doesn't take the where argument so if you need to filter the report at runtime, you either have to open the report (hidden) and remember to close it after the output or you use a WHERE clause in the report's RecordSource query that gets the arguments from your form.

Here is code from one of my report pages that lets the user choose what to do with his output.
Code:
    Select Case frm.fraOutputTo
        Case 1  'open form
            If rs!DisplayType = "Report" Then   'some selections can only be displayed as reports
                DoCmd.OpenReport stDocName, acViewPreview, , stLinkCriteria
            Else
                stDocName = frm.txtFormToOpen
                frm.Visible = False
                DoCmd.OpenForm stDocName, , , , , acWindowNormal, frm.Name
            End If
        Case 2  'preview report
            If rs!DisplayType = "Form" Then 'some selections can only be displayed as forms
                stDocName = frm.txtFormToOpen
                frm.Visible = False
                DoCmd.OpenForm stDocName, , , , , acWindowNormal, frm.Name
            Else
                DoCmd.OpenReport stDocName, acViewPreview, , stLinkCriteria
            End If
        Case 3  'print report
            If rs!DisplayType = "Form" Then 'some selections can only be displayed as forms
                stDocName = frm.txtFormToOpen
                frm.Visible = False
                DoCmd.OpenForm stDocName, , , , , acWindowNormal, frm.Name
            Else
                DoCmd.OpenReport stDocName, acViewNormal, , stLinkCriteria
            End If
        Case 4  'export to Excel
            If IsNull(rs!ExportQuery) Then      '
                MsgBox "Export is not available for this report.", vbOKOnly + vbInformation
            Else
'                ThisDBPath = Left(CodeDb.Name, InStrRev(CodeDb.Name, "\"))
'                ExportFileName = ThisDBPath & Forms!frmLogin!cboAuditParmsID.Column(4)
                DoCmd.OpenForm "frmGetDirectory2", acNormal, , , , acDialog
                ExportFileName = Forms!frmLogin!txtPath
                If ExportFileName & "" = "" Then
                    MsgBox "Path name was not provided.  Export cancelled.", vbOKOnly + vbCritical
                    rs.Close
                    GoTo lstReports_DblClick_Exit
                End If
                ExportFileName = ExportFileName & "-" & rs!ExportFileName & "-" & Format(Date, "yymmdd") & ".XLS"
                If Dir(ExportFileName) <> "" Then Kill ExportFileName
                DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, rs!ExportQuery, ExportFileName, True
                MsgBox "File Exported to ---> " & ExportFileName, vbOKOnly + vbInformation
            End If
        Case 5  ' pdf
                DoCmd.OpenForm "frmGetDirectory2", acNormal, , , , acDialog
                ExportFileName = Forms!frmLogin!txtPath
                If ExportFileName & "" = "" Then
                    MsgBox "Path name was not provided.  Export cancelled.", vbOKOnly + vbCritical
                    rs.Close
                    GoTo lstReports_DblClick_Exit
                End If
                ExportFileName = ExportFileName & "-" & rs!ExportFileName & "-" & Format(Date, "yymmdd") & ".pdf"
                If Dir(ExportFileName) <> "" Then Kill ExportFileName
                'DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, rs!ExportQuery, ExportFileName, True
                DoCmd.OutputTo acOutputReport, stDocName, acFormatPDF, ExportFileName, False
                MsgBox "File Exported to ---> " & ExportFileName, vbOKOnly + vbInformation
        Case Else   ' no output type selected
            MsgBox "Please select an output type.", vbOKOnly + vbInformation
            Exit Sub
    End Select
 

Attachments

  • DEAReportPage.jpg
    DEAReportPage.jpg
    97.6 KB · Views: 832

Users who are viewing this thread

Top Bottom