I am constructing a public function which accepts inputs and prompts to save various reports in a specific folder when called on the close event of the print preview of the report. The relevant code where this error occurs is:
The Debug.Print output looks OK:
sRptName: rptBloodPressureStats
sFileName: C:\Users\....\Reports\MyTestyMcTesty\BloodPressureStats_00007_20250214.pdf (... is just obfuscation)
error 2485: This action can't be carried out while processing a form or report event.
DoCmd.OutputTo can be used in public function? Is it because the function is called while the report is in Print Preview on the on Close?
Why or what is incorrect? How is it fixed?
Code:
sFilename = sRptPath & sFilename & sFileType
MsgBox "WARNING!!! If a file named -- " & sFilename & " -- exists it will be deleted. " & vbCrLf & _
"NOTE: The file name uses todays date. Unless this report has been run more than once today, for the same person, this is not an issue." & vbCrLf & _
"Rename the old file before pressing OK if you need to keep the old file.", vbInformation, "Overwrite warning"
KillFile (sFilename)
Debug.Print sRptName
Debug.Print sFilename
DoCmd.OutputTo acOutputReport, sRptName, acFormatPDF, sFilename ' <---- ERROR HERE
MsgBox "File saved successfully" & vbCrLf & _
" -- & sFileName", vbInformation + vbOKOnly
The Debug.Print output looks OK:
sRptName: rptBloodPressureStats
sFileName: C:\Users\....\Reports\MyTestyMcTesty\BloodPressureStats_00007_20250214.pdf (... is just obfuscation)
error 2485: This action can't be carried out while processing a form or report event.
DoCmd.OutputTo can be used in public function? Is it because the function is called while the report is in Print Preview on the on Close?
Why or what is incorrect? How is it fixed?