Question

Access 2003: Save report as pdf file

Asked by: mytfein

Hi EE,

I need to save report to pdf, and name the pdf based on the student's name.....

found this post on EE
http://www.experts-exchange.com/Microsoft/Development/MS_Access/Access_Reports/Q_23225253.html?sfQueryTermInfo=1+pdf+report+save

Would like to do the same thing.

Copied the code in the link mentioned in the above post:
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=35321&lngWId=1

into a public module.

Then I ran:
      Public Sub test()
             Call RunReportAsPDF("rpt_Test", "C:\ARG_09_M4_Graph\", "test.pdf")
      End Sub

Report went to printer.....

Do I have to modify any of the values for XP, and my adobe printer name is "Adobe PDF".
Pasted in code window below the default values:

Are there other techniques to saving access report as pdf.
Also, my access report uses criteria from a form, it's not just a static report....

Your advice is deeply appreciated, tx, sandra




Public Function RunReportAsPDF(rptName As String, sPDFPath As String, sPDFName As String)
    '---------------------------------
    'rptName = Microsoft Access report name
    '     you
    'want to create pdf from
    'sPDFPath = the directory path where you
    '     want
    'to create the pdf file (ex. - "c:\data\
    '     ")
    'sPDFName = the name of the pdf file you
    '     are
    'wanting to create (ex. - "file001.pdf")
    '
    '---------------------------------
    Dim sMyDefPrinter As String
    On Error GoTo Err_RunReport
    'Save current default printer
    sMyDefPrinter = bGetRegValue(HKEY_CURRENT_USER, "Software\Microsoft\WIndows NT\CurrentVersion\Windows", "Device")
    ' Set default printer to PDF Writer
    bSetRegValue HKEY_CURRENT_USER, "Software\Microsoft\Windows NT\CurrentVersion\Windows", "Device", "Acrobat PDFWriter"
    'Setting value for PDFFileName in the re
    '     gistry stops file dialog box from appear
    '     ing
    bSetRegValue HKEY_CURRENT_USER, "Software\Adobe\Acrobat PDFWriter", "PDFFileName", sPDFPath + sPDFName
    'Run the report
 
 
    DoCmd.OpenReport rptName, acViewNormal
Exit_RunReport:
        ' Restore default printer
        bSetRegValue HKEY_CURRENT_USER, "Software\Microsoft\WIndows NT\CurrentVersion\Windows", "Device", sMyDefPrinter
        Exit Function
Err_RunReport:
        MsgBox Err.Description
        Resume Exit_RunReport
    End Function

                                  
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:

Select allOpen in new window

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-09-24 at 14:14:54ID24760122
Topics

Access Reports

,

Microsoft Applications

,

Access Coding/Macros

Participating Experts
3
Points
500
Comments
19

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Crystal Report(RPT) to PDF Conversion
    Thanks that you take notes to my message. I just want to ask about that how to use the crystal report controls or dll to convert a RPT file into PDF file?Or we need other controls or active X?I am hereby to thanks all of you. I need this solution, thanks.
  2. export Crystal Report (.rpt) to (.pdf) on unix platform
    Hi, does anyone know how (if it is possible) to export an existing Crystal Report (.rpt) file to a .pdf file format under Unix/Linux/Solaris platform? Note that I don't want to have to create a web server to do this. I just want to know if Crystal provides any Unix API wher...
  3. calling crystal reports (.rpt) from (within) oracle to ge…
    I have a requirement of replacing a com object which is being used in my oracle environment to perform the job of generating pdf report given a .rpt file, with an alternative preferably using java. Now that we are upgrading from crystal reports 8.5 to crystal reports XI, also...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: kelvinsparksPosted on 2009-09-24 at 14:27:33ID: 25418140

In the Line bSetRegValue HKEY_CURRENT_USER, "Software\Microsoft\Windows NT\CurrentVersion\Windows", "Device", "Acrobat PDFWriter"

You have not chnaged the name of the pdf writer to Adobe PDF - the name of the printer name on your PC the code wants to change your printer to adobe but you've passed an invalid name.


Kelvin

 

by: boag2000Posted on 2009-09-24 at 22:57:41ID: 25420283

As far as using the student name as the PDF name, you must loop the Students table and grab the Student name in a variable then substitute it for part of the pdf Name argument.

I use this: http://www.lebans.com/reporttopdf.htm
So you must copy these DLLs to the same folder as the db.
http://www.lebans.com/DownloadFiles/UpdatedDLLs.zip

Sample DB is attached.
(You must create a "c:\YourFolder\" folder to test this.
Or simply change the code to "c:\" ...to sent the PDF's to your root folder directory.

I am sure you can adapt this to work in your database.

;-)

JeffCoachman

 

by: Helen_FeddemaPosted on 2009-09-25 at 06:57:12ID: 25422977

Here is some sample code for creating a save name from values in a listbox or recordset:

   strShortDate = Format(Date, "m-d-yyyy")
   'Value from a column in a listbox
   strCustomerName = Nz(lst.Column(10, varItem))
   'Value from a field in a record
   strCustomerName = Nz(rst![CustomerName])
   strDocType = "Invoice"
   strSaveName = strDocType & " to " & strCustomer _
      & " on " & strShortDate & ".doc"
                                              
1:
2:
3:
4:
5:
6:
7:
8:

Select allOpen in new window

 

by: mytfeinPosted on 2009-09-25 at 07:35:49ID: 25423378

Good morning,

Thank you Everybody for writing,

I need to try your ideas and get back to you soon,

Re: saving report to pdf
====================

One thing, I would like to say, is late yesterday, when I ran the code from  the link from Planet source
is that I think it shut off my default printer setting bec.

I have a vba script that runs in the morning via Windows scheduler to print an Excel spreadsheet,
and when I came in the morning, the code was paused in the excel spreadsheet waiting for me
to choose a printer.

I went to control panel, and chose my default printer, and my script ran fine.

At home, I was thinking, that I am not sure if the planet source code does a save as to pdf file,
or just converts and displays the pdf file on the screen.

If the planet source code does not do a save as to pdf, I could always tinker with with the SendKeys
to save as, but maybe Jeff is providing an alternate approach, so I have to look into Jeff's approach
this morning....

Re: customizing file name
====================
Helen, thx for the code

Helen and Jeff:
===============
I know this is off topic and I plan on opening a new post.
Can I please let you know when I post the question?

It relates to printing pictures:
=======================
I need to print a report that is 4 pictures across and 4 pictures down.
Currently the user uses Merge feature in Word. User created a doc with a table to hold pictures.
User told me that Word itself cannot merge pictures into a Word doc so they purchased a
Word AddIn call OnMerge.

I'm looking for alternate approaches to produce a "photo album"  by clicking a button on an Access form.

Many years ago, I copied from Helen's website vba to print Word labels.
I am aware that in Access I can print columnar reports.
Our pictures reside in a server folder not in a database.

So since Helen replied here, and Jeff has broad knowledge, I wanted to "flag" you guys here,
if I can alert you when I post this question.

tx and will  get back to you on the above ideas, tx, s

 

by: mytfeinPosted on 2009-09-25 at 07:58:23ID: 25423661

Hi Kelvin,

I followed your suggestion and changed printer name to "Adobe PDF" bec. that's what's mentioned
in Control Panel/Printers.

I ran:
Call RunReportAsPDF("rpt_Test", "C:\ARG_09_M4_Graph\", "test.pdf")

and got error msg in the screen shot below.... any thoughts...?

 

by: mytfeinPosted on 2009-09-25 at 08:44:32ID: 25424172

Hi Jeff,
thx for your instructions - i go tthe pdfs to print, thx so much!

... i do have questions, though, i'd like to describe my actions so far for any future EE reader:

a) downloaded you mdb to myfolder
b) downloaded zip file    to myfolder
c) extracted dll                to my folder
d) created a folder in c:\  called "YourFolder"
e) changed mdb startup setting to NONE for form, so that form does not show automatically when open
    mdb
