Solved

filtered PDF report going to wrong file location

Posted on 2011-02-27
7
406 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
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…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now