[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Access 2003 Report - vba to loop through records in query and save each as a separate report

Posted on 2009-04-08
12
Medium Priority
?
2,677 Views
Last Modified: 2013-11-28
I need help with the vba loop to produce an individual report for each record in a query.

I have a report named rptMissingReports based on a query named qry_rpt_MissingReports.  The report currently has a page break for each record in the query. So, if there are 50 records in the query, there are 50 pages in the report.  I would like to save (print to pdf) each page as a separate report.  

Rather than prompt the user for a file name each time a pdf is generated, I'd like it to save to c:\temp\ with a file name comprised of 2 of the field names - [name]_[number].pdf

0
Comment
Question by:kathy1234
[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
  • 6
  • 4
  • 2
12 Comments
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 24100452
post the codes that you have for creating the pdf report, so we can just edit it.

dim rs as dao.recordset
set rs=currentdb.openrecordset("queryname")

rs.movefirst
do until rs.eof
     docmd.openreport "reportX",,,"[name]='" & rs("name") &"'"

rs.movenext
loop
0
 

Author Comment

by:kathy1234
ID: 24100870
All users have "print to pdf" as a printer driver, and I just set the report to default to that printer.  So, usually, when I click print, it creates the pdf and I provide a file name and save location.  

In this instance, I'm clicking a command button on a form to run the code.  The code you provided works well for the first record (if I provide the file name and save location) but it is not always working for the next record - several times it printed the form with the command button rather than the next record.

Thanks.
Function PolicyMissingReports()
On Error GoTo PolicyMissingReports_Err
 
Dim rs As dao.Recordset
Set rs = CurrentDb.OpenRecordset("qry_rpt_MissingReports")
 
rs.MoveFirst
Do Until rs.EOF
    DoCmd.OpenReport "rptMissingReports", , , "[name]='" & rs("name") & "'"
    DoCmd.PrintOut acPrintAll, , , acHigh, 1, True
 
rs.MoveNext
Loop
 
 
 
PolicyMissingReports_Exit:
    Exit Function
 
PolicyMissingReports_Err:
    MsgBox Error$
    Resume PolicyMissingReports_Exit
 
End Function

Open in new window

0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 24100941
we can do this
dim newReport as string

docmd.CopyObject,"Newname",acReport,

Do Until rs.EOF

    newReport=rs("name") & "_" & rs("number")

    docmd.CopyObject,newReport,acReport, "rptMissingReports"

    DoCmd.OpenReport newReport, , , "[name]='" & rs("name") & "'"
    DoCmd.PrintOut acPrintAll, , , acHigh, 1, True
 
rs.MoveNext
Loop


the problem is you are going to have multiple reports created, but you can delete them using vba too..


0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 24100945


we can do this


dim newReport as string


Do Until rs.EOF

    newReport=rs("name") & "_" & rs("number")

    docmd.CopyObject,newReport,acReport, "rptMissingReports"

    DoCmd.OpenReport newReport, , , "[name]='" & rs("name") & "'"
    DoCmd.PrintOut acPrintAll, , , acHigh, 1, True
 
rs.MoveNext
Loop


the problem is you are going to have multiple reports created, but you can delete them using vba too..
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 24102933
kathy1234,

You can't simply "Create" a PDF Report in Access 2003.
You need a utility to create PDF files.

I use this:
http://www.lebans.com/reporttopdf.htm

Here is a sample of what I use.
When you click the form button, the code will save the PDF reports to your: C:\  root folder, with the individual CustomerID added to the filename.

let me know if something like this is what you are after.
;-)

JeffCoachman
Access-BulkConvertIndividualCust.mdb
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 24103124

<All users have "print to pdf" as a printer driver, and I just set the report to default to that printer.  So, usually, when I click print, it creates the pdf and I provide a file name and save location.  >
0
 

Author Comment

by:kathy1234
ID: 24105457
Hi guys.  Thanks for your input.  Yes, I can create a pdf by just selecting it as a printer.  The problem is that it isn't consistently selecting only the next record and creating the report.  The 3rd response from capricorn1 sort of worked, but it really inflated the size of the database and some reports had two records, etc.

Having slept on this, I'm wondering now whether I need to go back and look at my report to see if I can get it to loop through the records one at a time in print preview mode - forget about the pdf printing until I get it to loop through the records consistently and correctly.  

Thanks for your help so far.  I'll work on this today - I may be in back with a different approach.  : )
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 24106227
you can delete the report generated after printing..
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 24106426
capricorn1's post will work, all you need to do is explictly close the recordset, and set it to Nothing.

My guess is that he posted it just to see if it was applicable.

As the original poster, I am sure capricron1 can tweak the code for you.

;-)

0
 

Author Comment

by:kathy1234
ID: 24107319
I've made some changes and am using the CopyObject version you suggested above.  The code below works fine except that I have to click "enter" after each report is printed - but I could probably do a "Send Keys" line for that, couldn't I?

I'm still concerned about how this method dramatically increases the size of the database.  I compacted the database and started out at 2.07MB.  I ran 6 reports and the size went to 8.12MB.  We may be running hundreds of reports.

Would it be possible to programmatically rename the report each time rather than copy it?  So, I'd start with rptMissingReports, rename it  to rs("name") & "_" & rs("number"), open it, print, close, and move to next.

Thanks so much for the help and your patience.


Option Compare Database
Option Explicit
Function PolicyMissingReports()
On Error GoTo PolicyMissingReports_Err
 
Dim newReport As String
Dim rs As dao.Recordset
Set rs = CurrentDb.OpenRecordset("qry_rpt_MissingReports")
 
rs.MoveFirst
Do Until rs.EOF
 
    newReport = rs("name") & "_" & rs("number")
    DoCmd.CopyObject , newReport, acReport, "rptMissingReports"
    DoCmd.OpenReport newReport, acViewPreview, "", "[name]='" & rs("name") & "'", acWindowNormal
    DoCmd.PrintOut acPrintAll, , , acHigh, 1, True
    DoCmd.Close acReport, newReport, acSaveNo
    DoCmd.DeleteObject acReport, newReport
 
rs.MoveNext
Loop
 
 
 
PolicyMissingReports_Exit:
    Exit Function
 
PolicyMissingReports_Err:
    MsgBox Error$
    Resume PolicyMissingReports_Exit
 
End Function

Open in new window

0
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 2000 total points
ID: 24107499
you can rename the report with

docmd.rename "Newname" ,acreport, "reportName"


0
 

Author Closing Comment

by:kathy1234
ID: 31568179
I think this is going to work!  I let it loop through 50 reports with no additional increase in the size of the database.  Thanks.
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

649 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