f) click on form button, getting the error msg for msgbox, so I commented out the last line
       MsgBox "There was an error executing the command." _
    & vbCrLf & vbCrLf & "Error " & Err.Number & ": "
    '& vbCrLf & vbCrLf & "Error, vbExclamation

g) then got an error msg for Space command, pls see below
     so I went to vba/tools/references   and saw that next to Outlook it says MISSING
     so I unchecked OUTLOOK, bec. we use Lotus Notes here
h) then clicked on button, and saw pdfs flash, and msgbox of Done
i)  checked YourFolder and see 2 pdfs
j) thank you for customizing an mdb for me with a DAO loop for student table which is what I'll have
   to do, as well

k) quest, pls:   and pls advise if you'd like new posts for these quests:
=========================================================
    a) do you keep your pdfs in a sep folder like Your folder   or as a subfolder in the folder that
        your mdb is located in
    b)  what does "avoids the endless loop" mean in this code snippet window?
    c) In second screen shot, you see components of mdb
        1) I need to import the Lebans pdf logic into an exisitng mdb
              do i import the modules that are named "mod"
        2) Is the class module your generic routines that you use for your projects that is not
            required for me to import into my mdb?
        3) I will copy and adjust the code behind the form's button window
     
Pls advise on (k) and if you want (k) in a sep. post,
tx, s
           
   

