Convert MsAccess report to PDF and save it in specific customer path (1 Viewer)

Minty

AWF VIP
Local time
Today, 13:23
Joined
Jul 26, 2013
Messages
10,354
Actually the active object is the report that is open, and the code is run from it.
I'm not sure you can reference it that way. Certainly your error is nothing to do with that.

Like I said, create a form with all your records and simply open a filtered report to that record and output it.

Your file process checking should be (in order)

1. Does the folder and subfolder exist, if not create it.
2. Does the file exist, if it doesn't simply create it.
2.1 If it does ask user if they want to overwrite it.
2.2 Create the file or exit based on their answer.
 

gstylianou

Registered User.
Local time
Today, 15:23
Joined
Dec 16, 2013
Messages
357
DoCmd.OutputTo objecttype:=acOutputReport, objectName:=Me.Name, outputformat:=acFormatPDF, outputFile:=filePath


1. I am not sure about the red expression. May be correct, but may not.

More likely
2. You would get error 2501 if the report failed because of an error in the query. It's a generic "did not work" indicator. does the report open if you just try it outside this code block?


Hi again,

About the objectName:=Me.Name should be ok because if you check the first attached accent file which is the original file the report can be saved into a simple path. So about that I think we are OK.

The report source it's from simple select query without any filtering with data. So, I don't think so that the problem it's because of that..Really I don't know what else I must try...

Havr you check the modified example on your Pc,? It's working normally ?

(I start think that maybe something happened with my office pack....2016 ProPlus 32bit)
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 13:23
Joined
Sep 12, 2006
Messages
15,613
What I meant was - if you just open the report, to preview it, does it open, or do you get an error? If you get an error, trying to produce the report in code will give you error 2501

So your last post made me think - is this code INSIDE the report? if so that might be the problem.

you probably need a separate form with a button on - and then run that code from the button. I am not sure whether you can produce a pdf from a button on the report itself, and probably less likely from using code in design mode - although even that may be possible in later versions of access.

Alternatively you can open the report in preview mode, right-click it and then "save as pdf" from the short menu.
 

gstylianou

Registered User.
Local time
Today, 15:23
Joined
Dec 16, 2013
Messages
357
What I meant was - if you just open the report, to preview it, does it open, or do you get an error? If you get an error, trying to produce the report in code will give you error 2501

So your last post made me think - is this code INSIDE the report? if so that might be the problem.

you probably need a separate form with a button on - and then run that code from the button. I am not sure whether you can produce a pdf from a button on the report itself, and probably less likely from using code in design mode - although even that may be possible in later versions of access.

Alternatively you can open the report in preview mode, right-click it and then "save as pdf" from the short menu.


Morning,

The report works without any error if run.
The VBA code run using a command button on the report. So after your last post maybe the problem is because of that?
You suggest to use a form and from there to call the procedures? If so, please explain how you thing is better to do that from a form?

1. First filtering data on the firm?
2. Run direct the code without open the report? If so, how can the doctor check his/her data on the report without open it?

Thanks again for your help and your time

Have a nice day!
 

June7

AWF VIP
Local time
Today, 05:23
Joined
Mar 9, 2014
Messages
5,423
Report open in PrintPreview is not interactive, command buttons are not clickable. Buttons are clickable in ReportView but ReportView not exportable. So, conventional approach is code behind form to:

1. Open report
2. OutputTo pdf
3. Close report

Viewing filtered data on form may be one way to preview data that will output to report. Although, reports can be more complex and not actually look like form display.

But why should user need to look at report before output? User just needs to select filter criteria. If they really want to preview, then open in PrintPreview and let them right click Export.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 13:23
Joined
Sep 12, 2006
Messages
15,613
@june

thanks for that explanation. I am not completely familiar with the newer versions of Access. I realised you could now put buttons on reports, but hadn't researched them at all.

@gstylianou. In that case the error 2501 is happening because you aren't allowed to open (run) the report to prepare a pdf the way you are trying to - so the report fails, and 2501 is just a general "failed" message that isn't so helpful. Access probably doesn't know the code you are using is "in the report" - so it can't provide a more helpful message.

The easiest way is either to have button on a form to use the code you are using (but it won't be me.name - you will need the name of the report) - or to preview the report, and save it from there if you want a pdf.

I think you need to try some things instead of trying to get a chapter and verse solution. We don't know how your trying to design your app interface - we are just aware of what might be causing your problem.
 

Minty

AWF VIP
Local time
Today, 13:23
Joined
Jul 26, 2013
Messages
10,354
@OP - See post #21 we are all suggesting the same thing.

Create a form with cmd buttons to either preview the whole report, or select a specific record and export it.
 

riti90

Registered User.
Local time
Today, 13:23
Joined
Dec 20, 2017
Messages
44
I use this code on a button:
On Error GoTo Err_mySUB

Dim thisFile As String
thisFile = CurrentDb.Name
thisFile = Mid(thisFile, InStrRev(thisFile, "") + 1)
thisFile = Left(thisFile, Len(thisFile) - 6)

Dim DBPath As String
DoCmd.SetWarnings False
DBPath = CurrentProject.Path & ""
DoCmd.OpenReport "YOUR-REPORT", acViewPreview, , , acHidden
DoCmd.OutputTo acOutputReport, "YOUR-REPORT", acFormatPDF, DBPath & thisFile & "_Report" & ".pdf", True
DoCmd.Close acReport, "YOUR-REPORT", acSaveYes
DoCmd.SetWarnings True


Exit_mySUB:
Exit Sub
Err_mySUB:
MsgBox Err.Number & ": " & Err.Description & " (" & Erl & ")"
Resume Exit_mySUB
 

Users who are viewing this thread

Top Bottom