Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

filtered PDF report going to wrong file location

Posted on 2011-02-27
7
Medium Priority
?
415 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
  • 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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 

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 2000 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

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.
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

916 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