• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 74
  • Last Modified:

Print and Export to PDF Buttons on Reports

Is there a way to add a couple of buttons on an Access 2016 report for Printing and Export to PDF?
0
ML
Asked:
ML
  • 2
  • 2
2 Solutions
 
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
You could, but the more elegant solution is to either (a) use a Form to launch your report, and include an "Export to PDF" function on that form or (b) create a Ribbon that allows the user to interact with the report.

You can easily export a report to PDF like this, after opening it:


DoCmd.OutputTo acOutputReport, "YourReport", acFormatPDF, "Full path to your PDF save"
0
 
PatHartmanCommented:
Here's an example of a report form from one of my applications.  There are 5 output options.  The first is a list style form that acts like a to-do list.  The users just pick the next subscriber on the list to open the primary update form.  Some of the reports can be exported to excel.  those are marked in the Export Available column.  Keep in mind that if the criteria for the report is self contained, you do NOT have to preview it first.  You can just send it directly to .pdf.
ReportForm.JPG
0
 
MLGIS Program ManagerAuthor Commented:
Scott, I did change my search to a combo box on my main menu form.  Looks so much better!  But when I go to add a button to export with the code you provided directly onto the report, I don't see the button after I run the report from the combo box.  Does that make sense?  If I open the report up manually, I do see the button.
0
 
MLGIS Program ManagerAuthor Commented:
Pat, I really like the radio buttons!  I'm pretty novice at Access so I'm not sure how to work that up.  I do like how  all the options are there to see.  I believe having them open the report in preview would be best, then give the option to export or print.
0
 
PatHartmanCommented:
I believe having them open the report in preview would be best, then give the option to export or print.
That defeats the purpose of using the option buttons.  If you insist on previewing first, then don't use the option group.  It won't work on the report unless you open the report in Report View and that prevents pagination.  If you always preview the report first, it is better to make a custom ribbon.
 
This is code in the click event of the option group.  It makes the path field visible or invisible.  The Excel and PDF options have to create files and this is the place where the files will be saved.
Private Sub fraOutputTo_Click()
    Select Case frm.fraOutputTo
        Case 1  'open form
            Me.txtPath.Visible = False
        Case 2  'preview report 
            Me.txtPath.Visible = False    
        Case 3  'print report 
            Me.txtPath.Visible = False    
        Case 4  'export to Excel
            Me.txtPath.Visible = True
        Case 5  ' pdf
            Me.txtPath.Visible = True
    End Select
End Sub

Open in new window

This is the code from the command button to view the report.  I edited it because it was much more complicated than what you need.  Hopefully, I didn't remove anything important.  In my case, the report list comes from a local table and includes various options for each report such as whether it can be exported to Excel
Private Sub cmdOutput()
    Select Case frm.fraOutputTo
        Case 1  'open form
  
                stDocName = frm.txtFormToOpen
                frm.Visible = False
                DoCmd.OpenForm stDocName, , , , , acWindowNormal, frm.Name
           
        Case 2  'preview report
   
                DoCmd.OpenReport stDocName, acViewPreview, , stLinkCriteria
            
        Case 3  'print report
   
                DoCmd.OpenReport stDocName, acViewNormal, , stLinkCriteria
            
        Case 4  'export to Excel
            If IsNull(rs!ExportQuery) Then      '
                MsgBox "Export is not available for this report.", vbOKOnly + vbInformation
            Else                
                ExportFileName = Me.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                
                ExportFileName = Me.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.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
End Sub

Open in new window

The final piece is a browse button for the users to find a directory so they don't have to type it in
Private Sub cmdBrowseForPath_Click()
On Error GoTo Err_Proc
    Me.txtPath = fChooseDirectory()
Exit_Proc:
    Exit Sub
Err_Proc:
    MsgBox Err.Number & "--" & Err.Description, vbCritical
    Resume Exit_Proc
End Sub

Open in new window


Anf finally, the choose directory code
Public Function fChooseDirectory()

    'Declare a variable as a FileDialog object.
    Dim fd As FileDialog

    'Create a FileDialog object as a File Picker dialog box.
    Set fd = Application.FileDialog(msoFileDialogFolderPicker)

    'Declare a variable to contain the path
    'of each selected item. Even though the path is a String,
    'the variable must be a Variant because For Each...Next
    'routines only work with Variants and Objects.
    Dim vrtSelectedItem As Variant

    'Use a With...End With block to reference the FileDialog object.
    With fd

        'Use the Show method to display the File Picker dialog box and return the user's action.
        'The user pressed the action button.
        If .Show = -1 Then

            'Step through each string in the FileDialogSelectedItems collection.
            For Each vrtSelectedItem In .SelectedItems

                'vrtSelectedItem is a String that contains the path of each selected item.
                'You can use any file I/O functions that you want to work with this path.
                'This example simply displays the path in a message box.
                
                'Only one item will be returned since the file dialog is a folder picker
                'MsgBox "The path is: " & vrtSelectedItem
                fChooseDirectory = vrtSelectedItem
                Exit Function
            Next vrtSelectedItem
        'The user pressed Cancel.
        Else
        End If
    End With

    'Set the object variable to Nothing.
    Set fd = Nothing
    fChooseDirectory = "Error - nothing chosen"
End Function

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

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