By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,581 Members | 1,990 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,581 IT Pros & Developers. It's quick & easy.

Cannot save a generated report as a pdf

P: 2
Hi,

I am very inexperienced with vba. I am trying to save a generated report to a pdf using the following code. The report is generated perfectly and is displayed on the screen.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Report_Unload(Cancel As Integer)
  2.     Dim FileName As String
  3.  
  4.     FileName = "C:\Data\Personal\report.pdf"
  5.     DoCmd.OutputTo acOutputReport, "RP_Annual_Report", AcFormatPDF, FileName
  6.  
  7. End Sub
I get a Run time error of 2585. This action could not be carried out while processing a form or report event.

Any assistance would be greatly appreciated.
4 Weeks Ago #1
Share this Question
Share on Google+
3 Replies


NeoPa
Expert Mod 15k+
P: 31,561
Hi Gerry.

Welcome to Bytes.com.

It does look very like it's exactly what it tells you in the error message. Your code is running within the Report_Unload() event procedure. That is to say it is code which runs when, and only when, the Report object is unloaded during the closing of the Report.

This means you need something else to run that code. You could try an obvious CommandButton on the Report, but that would print with the report unless you're careful to ensure it doesn't. Alternatively you could use something like a Double-Click event on one of the existing Controls to trigger the code.

Actually, that's all more complicated than necessary. When you trigger the report to open, why not add the code immediately after it's opened. That would be outside of the control of the Report entirely. The best approach is really determined by what exactly it is you require. Only you know that at this stage.
4 Weeks Ago #2

P: 2
Hi NeoPa, THanks for taking the time to understand and respond. I run the report directly from a portal for with a button using DoCmd.OpenReport "RP_Annual_Report", acViewReport.

The report has got a form inside it to accept parameters. When I try running it directly from the portal, it generates a report without any data in it.
4 Weeks Ago #3

NeoPa
Expert Mod 15k+
P: 31,561
Hi Gerry.

Gerry:
I run the report directly from a portal form with a button using DoCmd.OpenReport "RP_Annual_Report", acViewReport.
I'm guessing that meant Form? No idea what portal is supposed to represent in this but I imagine the important part is that it's run from a Form. After running :
Expand|Select|Wrap|Line Numbers
  1. DoCmd.OpenReport "RP_Annual_Report", acViewReport
you should run the code :
Expand|Select|Wrap|Line Numbers
  1. DoCmd.OutputTo acOutputReport, "RP_Annual_Report", AcFormatPDF, FileName
Gerry:
The report has got a form inside it to accept parameters. When I try running it directly from the portal, it generates a report without any data in it.
Well, that sounds like another problem Gerry. It's very hard to know exactly what the problem is though, unless you can describe it a little better. Are you trying to say that it has a Sub-Form within the Report and that the Report uses references to that Sub-Form to determine what it should include? Sounds like a mess if I'm brutally honest (One of my failings. I often AM brutally honest - but I mean it well).

Let's make it simple. Information that determines how you build up the information in a Report should be determined before you ever get to the point of opening it. I prefer to get that information and build a filter for the Report in code that is then used to open the Report with, as a WhereCondition parameter. Some people prefer an existing string that has links to the Controls on a Form and to hope that the particular Form is available whenever the Report is run. I find my way gives more reliable results as there are fewer things that can go wrong - obviously assuming you do it right when you do it of course. getting the information from somewhere only after it's been opened though, that seems very awkward.
4 Weeks Ago #4

Post your reply

Sign in to post your reply or Sign up for a free account.