Solved

filtered PDF report going to wrong file location

Posted on 2011-02-27
7
410 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 84
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
Independent Software Vendors: 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!

 

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 84

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

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

Suggested Solutions

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…

756 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