runtime error 2501 outputto action was cancelled with access vb macro (1 Viewer)

justin198z28

New member
Local time
Today, 17:46
Joined
Sep 30, 2011
Messages
4
Hi!

I'm struggling with a vb macro in access 2007 that returns a "runtime error 2501 outputto action was cancelled". The codes is intended to break up a 1690 page report into 338 five page pdfs, name them, and save in a file structure it creates. Unfortunately it errors when it finishes creating the first pdf - the debugger highlights the DoCmd.OutputTo acOutputReport line.

I've tried different computers and uninstalling/reinstalling printer drivers. I've added error handling to ignore error 2501 but the code then runs but the pdfs and folder structure is never produced. This code worked the last time the report was run a year ago. I've inherited this project from someone who left the company and I have to admit I have almost no knowledge of vb code. Any idea what the problem is?

Option Compare Database
Option Explicit
Private Sub CreateReports_Click()
Dim cn As ADODB.Connection
Dim rsDistricts As ADODB.Recordset
Set cn = CurrentProject.Connection
Set rsDistricts = cn.Execute("SELECT [District_Code], [AGENCY_KEY], [AGENCY_NAME] FROM District_List")
Do Until rsDistricts.EOF
'Open report for a specific district
DoCmd.OpenReport "JUSTIN_District_Report_Template for 2010-11 9-27", acViewPreview, , "[District_Code]='" & _
rsDistricts![District_Code] & "'", acWindowNormal
'Output report to pdf file
DoCmd.OutputTo acOutputReport, "JUSTIN_District_Report_Template for 2010-11 9-27", acFormatPDF, _
"G:\OEA\TSH\ACCESS for ELLs Data\SecureReports\AMAO Reports\" & rsDistricts![AGENCY_KEY] & "\999999\2010_11\" & rsDistricts![AGENCY_NAME] & "_" & rsDistricts![District_Code] & "_ELL_AMAO_2010-11.pdf", , , , acExportQualityPrint

'Close report
DoCmd.Close acReport, "JUSTIN_District_Report_Template for 2010-11 9-27"

rsDistricts.MoveNext
Loop
cn.Close
End Sub
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 23:46
Joined
Sep 12, 2006
Messages
13,892
error 2501 is report/form failed to open.

you can handle it, and dismiss it - but you dont get the report, as you have found.

this may be a problem with the report itself. maybe there is some error on the query that means the report fails - in which case, you get error 2501. It may not necessarily be anything to do with the print itself.

Having said that - something obvious - you have got a default printer installed on this PC haven't you?

-------------
i see you have these

docmd.openreport ...

docmd.outputto ...

docmd.closereport ...


so maybe these don't work together. try just one or two of these, and see which combination is failing.
 
Last edited:

justin198z28

New member
Local time
Today, 17:46
Joined
Sep 30, 2011
Messages
4
Thanks for helping!

If the report successfully opens as print preview or exports as one large pdf (by right clicking on the report and clicking export, not with the macro), does that confirm that there are no problems with the queries? I can't find a problem with the queries.

I do have a default printer installed.

Will test each of these next:
docmd.openreport ...

docmd.outputto ...

docmd.closereport ...
 

justin198z28

New member
Local time
Today, 17:46
Joined
Sep 30, 2011
Messages
4
I couldn't get it to work so I used a some other methods on the exported pdf. One utility for breaking up the pdf into multiple pdfs, another for renaming, and COPY in the command prompt to put the files into the right folders. Not pretty but it worked. Thanks!
 

boblarson

Smeghead
Local time
Today, 15:46
Joined
Jan 12, 2001
Messages
32,068
I think you may have just had a problem with the length of the file name actually being in the DoCmd.OutputTo code. If you had assigned it to a variable and then used that variable in the DoCmd.OutputTo code it may have worked.
 

justin198z28

New member
Local time
Today, 17:46
Joined
Sep 30, 2011
Messages
4
Thanks! I'll try your suggestion on Monday just to see if that was the problem. Is there a specific character limit?
 

njheathen

