This is driving me mad. In some inherited code are lots of examples of saving reports as PDF files, prior to emailing them.
So I have copied working code and changed it for an existing report:
Code:
If gMessage = "Option1" Then DoCmd.OpenReport "repPaymentHistory", acViewPreview, , "customerid=" & intCustomerID & "", acDialog
Else
gEmailMessage = "Please see attached Payment History Report"
strFolder = Left(CurrentDb.Name, Len(CurrentDb.Name) - Len(Dir(CurrentDb.Name)))
strFile = "PaymentHistory.pdf"
strPath = strFolder & strFile
DoCmd.OpenReport "repPaymentHistory", acPreview, , , acHidden
Reports![repPaymentHistory].Filter = "CustomerId=" & intCustomerID
DoCmd.Close acReport, "repPaymentHistory", acSaveYes
DoCmd.OutputTo acOutputReport, "repPaymentHistory", acFormatPDF, strPath, False
"Option1" simple displays the report on the screen giving just one page for the relevant CustomerId.
However the else branch produces a PDF file with several thousand pages - there are lots of customers!!!
The Report design has FilterOnLoad = True.
The only clue I have is that CustomerId is not used in the Report, but in a Query which is the DataSource of the Report (but it works for the "option1" branch????