Link to home
Start Free TrialLog in
Avatar of Edward Hamill
Edward HamillFlag for United States of America

asked on

MS Access 2016 - Export Reports to PDF with dynamic criteria (seperate by office)

OK, I am out of my element but willing to learn!  I created an access database and have a report that I need to export into various folders by office each week.

Not knowing better, I coded each office like this:
    DoCmd.OpenReport "OfficeUserReport", acViewPreview, , "[NRT Enterprise Performance].[Branch]='Marietta/West Cobb' and [NRT Enterprise Performance].[Non-Public]=0"
    DoCmd.OutputTo acOutputReport, "", acFormatPDF, "\\...\Atlanta\Office Reports\Marietta-West Cobb Office Report.pdf", False
    DoCmd.Close acReport, "OfficeUserReport"

It works, but when an office changes names, gets added or removed, it messes up the reports.  How can I create a report to look at a table of offices and "metros" and run the report based off of that?

For example, Table 1 is called NRTOffices.  Fields are Office, repOffice, Metro, Region and Address.  Table 2 is called NRT Enterprise Performance and has fields called Office and Metro (among many others).  *** repOffice is a "friendly version" of the office name that has no invalid characters such as "/", " ' ", "\", etc.

I would like to be able to have the report module run so that it exports each report to \\...\[Metro]\[Region]\Office Reports\[repOffice]&" Office Report.pdf"

Can someone point me in the right direction?  Hoping I provided enough detail to make sense.

thanks in advance!  I have been racking my brain trying to understand loops but fail each time.
ASKER CERTIFIED SOLUTION
Avatar of PatHartman
PatHartman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Edward Hamill

ASKER

I have created the 2 Queries correctly (I believe) and the form but I am getting an error regarding "access run-time error 2147352567 recordset not updateable"
I'm not sure what you are trying to update.  The code opens a recordset and reads it.  It does not update it.  What are you doing when you get the error?  If you are running code, please post it.  If you are running a query, please post it.
Not updating anything that I can tell... I am running this:

Dim db As DAO.Database
Dim qd As DAO.QueryDef
Dim rs As DAO.Recordset
Dim FileName As String

Set db = CurrentDb()
Set qd = db.QueryDefs!qUniqueOffices
Set rs = qd.OpenRecordset

Do Until rs.EOF = True
    Me.txtHiddenOffice = rs!repOffice   
    FileName = "\\vmware-host\Shared Folders\edward.hamill\Box Sync\Edward Hamill\Performance Reports - 2016\Reports\" & rs!repMetro & "\Office Reports\" & rs!repOffice & "OfficeReport.pdf"
 
    DoCmd.OutputTo acOutputReport, "", acFormatPDF, FileName, False
    rs.MoveNext
Loop

rs.Close
qd.Close

Open in new window

On what line does the error appear?  If it is the line that opens the report, please post the query used as the report's recordsource.
Appears on "Me.txtHiddenOffice = rs!repOffice

Query qReport
SELECT [NRT Enterprise Performance].Name, [NRT Enterprise Performance].MLS, [NRT Enterprise Performance].[Intouch Domain], [NRT Enterprise Performance].[Intouch Email], [NRT Enterprise Performance].[Order Type], [NRT SE Branches].repOffice, [NRT SE Branches].repMetro
FROM [NRT Enterprise Performance] INNER JOIN [NRT SE Branches] ON [NRT Enterprise Performance].Branch = [NRT SE Branches].Branch
WHERE ((([NRT SE Branches].[RepOffice])=[Forms]![yourform]![txtHiddenOffice]));

Open in new window


Query qUniqueOffice
SELECT DISTINCT [NRT SE Branches].repOffice, [NRT SE Branches].Metro, [NRT SE Branches].Region, [NRT SE Branches].SubRegion, [NRT SE Branches].Branch, [NRT SE Branches].repMetro
FROM [NRT SE Branches]
ORDER BY [NRT SE Branches].repOffice;

Open in new window


Appreciate you not giving up on me yet :-)
The Me.txtHiddenOffice control should not be bound.  Remove it's ControlSource.  You are just using it as a holding area for Office as the loop runs.  The data will not ever be saved.
Getting to the point of creating the pdf, but receive a '2487': Object argument for the action or method is blank or invalid

 DoCmd.OutputTo acOutputReport, "", acFormatPDF, FileName, False

Open in new window


Closer!!!
Hover over the FileName or print it to the debug window so you can make sure it is a valid name.
It is correct.  I even changed it to a local directory on "C" to make sure it was not a server issue.

Dim db As DAO.Database
Dim qd As DAO.QueryDef
Dim rs As DAO.Recordset
Dim FileName As String

Set db = CurrentDb()
Set qd = db.QueryDefs!qUniqueOffices
Set rs = qd.OpenRecordset

Do Until rs.EOF = True
    Me.txtHiddenOffice = rs!repOffice
    FileName = "c:\test\" & rs!repOffice & "Report.pdf"
    '& rs!repMetro & "\Office Reports\"
 
    DoCmd.OutputTo acOutputReport, "", acFormatPDF, FileName, False
    rs.MoveNext
Loop

rs.Close
qd.Close

Open in new window


Filename shows as "c:\test\183OfficeReport.pdf"
You are missing the report name

DoCmd.OutputTo acOutputReport, "reportname", acFormatPDF, FileName, False
Perfect!!!!  You are the man (or Woman)!!!!!!
Pat was great!  Thanks for the all of the help.
You're welcome.