Solved

filtered PDF report going to wrong file location

Posted on 2011-02-27
7
412 Views
Last Modified: 2013-11-28
Hi. I'm using found code to send a PDF report from Access to a specified file location. I've modifed it to filter on a field called Program and send the created PDF to a file location based on Program value.  It appears to work except it sends the first Program to the second file location, second Program to third file location, etc.  Any ideas what I've done wrong?  Here is what I think are the relevant aspect of the code.

Naturally, I'm under the gun, so I'm hoping for quick miracles!

    Dim rs As DAO.Recordset, pdfName As String, MyFilter As String, MyFilename As String, MyPath As String

Set rs = CurrentDb.openrecordset("unique_programs_distinct")

If rs.RecordCount > 0 Then

rs.MoveFirst
Do Until rs.EOF
    pdfName = rs!Program & "_Review.pdf"
    MyFilter = "[Program] ='" & rs!Program & "'"
    MyPath = rs("path")
   
    DoEvents
   
 Rem  -- a bunch of printer settings to reset the printer to a PDF printer

    Rem -- Configure the PDF printer
    With oPrinterSettings
        Rem -- Set the destination file name of the PDF document
        .SetValue "output", MyPath & pdfName

    End With
   
    Rem -- Run the report
    DoCmd.OpenReport "THE_WHOLE_SHABANG", , , MyFilter, acHidden
   
    rs.MoveNext
Loop
Else
 MsgBox "No records found"
 rs.Close
 Set rs = Nothing
 Exit Function
 End If
 
 MsgBox "All PDFs created"
 
rs.Close
Set rs = Nothing

End Function
0
Comment
Question by:SDC_NC
[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
  • 4
  • 2
7 Comments
 
LVL 6

Expert Comment

by:TinTombStone
ID: 34992739
You are looping through a recordset
First loop equates to the first record
Second loop equates to the second record etc.

So,  rs("path") and rs!Program will refer to whatever is in thoses fields for a given record

Presumably rs("path"), if you check, will be differant for each
0
 

Author Comment

by:SDC_NC
ID: 34993117
I'm not a regular coder, so I'm not seeing where my loop is jumping from first to second.  The table Unique_Programs_Distinct includes fields for path and program.  Do I need to move the Loop command?  Where so that it keeps the same record values for both path and program?

0
 
LVL 85
ID: 34993119
Could also be a timing issue - the value of the path may not be completely "set" before your call to the Print function.

What version of Access are you using? 2007 and 2010 now have builtin PDF printing capabilities.
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:SDC_NC
ID: 34993247
I have updated the rs to Set rs = CurrentDb.openrecordset("qu Select Unique Programs")

This query is

SELECT Unique_Programs_Distinct.School, Unique_Programs_Distinct.DPT, Unique_Programs_Distinct.Program, Unique_Programs_Distinct.path
FROM Unique_Programs_Distinct
ORDER BY Unique_Programs_Distinct.School, Unique_Programs_Distinct.DPT, Unique_Programs_Distinct.Program;

Now rs("path") and rs("program") should be from the same record, but I still get a report that contains the first program named for and put in the second file.
0
 

Author Comment

by:SDC_NC
ID: 34993257
I'm using 2007.  I had developed a different process using     DoCmd.OutputTo acOutputReport but it would throw an error after the first record.  I found this other solution that seemed to get me closer, but for this problem I'm describing here.
0
 
LVL 85

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 500 total points
ID: 34993426
You'd be much better off using the builtin PDF capabilities of Access 2007.

If you open the query "qu Select Unique Programs" directly in the query editor, does it return the correct data, with the correct paths reported? If it does not, then you'll need to fix that query before your code will work correctly. It looks as if that query is built from another query (Unique_Programs_Distinct), so you may have to go several levels deep.

IMO you're far better off using OutputTo. AFTER you've made sure that your queries are working correctly, you'd do this:

Dim rst As DAO.Recordset
Set rst = Currentdb.OpenRecordset("SELECT SomeField FROM SomeTable")

Do Until rst.EOF
  DoCmd.OpenReport "ReportName", acViewPreview, , "YOurReportField=" & rst("YourRecordsetField"), , acHidden
  DoCmd.OutpTo acOutPUtReport, "" acFormatPDF, rst("Path") & "\Review.pdf", True
  DoCmd.Close acReport, "ReportName"

  rst.MoveNext
Loop

This is a much better alternative than trying to mess with 3rd party PDF printers, which will have to be distributed to anyone using your applications.
0
 

Author Closing Comment

by:SDC_NC
ID: 34993614
THANK YOU - made modifications as need for field names and it worked exaclty as I needed.  You're a life-saver
0

Featured Post

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!

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…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
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…

628 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