New member
Local time
Today, 15:46
Joined
Aug 16, 2012
Messages
3
error 2501 is report/form failed to open.
No, it doesn't mean that.

I'm getting the same error, intermittently, on the Output to PDF statement actuated by a button on the already opened report.

The error occurs when Access tries to save the PDF.

I also don't think the problem has anything to do with the length of the name. My user also has long names for her PDFs, which are encased in a variable in my statement:
Code:
DoCmd.OutputTo acOutputReport, "", acFormatPDF, strReportFile, True
 

spikepl

Eledittingent Beliped
Local time
Tomorrow, 00:46
Joined
Nov 3, 2010
Messages
6,144
I'm getting the same error, intermittently, on the Output to PDF statement actuated by a button on the already opened report.

The error occurs when Access tries to save the PDF.
This can happen if you try to output a report to a pdf file with the same name as a PDF file currently open in the same location.
 

njheathen

New member
Local time
Today, 15:46
Joined
Aug 16, 2012
Messages
3
The problem turned out to be my user's unfortunate habit of including slashes in her file names.

She knows better now. ;-)
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 23:46
Joined
Sep 12, 2006
Messages
13,892
I thought i would re-open this, as I have a really frustrating similar issue.

I am having real problems with saving reports as pdfs in A2010

the outputto command works sometimes, but sometime decides not to work, and just gives a 2501 error. I was using Stephen Lebans code to produce pdf's, which has always worked without any problems - but this is not available in A2010 (out of interest it still produces a pdf, but only the top half!)

I actually get the same behaviour (save fails) if I preview the report, and then try and export to pdf.

here's a screen shot of this

ScreenHunter1.gif

given that the file includes a date/time stamp, and works sometimes, i just can't see how this can be an issue with the file/folder etc.

I would be most grateful for any additional suggestions.


-----
update on this

I modified the code again, and removed some underscores from the file name, replacing them with hyphens - (following Bob L's thoughts above) and i have been unable to reproduce the error since. A good sign. This doesn't account for the manual procedure not working though!
 
Last edited:

acProgrammer

New member
Local time
Today, 23:46
Joined
Oct 17, 2013
Messages
2
To add a little something to the pot - in the hope it'll trigger an 'ah-ha!' moment in someone ... leading to a reliable solution...

I, too, am suffering with this problem. I'm outputting a multi-page report as individual pages in PDF format using Access 2010. The test report has 96 pages which would normally be sent to the printer as a job lot but, for saving purposes, are being opened individually by looping through a recordset and running these lines once for each record:

Code:
DoCmd.OpenReport REPORT_NAME, acViewPreview, , strWhere, acWindowNormal, strOpenArgs
DoCmd.OutputTo acOutputReport, "", acFormatPDF, strFullPath, False
DoCmd.Close acReport, REPORT_NAME
DoEvents
And here's the thing...

If I save to a mapped network drive - I get no problem at all - 100% reliability. All 96 pages/files are saved:
Code:
strFullPath = "N:\!TEST\!For Processing\10-19-2013 08-06-45 Certificates\SMITH Tom (Ref# 04879) 2013-10-19.pdf"
If I use a local path - I see the issue described above:
Code:
strFullPath = "C:\Users\james.admin\Desktop\Certificates\Testing\SMITH Tom (Ref# 04879) 2013-10-19.pdf"
The 'canceled' pages are different at every run (I debug.printed the primary key and also the .absoluteposition).

I stress - the ONLY difference is the path used (needless to say I've checked the permissions and ensured all folders exist).

You'll notice that I don't have any underscores - yet the problem still exists for me.

I've wasted 2 days trying to fix this :banghead: The users want to save to their Desktops. All I can offer is to use a network drive. Not a problem - just makes me look stupid.

James
 

acProgrammer

New member
Local time
Today, 23:46
Joined
Oct 17, 2013
Messages
2
PS So now my code saves the PDFs to the network drive. Then uses FileCopy to copy them back to a Desktop folder. Works a treat ... but I wish it didn't need to!

Anyone know if this problem is still present in Access 2013?

James
 

Zakraket

Registered User.
Local time
Tomorrow, 00:46
Joined
Feb 19, 2013
Messages
56
And still in 2015 ;)

Same kind of code, same problem: I kind of randomly get this error when trying to output a specific report to separate files based on 10-30 records (per "project").
In my case, my separate PDF-files get saved in a "project"-directory (that gets automatically created in a "documents" directory)

After the error, while keeping Access open, when I try to delete the projectdirectory through explorer, I get the error: "can't complete the task because the folder of files are in use in a different program. Close the folder or file and try again." (translated from dutch)

When I close Access I can delete the folder.

Might explain why this problem occurs less (of not) as stated above when using a mapped networklocation.
In my case, my developdatabase tries to save to a local dir (a setting), my productiondatabase saves to a network dir. I was trying to figure out why my dev-db was not able to output PDFs without generating the error.... Might try a networklocation

Still doens't solve the actual error though. Seems Access itself keeps some process running that blocks files locally which causes this error????

It has NOTHING to do with the report itself, since I get the same problem when I use a blank report with only a label "test" on it (and not even a reportdatasource), which should generete only white PDFs with one word ;)

