Export Reports to pdf

Gerrit.B

Board Regular
Joined
Aug 10, 2004
Messages
237
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
In my database I have created report (rptInvoice) based on query (qryInvoice) that includes all customers.

Is is possible to export the report to pdf, 1 (one) pdf for each customer (CustomerID), and save the reports as customerID.pdf.

Regards,

G
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Hi, assuming you are using Access 2007 or higher you can accomplish this as follows.
First you’ll need a control structure to enable looping through all Customer ID’s. Best option would be creating a form (let’s name this frmCreateInvoices) with a listbox on it and two command buttons (one to create all invoices, one to create a single invoice for a selected customer id).
As I don’t know anything about you’re database, I’ll stick to the ‘qryInvoice’ for the examples I’m giving you.
To use the examples, first create a user form named frmCreateInvoices (although the name of the form is not relevant in this case).
Then create the next controls on the form:

listbox named lstCustomerID
command button cmdAllInvoices
command button cmdOneInvoice

In the load event of your report place the next code:

Code:
Private Sub Report_Load()
Me.RecordSource = "Select * From qryInvoice " _
                & "Where CustomerID = '" & Me.OpenArgs & "'"
End Sub


In the load event of your form place the next lines of code

Code:
Private Sub Form_Load()

'Set the listbox properties and data
With Me.lstCustomerID
    .ColumnCount = 1
    .RowSource = "Select CustomerID from qryInvoice"
End With

End Sub

And for the command buttons use the next code

Code:
Private Sub cmdAllInvoices_Click()
'Create an invoice for each customer
'Here we do'n really need the listbox, as we can derive the Customers from the query
'but for the sake of the example we use the listbox anyway

Dim i As Integer
Dim lstBox As ListBox

Set lstBox = Me.lstCustomerID

For i = 0 To lstBox.ListCount - 1
HandlerCreateInvoice lstBox.ItemData(i)
Next i

End Sub

Private Sub cmdOneInvoice_Click()
'Although the command button is called OneInvoice, we can use it to create multiple invoices.
'Make sure to set the multiselect property to EXTENDED
'Create an invoice for each selected customer

Dim vItem As Variant
Dim lstBox As ListBox

Set lstBox = Me.lstCustomerID
'Test if user made a selection
If lstBox.ListIndex = -1 Then
    MsgBox "Select at least one customer"
End If

For Each vItem In lstBox.ItemsSelected
        HandlerCreateInvoice lstBox.ItemData(vItem)
Next vItem

End Sub

Now you have the first part set up, the next step is creating a new module with the code to actually create the PDF's.
The complete code is:


Code:
Option Compare Database
Option Explicit

Public Sub HandlerCreateInvoice(ByVal sCustomerID As String)

[B]Const sDefaultPath As String = "c:\temp\"
[/B]Const sReportName As String = "rptInvoice"

Dim sPDFName As String

sPDFName = sDefaultPath & sCustomerID & ".pdf"
CreatePDF sPDFName, sCustomerID, sReportName

End Sub

Public Sub CreatePDF(ByVal sPDFName As String, _
                     ByVal sCustomerID As String, _
                     ByVal sReportName As String)

Dim rpt As Report
'Open the report, giving sCustomerID as OpenArg
DoCmd.OpenReport sReportName, acViewReport, , , , sCustomerID

Set rpt = Reports(sReportName)

DoCmd.OutputTo acOutputReport, rpt.Name, "PDFFormat (*.pdf)", sPDFName, , , , acExportQualityPrint
DoCmd.Close acReport, rpt.Name

End Sub



I guess from here you'll know what to do. But feel free to ask any questions.

 
Last edited:
Upvote 0
Erratum: Of course the line for the report has to be in the OPEN event and NOT in the load event.

Code:
Private Sub Report_Open(Cancel As Integer)
Me.RecordSource = "Select * From qryInvoice " _
                & "Where CustomerID = '" & Me.OpenArgs & "'"
End Sub
 
Upvote 0
Private Sub Form_Load()
'Set the listbox properties and data
With Me.lstCustomerID
.ColumnCount = 1
.RowSource = " SELECT qryInvoice.CustomerID FROM qryInvoice GROUP BY qryInvoice.CustomerID; "
End With
End Sub

Changed BOLD code above to avoid creating reports multiple times for the same customer.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,620
Messages
6,120,559
Members
448,970
Latest member
kennimack

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top