Solved

Button Code to Save Access 2013 Report to PDF has stopped working?

Posted on 2015-01-05
8
294 Views
Last Modified: 2015-01-10
Hi all, I created a simple Access 2013 project that has a single table, a form and a report.  The form has entry for all fields on the table and then a button at the bottom of the form.  The button has code behind it which is supposed to print the current record details using the report and then save the current report as a PDF to 2 different locations.  As I am a novice with Access, I had help to get this working but did get it all working and has been working fine for about 6 months now.  I just noticed today that the saved PDF's of the reports have no data on them?  It saves the report to a PDF in the correct location but when you open the PDF, you just see the report as it was designed but the actual data fields have no data in them?  When looking back at the data, it has been doing this now for about a month.  I will attach the code I use to accomplish this and display below.  Thank you for your help!  I am using Access 2013 32-bit as part of 365 and running on Windows 7 64-bit with SP1.

Private Sub PrintCurrent_Click()
'Print current record
'using JobTicket report
'then Save to Desktop as PDF
Dim strReport As String
Dim strFileName As String
Me.Dirty = False
If IsNull(Me!ID) Then
MsgBox "Please select a valid record", _
vbOKOnly, "Error"
Exit Sub
End If
DoCmd.OpenReport "JobTicket", acViewNormal, , "ID =" & Me.ID
strReport = "JobTicket"
strFileName = "C:\Users\Zund\Desktop\" & Year([Entry Date]) & "-" & Me.[Jobnum] & ".pdf"
DoCmd.OutputTo acOutputReport, strReport, acFormatPDF, strFileName
strReport = "JobTicket"
strFileName = "W:\MASTER JOB TICKETS\" & Year([Entry Date]) & "-" & Me.[Jobnum] & ".pdf"
DoCmd.OutputTo acOutputReport, strReport, acFormatPDF, strFileName
End Sub

Private-Sub-PrintCurrent.docx
0
Comment
Question by:mjchevalier
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 37

Expert Comment

by:PatHartman
ID: 40531940
Sounds like a new bug introduced in the past month.  I would report it to Microsoft.

In the mean time, you can modify your method.  I am using A2013 and don't have a problem exporting to pdf.  I use a different method though.  The OutputTo method does not support a where argument the way the OpenReport method does so rather than open the report in print preview first and then outputting the open report, I have the report's RecordSource query get its argument directly from the form.

Select ...
From ...
Where ID = Forms!yourform!ID;

Using that technique, you should get rid of the OpenReport.
0
 

Author Comment

by:mjchevalier
ID: 40532096
Thanks PatHartman.  I went back to the report and looked at the RecordSource and found that it is currently set to:

SELECT * FROM JobTicket WHERE (((JobTicket.[ID])=[Forms]![JobNum]![ID]));

So now I am confused because apparently I am already doing what you have asked me to do?  I am not sure where to go from here now???
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 40532275
Try these three things:
1. Compile your code, ...retest
2. Create a blank database and import all of the objects, ...retest
Try the code as something similar to this syntax:
    'Set the name of the Report
    strReport="YourReport"
    'Open the report in Print Preview mode, filtered for the current ID, and Hidden
    DoCmd.OpenReport strReport, acViewPreview, , "ID=" & Me.ID, acHidden
    'Set the output file specs
    strFileName = "C:\YourFolder\" & Year([OrderDate]) & "-" & Me.[CustID] & ".pdf"
    'Output this Report to a PDF
    DoCmd.OutputTo acOutputReport, strReport, acFormatPDF, strFileName
    DoCmd.Close acReport, strReport

...Then retest...

Also, ...can you export the report as a PDF from the menus? (or right-tclicking the report from the navigation pane?)

JeffCoachman
0
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 

Accepted Solution

by:
mjchevalier earned 0 total points
ID: 40532317
I fixed it myself by adding a ", False" to the end of each OutputTo command and that did it.  Thanks for the help but this one I fixed.
0
 
LVL 37

Expert Comment

by:PatHartman
ID: 40532860
because apparently I am already doing what you have asked me to do
No.  You were still opening the report in print preview first (and I said not to) and so the OutputTo was using the open version of the report rather than opening its own.
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 40532871
...odd because:
AutoStart:false
...is presumed if this argument is left out...
0
 
LVL 31

Expert Comment

by:Helen_Feddema
ID: 40534068
There is no need to open the report first -- just the OutputTo line works fine (supposing the various arguments have been set correctly).  I would replace the reference to a value on a form with a saved value, either in a global variable or a custom database property, so the value won't disappear if the form is closed.
0
 

Author Closing Comment

by:mjchevalier
ID: 40541769
I was able to find the solution myself through testing and adding a statement to the code.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Having trouble getting your hands on Dynamics 365 Field Service or Project Service trial? Worry No More!!!
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question