edit: I checked if maybe the creating of the folders was blocking deleting them, but it's just a looping MKDIR which creates the desired tree if not present.
When the tree is already there and I don't use the MKDIR at all the folder is still blocked after filling it with PDFs (even when NO error), while the PDFs themselves are not blocked. Seems like some kind of tempfile is created but not cleaned up after MSAccess finishes or errors exporting PDFs?
Can't find any hidden files though.
 
Last edited:

Minty

AWF VIP
Local time
Today, 23:46
Joined
Jul 26, 2013
Messages
6,664
Check that you aren't running access.exe as administrator, I found this led to an number of weird problems.

Also if you are trying to open the pdf after creation and have Acrobat DC loaded it doesn't work if you use the Application.FollowHyperlink method.
 

Zakraket

Registered User.
Local time
Tomorrow, 00:46
Joined
Feb 19, 2013
Messages
56
I don't open the PDF (perhaps I should include the false' explicitly) after creation from the Outputto command (at all).

My machine is a fresh install, PDFs are currently linked to MS Edge, and I don't think Access is "run as admin" by default? (Of course, I'm currently logged in with an adminaccount)

Tnx for the tips
 

Pikaworm

New member
Local time
Today, 15:46
Joined
Jul 21, 2016
Messages
1
Thank you so much!
your post help me after 1 week to search, I have my solution!
Bravo from the France!:D


To add a little something to the pot - in the hope it'll trigger an 'ah-ha!' moment in someone ... leading to a reliable solution...

I, too, am suffering with this problem. I'm outputting a multi-page report as individual pages in PDF format using Access 2010. The test report has 96 pages which would normally be sent to the printer as a job lot but, for saving purposes, are being opened individually by looping through a recordset and running these lines once for each record:

Code:
DoCmd.OpenReport REPORT_NAME, acViewPreview, , strWhere, acWindowNormal, strOpenArgs
DoCmd.OutputTo acOutputReport, "", acFormatPDF, strFullPath, False
DoCmd.Close acReport, REPORT_NAME
DoEvents
And here's the thing...

If I save to a mapped network drive - I get no problem at all - 100% reliability. All 96 pages/files are saved:
Code:
strFullPath = "N:\!TEST\!For Processing\10-19-2013 08-06-45 Certificates\SMITH Tom (Ref# 04879) 2013-10-19.pdf"
If I use a local path - I see the issue described above:
Code:
strFullPath = "C:\Users\james.admin\Desktop\Certificates\Testing\SMITH Tom (Ref# 04879) 2013-10-19.pdf"
The 'canceled' pages are different at every run (I debug.printed the primary key and also the .absoluteposition).

I stress - the ONLY difference is the path used (needless to say I've checked the permissions and ensured all folders exist).

You'll notice that I don't have any underscores - yet the problem still exists for me.

I've wasted 2 days trying to fix this :banghead: The users want to save to their Desktops. All I can offer is to use a network drive. Not a problem - just makes me look stupid.

James
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Top Bottom