'Recordset Cleanup Code
rst.Close
Set rst = Nothing
dbs.Close
Set dbs = Nothing
 
Exit_cmdEmailIndividualCustReports_Click:
    'Avoids the endless loop
    DoCmd.Echo True
    On Error Resume Next
    rst.Close
    Set rst = Nothing
    dbs.Close
    Set dbs = Nothing
    Exit Sub
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:

Select allOpen in new window

 

by: boag2000Posted on 2009-09-25 at 09:53:29ID: 25424757

Thanks for the kind words.

One of the great things about being here is that caliber of the Experts here is very high.

There are always many ways to accomplish the same thing.

It all depends on things like your exact needs, your skill level, ...etc

The main goal of most of the Experts here is getting you the solution that you need, with little regard to ego.
;-)

Now for your questions:

First I will apologize for the "Email" portions of this code.
I had quickly copied this from a sample I made for emailing reports as pdf with the Customer Names as part of the PDF file name.
On the other hand, I will congratulate you for being savvy enough to adapt the code without any outside help!
;-)


< do you keep your pdfs in a sep folder like Your folder   or as a subfolder in the folder that your mdb is located in>
I keep the PDF's wherever they are best located.
If you will be generating lots of PDF, perhaps you should make a dedicated "PDF" folder somewhere.

<what does "avoids the endless loop" mean in this code snippet window?>
If the code errors before the cleanup code you can end up with unused memory hanging out from the unclosed recordset.
I also used "Echo False" here to hide the report  in case a developer was not using Access 2003. In Access 2003 the OpenReport command has a "Hidden" argument, that you can use to hide the report.
On Error Resume Next is a quick way to say: "If you get an error, move to the next command."

<do i import the modules that are named "mod">
Yes

< 2) Is the class module your generic routines that you use for your projects that is not required for me to import into my mdb?>
The Class module is included from the Author, Stephen Lebans (http://www.lebans.com/)
It contains Common dialog box code.
In the interest of maintaining the "Integrity" of the Project, I see no reason to remove it.

JeffCoachman

 

by: mytfeinPosted on 2009-09-25 at 10:18:01ID: 25424993

Hi Jeff,

a) It looks like the code does not allow ** subfolder within **  a folder, do you agree?
IOW:
i tried: "C:\ARG_09_M4_Graph\ARG_09_M4_Graph_pdfs\"
                     instead of
           "C:\YourFolder\"
  and the pdfs did not go to subfolder.

if I did a different (you called it "root"?) folder like this: "C:\ARG_09_M4_Graph\"
       pdfs went there successfully.

I have another related question, relating to opening a report hidden?
Can I let you know when it is posted? tx, s

 

by: mytfeinPosted on 2009-09-25 at 10:20:53ID: 31633283

Thank you everybody for your help...s


 

by: mytfeinPosted on 2009-09-25 at 10:29:38ID: 25425121

Hi Jeff, new post related to this post based on your code
    can u look at if u have a minute? tx! s

http://www.experts-exchange.com/Microsoft/Development/MS_Access/Access_Reports/Q_24762463.html

 

by: boag2000Posted on 2009-09-25 at 11:13:58ID: 25425618

The code worked fine for me going to a subfolder.
Make sure you reference it in all places with the trailing" "\"

Also make sure you have rights/permissions to this subfolder.

Finally, check your "speeling"
;-)

Jeff

 

by: mytfeinPosted on 2009-09-25 at 11:46:09ID: 25425960

Thank you so much, Jeff...

You are right it was a typo.... I misspelled my sub-folder name

I imported all the components to my actual mdb and tested it there. Work fine!

