
- #Sending ms access reports to pdf via vba code access how to
- #Sending ms access reports to pdf via vba code access plus

Not very flexible and a maintenance burden. It requires to create multiple very similar objects (reports).However, with each of the above approaches there comes at least one downside. Build the SQL for the report at runtime and assign it to the report.Īll of these do work.Create a parameter query referencing form fields or global functions as record source for the report.Create different reports with the criteria built in.The obvious workarounds that come to mind are usually one of the following: So, it seems to be a problem to export a report that displays different data sets depending on user input or other factors. Other than the DoCmd.OpenReport-Method, the DoCmd.OutputTo-Method does not support to supply any criteria to the report the filter the data.
#Sending ms access reports to pdf via vba code access how to
It is not obvious how to export a dynamically filtered report using this method. There is one aspect of using this very convenient method that I frequently see people struggle with. This method allows you to easily export Access reports to PDF files from VBA code without any 3rd-Party components. In Microsoft Access 2010 and newer (Access 2007 with an additional Add-In) there was the very useful output format option acFormatPDF added to the DoCmd.OutputTo-Method. "Error Description: " & Err.How to Output a dynamically filtered Access report to PDFīy Philipp Stiefel, originally published November 8th, 2017 "Error Number: " & Err.Number & vbCrLf & _ MsgBox "The following error has occured" & vbCrLf & vbCrLf & _ If Err.Number 2501 Then 'Let's ignore user cancellation of this action! 'Now we loop and start the process over with the NEXT ID 'Here we close the form so we can loop to the next recordĭoCmd.Close acReport, "Rpt_AssetActionForm" ' Now we have Access preview the report passing it the one record using ID - and it's hidden to you don't see the previewĭoCmd.OpenReport "Rpt_AssetActionForm", acViewPreview,, "=" & rs1.Fields("ID").Value, acHiddenĭoCmd.OutputTo acOutputReport, "Rpt_AssetActionForm", acFormatPDF, strFileName,, ,, acExportQualityPrint StrFileName = strFolder & "\" & strFileName & "-" & rs1.Fields("Parcel Acct Num").Value & "-AAF-" & Format(Now(), "YYYY-MM-DD") & ".pdf" StrFileName = rs1.Fields("Site Number").Value
#Sending ms access reports to pdf via vba code access plus
In this case Site Number and Parcel Acct Num plus the date makes up the file name 'Build the filename - in this example I wanted the filename to use data from the single record. StrSQL = StrSQLBase & rs.Fields("ID").Value & ")) " This gets us just one record to send to the PDF 'build a query to pull all the data need for the report for the first ID. 'move to the first record of rs (Get the first ID) 'if no records are found in the query passed to the function - exit Set rs = db.OpenRecordset("SELECT ID From ") This gives you a list of Primary Keys (ID). ' this is a direct query to the underlying table for for strQry. 'strQry are all records passed into the function - Create the recordset rs1 This ID is used to select JUST ONE RECORD for the report - one PDF per record The ID field is the primary key from the underlying table you query is based on. It's the SQL version of the query that runs your report with the ID field added. 'This is the SQL query that will return a single record.

'Set the folder where the files will be saved Here’s the code with notes 'Pass in the Query that the report is based on Loop through the records (returning only one record) and pass it to the report to output the PDF file as desired.Create a Query in VBA (Query from #1) that I could add the ID from #2 that would return the data for just one of the records in the underlying table.Query a unique list of ID’s (Primary Keys) from the recordset used for the report.Create the report as desired based on some query.Here the outline of the process that worked for me. The trick was sending only one record to the report so that only one file would be created per record… MS Access has the ability to print directly to PDF. I needed to create a report where when printed it would create one PDF file per record.

What I had was a recordset from a query in MS Access.
