Solved Run-time error 2501 (1 Viewer)

A1ex037

Registered User.
Local time
Today, 10:17
Joined
Feb 10, 2018
Messages
50
I'm having trouble with exporting report to a PDF file. This error happens only on newly installed PC and fresh installation of Office 2016. On two other machines (same version of Office, same database, different paths), everything works as expected. I have tried checking regional and language settings, format options, and everything looks the same.

Dim reportName As String
Dim filePath As String

reportName = "ReportK no " & Forms!frmSales!ID & " - " & Forms!frmSales![Name] & " - " & Forms!frmSales!SaleDate
filePath = Application.CurrentProject.Path & "\reportK\" & reportName & ".pdf"

DoCmd.OpenReport "ReportK", acPreview, , "[ID]= forms![frmSales]![ID]", acHidden
DoCmd.OutputTo acOutputReport, "ReportK", acFormatPDF, filePath, True, acExportQualityPrint
DoCmd.Close acReport, "ReportK", acSaveNo

If I check reportName and filePath with msgBox, it looks OK. If I test DoCmd.OpenReport (without acHidden, and two last lines commented out), it correctly displays preview in access. If I continue with print option from access (and choose Microsoft Print to PDF), it saves correctly. User has full permissions to save at designated folder.

Any help is appreciated.
 

Minty

AWF VIP
Local time
Today, 09:17
Joined
Jul 26, 2013
Messages
10,355
Does the file already exist, and/or is already open in the target directory?

I always check in code first and either delete or prompt that it open and ask them to close it.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 17:17
Joined
May 7, 2009
Messages
19,170
test also for the existence of reportK subfolder.
also add a Pause between OutputTo and Closing the Report.
access will not pause the execution and will continue to close the report
even if it has not yet produced the pdf.

Code:
DoCmd.OpenReport "ReportK", acPreview, , "[ID]= forms![frmSales]![ID]", acHidden
DoCmd.OutputTo acOutputReport, "ReportK", acFormatPDF, filePath, True, acExportQualityPrint

'add pause, checking if the pdf has been created
Do Until Len(Dir$(filePath)) > 0
    DoEvents
Loop
'add another pause, checking if the pdf "length" > 0
Do Until VBA.FileLen(filePath) > 0
    DoEvents
Loop
'it is safe to close the report
DoCmd.Close acReport, "ReportK", acSaveNo
 

A1ex037

Registered User.
Local time
Today, 10:17
Joined
Feb 10, 2018
Messages
50
Thank you for fast replies. Directories are empty. Nothing inside, reportK subfolder is also there (with appropriate permissions).
Same thing happens if I add pause between OutputTo and Close lines.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 17:17
Joined
May 7, 2009
Messages
19,170
check your Where Criteria (OpenReport), is this correct?
or something like this:

DoCmd.OpenReport "ReportK", acPreview, , "[ID]=" & Forms![frmSales]![ID], acHidden
 

Minty

AWF VIP
Local time
Today, 09:17
Joined
Jul 26, 2013
Messages
10,355
I've never had to pause a report output before closing it, but will bow to @arnelgp suggestion as he must have seen that happen.

I would use a simple check in the code to absolutely make sure of your paths and the availability of them to Access
As you said the only thing different is the paths so that would be my prime suspect.

Separate out your File name and file path and check (in code) if you can access it.
 

A1ex037

Registered User.
Local time
Today, 10:17
Joined
Feb 10, 2018
Messages
50
I have tested each line separately, paths are correct. I have a bug at the back of my head that I had to do something on machine when it was first brought to work, but cannot remember what. That drives me out of my mind right now. Might be related to Unicode characters (file names contain Unicode chars), but with this newly installed machine, I cannot export even non-Unicode file name. It is ridiculous, same db works flawlessly on 2 other machines, it has to be something Windows related.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 04:17
Joined
Feb 28, 2001
Messages
27,001
You said this was on a newly installed machine whereas other machines were working OK. Can you tell if this newly installed machine is at the same patch level for Windows and for Access as the machines where it works? Newly installed machines tend to take a while to come up to the correct patch level in some cases if a cumulative patch isn't yet available. In that case the patches have to be installed in proper sequence. It is not widely appreciated, but not all Windows patches are always cumulative. Or at least they weren't when I was still doing system security work for the Navy.
 

A1ex037

Registered User.
Local time
Today, 10:17
Joined
Feb 10, 2018
Messages
50
Yes, the computer is freshly installed. It's a nice thought, but everything has been done and configured properly. I have checked for updates, etc. It had to be something with the file name or Unicode chars for reportName. I have tested it again with fixed path (instead of filePath) and fixed name (non-Unicode), and that works (so it's not permissions). If I go with fixed path and reportName, it fails. It's not the code, since this syntax works on other machines. Is there anything in regional settings, format options that I might have overlooked?
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 04:17
Joined
Feb 28, 2001
Messages
27,001
The only other facet of this that could be machine-specific would be the references. You ARE, after all, calling subroutines that are in external libraries. Find a machine that works, get into its VBA screen, follow menu: Tools >> References, and scroll to the top of the list box. Take a screen shot of its checked references. Then print that and take it to the machine that is NOT working. Unchecked references don't count, but verify that you have the same references checked and that they appear in the same order as for one of the working machines.

(For the curious, reference order sometimes matters if there is an object name referenced that could be satisfied by either of two libraries. We used to see this problem fairly often for unqualified DAO vs. ADO recordset references.)
 

Minty

AWF VIP
Local time
Today, 09:17
Joined
Jul 26, 2013
Messages
10,355
So if it's not the path and is the actual filename, can you give us some example filenames that fail?
BTW I would permanently keep the two separate in your procedure just for ease of troubleshooting.

And definitely check your references as per @The_Doc_Man suggestion.
 

A1ex037

Registered User.
Local time
Today, 10:17
Joined
Feb 10, 2018
Messages
50
I did that few days ago (honestly, didn't know that the order matters). Everything looks completely the same. I have compared all 3 computers. It has to be something related to formatting or regional settings, that's for sure.

Here is an example of generated output name:

03275 - Marko Marković - 12.12.2020. 12.01.34
 

Minty

AWF VIP
Local time
Today, 09:17
Joined
Jul 26, 2013
Messages
10,355
Do the computers have the same languages installed?

And assuming more than one which one is set to default?
 

A1ex037

Registered User.
Local time
Today, 10:17
Joined
Feb 10, 2018
Messages
50
Yes, same languages, same defaults (english). I will remove 2nd language now and see if it changes anything.

Tried that, nothing changes. It's something with formatting that I'm missing. When I try to save it without last part of the name (date and time), it can save it.

Found it. It was a time separator (should have been "." instead of ":")
So simple, yet so easy to overlook.

Thank you for your suggestions. I already implemented separation of filePath and reportName as @Minty suggested. Also, @The_Doc_Man I have nothing but the most respect for you. I had noticed that you always give answers to any of the questions that I had in the past. I am learning from all of your answers. For that, I am truly thankful.

Thank you all.
 
Last edited:

Minty

AWF VIP
Local time
Today, 09:17
Joined
Jul 26, 2013
Messages
10,355
Good to hear you have sorted it out.

I nearly suggested removing the "." from the file name I always use underscores, as I have an old DOS based reluctance to use periods in a filename, as it used to cause problems with file extensions.
 

Users who are viewing this thread

Top Bottom