I will be off on Mon. I will continue to adapt this solution for business needs.
    First  will work with creating pdf report for an individual student when chosen from combo box
    Then will create a DAO loop/script to create files for an entire class
                   (each student's pdf will live in a target folder)
                               thx again and have a nice weekend, s

mental note for next week... may open a post to ask if I can bulk print these pdf files, to give the user an option of printing them off the folder thru .bat file, scripting commands etc....

tx everybody, have a nice weekend, s



 

by: boag2000Posted on 2009-09-25 at 17:56:52ID: 25428344

Again, I would like to emphasize how impressed I was that you were able to do most of the work on your own.
Then to follow up by asking great questions that will help you understand how things are working.

So in honor of your efforts, I have updated my sample file and removed all references to "Email".
In this way future generations of askers here will benefit from your wisdom.
;-)

JeffCoachman

 

by: boag2000Posted on 2009-09-25 at 19:35:38ID: 25428551

To adress your "printing PDF" concern.

Please remember that Printing the actual Report or the PDF from that report will result in the *exact same printout*.

It is *Far* easier to print the report than it is to print the pdf.

The main reason why you should need a pdf is that you need to "Give" someone an exact "snapshot" file of the data/report, at a particular time.

But again, I know how you are, ;-) , don't take my word for it, try it out for yourself.
Print the report, then run this code.
Open the PDF and print it from Adobe.
Compare the two...


Batch printing the reports only requires you to modify the code to loop the Report Open command:
DoCmd.OpenReport strReportName, acViewNormal, , "CustomerID=" & "'" & strCustID & "'"

;-)

JeffCoachman

 

by: mytfeinPosted on 2009-09-29 at 07:49:58ID: 25449645

Hi Jeff,

a) I was off on Mon. and over the weekend I was thinking of posting a revised mdb
(w/o Outlook reference and commenting out one of the lines of code in the msgbox at
 error handling)

This morning I see that you did the same for your sample mdb.
Perhaps you want to upload yours....

In the meantime, below is modified mdb.

Also for EE reader, is a screen shot of windows explorer. I highlighted the components
that you need (the rest of window explorerer you can ignore)

Mr. Lebans approach to pdf is wonderful and is quoted alot in EE. G-d bless him.
G-d bless Jeff for his very clear way of explaining how to use Mr. Lebans approach and for
Jeff's sample mdb which is very clear as well.

b) Jeff, will be back shortly to comment on your print-pdfs comment.
    I have to digest your remarks.

    tx, s

   
   

 

by: mytfeinPosted on 2009-09-29 at 08:12:05ID: 25449930

Hi Jeff,

In June 2009, I asked a similar question FROM THE EXCEL side about printing the file and
saving as a pdf.

In that post you explained as you do in this post, that it's EASIER to print from EXCEL than to print
from pdf screen, after one has save Excel to pdf. I tried to print from pdf using Sendkeys and it
did not work properly, so I ended up printing spreadseeht using Excel print command.

So, without having testing printing Access report after it has been converted to pdf, I will agree
with you, that it's probably EASIER to print from within Access.

The reason for my quest. is a bit diff, and should open a new post:
======================================================

You see, at this time, my users are not really Access users.  This is an Oracle environment and
they use a very expensive web app. to do their work. I transferred from another dept bec. they
had an opening. I was working with Access 2000 in old dept.

So for their missing functionality, I am approaching with Access 2003.
One day, I may put all my functions on an Access menu and install on users machines.

Right now, I'm running off my pc.

And so:
=======
Once I get the DAO routine to work to bulk print to pdf files.
I will have 200 pdfs living in a folder on a server.

The user is used to going to server "for things" so if they need a student's report they can go to the
server.

I want to post, if there is a way to code a .bat file or script language to loop thru the folder
and print ALL 200 pdfs w/o the user doing manually, if adobe allows parameters for printing like
i'm making this up:
    shell c:\program files\adobe\adobe.exe   "pdf file name" -p  (where -p would be to print)

tx, s




 

by: mytfeinPosted on 2009-09-29 at 08:13:22ID: 25449948

oh Jeff,

this is the link that i forgot to post above where you advised about printing from Excel and not from
Adobe:  tx, s

http://www.experts-exchange.com/Microsoft/Development/MS_Access/Q_24530934.html

 

by: mytfeinPosted on 2009-09-29 at 09:35:30ID: 25450804

 

by: mytfeinPosted on 2010-09-20 at 09:11:30ID: 33717775

Hi Jeff,

I have a related question here:

http://www.experts-exchange.com/Microsoft/Development/MS_Access/Q_26485626.html#a33717762

If you have a minute, can you take a look, pls?

tx, sandra

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...