Separate Access Report for Each record in a Query - Loop I Think via VBA

healey21

Well-known Member
Joined
Dec 22, 2009
Messages
900
I have a report which currently shows all records from a query, what I would like to achieve is the ability to Output the report as a snapshot for each record on its own snapshot into a folder for our accounts department. I am searching but can't find a solution. The details are:

Report Name: "rptInvoiceToProcess"
Query Name: "qryInvoiceToProcess"
Folder Location: "L:\Accounts To Process\"
Field ID Name is: "InvoiceID"
Table Name if it helps: "tblInvoices"

Any help or direction would be very much appreciated.
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
I have simulated your information, and created a form with a combo box and a command button the combo is named cboNames and behind the command button is the code for On Click Event. You would need to select the first name in the combo then click the button. The code used is as follows:

Private Sub cmdExportPDF_Click()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim MyFileName As String
Dim mypath As String
Dim temp As String
mypath = "L:\Accounts to Process\" 'Adjust the path to your location
Set db = CurrentDb()
Set rs = db.OpenRecordset("SELECT [InvoiceID] FROM [tblInvoices]", dbOpenDynaset) 'Change the table name as required

Do While Not rs.EOF
temp = rs("InvoiceID") 'Change field name as required

MyFileName = rs("InvoiceID") & ".PDF" 'You can explore assigning the other field names

' set form value to the Customer Name, so report will be filtered correctly

Me.cboName.Value = rs("InvoiceID") 'cboName is the combo box name on the form

' set the Me.cboName as filter for your report
DoCmd.OpenReport "rptInvoiceToProcess", acViewReport, Me.cboName
DoCmd.OutputTo acOutputReport, "", acFormatPDF, mypath & MyFileName
DoCmd.Close acReport, "Invoices to PDF"
rs.MoveNext
Loop
Set rs = Nothing
Set db = Nothing

End Sub
 
Upvote 0
Trevor/Heaely,

i am using the same code, but when i use with a query it gives an error of 3061 expected 2 but when i use with the table it works OK.

and the error is in the this line

Set rs = db.OpenRecordset("SELECT [InvoiceID] FROM [tblInvoices]", dbOpenDynaset) 'Change the table name as required

Do i have to use different coding for a query?

Any thoughts.

Regards
 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,119
Members
452,381
Latest member
Nova88

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