Outputto PDF 2501 Error (1 Viewer)

algecan

Registered User.
Local time
Today, 08:33
Joined
Nov 11, 2009
Messages
37
I seem to get an intermittent problem when trying to output access reports to PDF, resulting in the error 2501.

This has been covered numerous times on the internet but the fault usually seems to lie with either characters in the file name such as brackets and colons or permissions, neither of which seem to apply to me.

When I say the problem is intermittent, it happens on certain records and not others, rather than sometime I can output a report with record 'a', and other times I can't.

Something else is is how I output the report. The error happens when I click on a command button on a form using this code:

Code:
 DoCmd.OpenReport stDocName, acViewPreview, , "Job_Reference = " & Me.Job_Reference
    DoCmd.OutputTo acReport, stDocName, acFormatPDF
    DoCmd.Close acReport, stDocName

However, I have another button on the form to just view the report, using:
Code:
 DoCmd.OpenReport stDocName, acViewPreview, , "Job_Reference = " & Me.Job_Reference
If I right click on the report, and then print to PDF using the Microsoft to PDF printer, I don't get the error message, even on a record where I would normally get it using the outputto function.

During testing, it doesn't matter what I name the file or where I save it to, I still get the error.

Any ideas what may be causing the issue?
 

Minty

AWF VIP
Local time
Today, 08:33
Joined
Jul 26, 2013
Messages
10,355
Have you checked that the file being created isn't either already open or Access "thinks" it is open?
 

algecan

Registered User.
Local time
Today, 08:33
Joined
Nov 11, 2009
Messages
37
I'll get the error regardless of whether I'm trying to overwrite a PDF that already exists or If i'm outputting a new one - if I have understood your question correctly?
 

Minty

AWF VIP
Local time
Today, 08:33
Joined
Jul 26, 2013
Messages
10,355
If the file that is trying to be overwritten is already open then you'll get an error.

If that is not the case it has to be something in your file naming convention. What is the report called?
And what are your job references ?
 

algecan

Registered User.
Local time
Today, 08:33
Joined
Nov 11, 2009
Messages
37
I did think about the naming convention but it doesn't seem to be the case as far as i can see. As I don't specify one in the outputto, it's up to the user to decide that in the save as box that appears. it defaults to rptJCS, which is fine but again no matter what I change the name to I get the same outcome.

the job ref is the primary key, auto number for the record.
 

isladogs

MVP / VIP
Local time
Today, 08:33
Joined
Jan 14, 2017
Messages
18,186
Error 2501 occurs when code is cancelled. The generic version of the error is:

The | action was canceled.@You used a method of the DoCmd object to carry out an action in Visual Basic, but then clicked Cancel in a dialog box. For example, you used the Close method to close a changed form, then clicked Cancel in the dialog box that asks if you want to save the changes you made to the form.@@1@@@1

So you need to step through your code and identify what is cancelling the code.

You could also try adding error handling which tells Access to ignore error 2501 if it occurs (whilst still stopping for other errors). For example

Code:
Private Sub YourProcedureName()

On Error GoTo Err_Handler

'procedure code here

Exit_Handler:
  Exit Sub

Err_Hanlder:
  If err=2501 Then 
      Resume Next
  Else
      MsgBox "Error " & err.Number & " " & err.Description
      Resume Exit_Handler
  End If

End Sub

However, I would first try & identify the cause of error 2501 rather than use a 'sticking plaster' to cover the error
 

Eugene-LS

Registered User.
Local time
Today, 11:33
Joined
Dec 7, 2018
Messages
481
Code:
Sub test01()
Dim sReportName$, sFilePath$
    sReportName = "rptTest"
    sFilePath = "D:\Temp\rptTest" & Format(Now, "yyyy.mm.dd hh\-nn\-ss") & ".pdf"
    'or
    'sFilePath = CurrentProject.Path & "\rptTest" & Format(Now, "yyyy.mm.dd hh\-nn\-ss") & ".pdf"

'Export to PDF ...
    DoCmd.OutputTo acOutputReport, sReportName, acFormatPDF, sFilePath, False
    MsgBox sReportName & " - Exported as: " & vbNewLine & sFilePath, vbInformation, "Report Exported as PDF"

End Sub
 

algecan

Registered User.
Local time
Today, 08:33
Joined
Nov 11, 2009
Messages
37
Error 2501 occurs when code is cancelled. The generic version of the error is:



So you need to step through your code and identify what is cancelling the code.

You could also try adding error handling which tells Access to ignore error 2501 if it occurs (whilst still stopping for other errors). For example

Code:
Private Sub YourProcedureName()

On Error GoTo Err_Handler

'procedure code here

Exit_Handler:
  Exit Sub

Err_Hanlder:
  If err=2501 Then 
      Resume Next
  Else
      MsgBox "Error " & err.Number & " " & err.Description
      Resume Exit_Handler
  End If

End Sub
However, I would first try & identify the cause of error 2501 rather than use a 'sticking plaster' to cover the error

I have stepped through and it errors on the outputto line. As I have mentioned I have done a bit of testing to narrow it down and the one thing that stands out is the outputto as a PDF fails, put if I view the access report and then try and print this as a PDF using the Microsoft to PDF printer using the same criteria (Name, location), it saves as a PDF fine.

I do have error handling for this error number which has been fine for a while but the number of occurrences of this error have increased and I can no longer just ignore it really.
 

isladogs

MVP / VIP
Local time
Today, 08:33
Joined
Jan 14, 2017
Messages
18,186
Just read the first post again more carefully!
I think you may want to kick yourself when you read this ....

In post #1 you wrote this code:
Code:
 DoCmd.OpenReport stDocName, acViewPreview, , "Job_Reference = " & Me.Job_Reference
    DoCmd.OutputTo acReport, stDocName, acFormatPDF
    DoCmd.Close acReport, stDocName

The correct syntax (including optional arguments) is:
DoCmd.OutputTo(ObjectType, ObjectName, OutputFormat, OutputFile, AutoStart, TemplateFile, Encoding, OutputQuality)

The problem is that you haven't specified an output file name & path!
Once you add that, all should be OK.
Change it to e.g.
Code:
DoCmd.OutputTo acReport, stDocName, acFormatPDF, strFileNamePath

It probably works on occasions because you've already specified a path by a different method such as the MS PDF 'printer'.
If nothing has been specified, the code errors

HTH
 

algecan

Registered User.
Local time
Today, 08:33
Joined
Nov 11, 2009
Messages
37
The reason I had left it blank is because if you do, you get prompted for a save location. This error messages occurs after a save location has been selected and your see the window coming up to say that it is outputting page 1 of 1.

I do see your point though and it's problems to explicitly write the save location, however it is different for each document as it goes into the files working folder. I'm would like to avoid it having to go somewhere temporary first for it to then be moved to the desired location. Is there anyway to do this?

In the meantime, I will use your suggestion and see if it solves the error.

Thanks.
 

isladogs

MVP / VIP
Local time
Today, 08:33
Joined
Jan 14, 2017
Messages
18,186
In that case, you just need a way of specifying that first.

From what you have written, you will need to do that in 2 parts:
1. Folder path
a) If this is fixed, save the path as a variable
b) If it depends on the folder the source data came from, save that as a variable
c) Or use code for the user to select the correct folder
d) If the folder doesn't exist, use code to create a specified folder

2. File name
a) As 1a)
b) If it depends the value of a particular field, pull that data in code
c) Otherwise use an input box or a form control for the file name to be added

Once both folder path & file name have been identified using the appropriate method, concatenate them to give the output file name & path using code similar to that by Eugene-LS in an post#8
 

Users who are viewing this thread

Top Bottom