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
Solved

filtered PDF report going to wrong file location

Posted on 2011-02-27
7
409 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 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Familiarize people with the process of utilizing SQL Server stored procedures 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 Micr…
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…

789 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