How to Export Your Access Report to a PDF File via VBA

Learn how you can create a VBA function that will allow you to save your Access Report as PDF with just a click of a button. No need to go to the Access Ribbon every time!

Access Reports can be manually saved as PDF file. All you have to do is:Manually Saved As PDF File

  1. Open the report.
  2. Go to the Access Ribbon: External Data > PDF or XPS.
  3. Select the filename and folder path where you would like it to be saved.
  4. Click Publish and you’re done.

Easy, right? However, if there is a need for you to repeatedly export reports, say on a daily basis, this can be a bit taxing.

Don’t worry. There’s an easier way. With just one click of a button, your report can be immediately exported to your designated folder. This can be done with the help of VBA. Just follow the steps below.

Download Now

If you want to start to use the feature as soon as possible, then you can:

Download the Sample Database with VBA Codes Now

Otherwise, if you want to DIY, you can read the contents below.

Steps to Creating a VBA Function that Saves Access Report as PDF File

1. Add a command button inside your report

This article assumes that you already have an Access Report created. Open it in design view and add a button in the Report header (as shown below).Add A Command Button Inside Your Report

Rename the button as cmd_exportPDF. To do this:

  1. While in design view, select the button.
  2. Open the Property Sheet (ALT + Enter).
  3. Go to the Other tab > Name.
  4. Change the Name value to cmd_exportPDF.

2. Add code for the command button

To add the code, you have to first add an On Click event to the button.

  1. With the command button selected, go again to the Property Sheet.
  2. Click on the Event tab.
  3. Look for On Click and select …
  4. When the Choose Builder menu pops up, select Code Builder and click OK.
  5. This will open the VBA Editor.

Now, copy the code below.

Don’t forget to update values for filename and fldrPath. They each have asterisk (*) in the comment section.

Function FileExist(FileFullPath As String) As Boolean
  Dim value As Boolean
  value = False
  If Dir(FileFullPath) <> "" Then
    value = True
  End If
  FileExist = value
End Function

Private Sub cmd_exportPDF_Click()
 
  Dim fileName As String, fldrPath As String, filePath As String
  Dim answer As Integer
 
  fileName = "Member Contact Details" 'filename for PDF file*
  fldrPath = "C:\Users\Jessica\Desktop\PDF Exports" 'folder path where pdf file will be saved *
 
  filePath = fldrPath & "\" & fileName & ".pdf"
 
  'check if file already exists
  If FileExist(filePath) Then
    answer = MsgBox(prompt:="PDF file already exists: " & vbNewLine & filePath & vbNewLine & vbNewLine & _
      "Would you like to replace existing file?", buttons:=vbYesNo, Title:="Existing PDF File")
    If answer = vbNo Then Exit Sub
  End If
 
  On Error GoTo invalidFolderPath
  DoCmd.OutputTo objecttype:=acOutputReport, objectName:=Me.Name, outputformat:=acFormatPDF, outputFile:=filePath
 
  MsgBox prompt:="PDF File exported to: " & vbNewLine & filePath, buttons:=vbInformation, Title:="Report Exported as PDF"
  Exit Sub
 
  invalidFolderPath:
  MsgBox prompt:="Error: Invalid folder path. Please update code.", buttons:=vbCritical
 
End Sub

These lines of code will:

  1. First check if a PDF file with the same filename already exists in the specified folder. If it does, a prompt will appear asking if you would like to replace it with a new one.

If you select yes, the report will continue with the export process. If not, the file will not be converted to PDF.

  1. If you entered a folder path that does not exist, a prompt will appear to ask you to update the folder path specified in the code. Export process will not continue until this is corrected.
  2. Once the report has been successfully exported, a prompt will appear indicating where the PDF file was saved.

That’s it! Now it’s time to test your button.

Unable to Open Access Database

The most terrifying thing that can happen to any Access user is to wake up one day and realize that the database could no longer be opened. It has been corrupted. What’s even worse is that you realize that you were not able to back up your data. Don’t worry. DataNumen Inc. created a software that can fix Access database. Your database can still be brought back to life.

Author Introduction:

Jayme Stack is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including sql recovery and excel recovery software products. For more information visit www.datanumen.com

Comments are closed.