Results 1 to 5 of 5
  1. #1
    jcc285 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2014
    Posts
    70

    Angry Output Report to PDF file

    Tearing my hair out!! I use the following code to Open a report, specifically, using the Where clause to select an individual record as:

    DoCmd.OpenReport strReportName, acPreview, , strWhere

    There is a sub-report in the output linked by Main and Child fields and it all works fine and I get the Report Preview, including correct sub-report, on screen

    Now what I want to do is Output a report for each record as a PDF file. So next line of code:

    DoCmd.OutputTo acOutputReport, strReportName, acFormatPDF, strFileOut, True

    However, when this is run I get the following error 'Runtime error 2501 - The OutputTo Action was cancelled'

    I don't need to Preview the report so could omit this step but, in that case, I don't know how to pass the WHERE clause and just create the file. This did not ought to be difficult!!!



    Full Test Code as below, hope it makes sense.

    Private Sub Test()

    Call Initialise_Variables ' Collects str_MemberNumber as Global variable

    Dim strReportName As String
    Dim strSQL As String
    Dim strWhere As String
    Dim strFileOut As String
    Dim db As DAO.Database
    Dim rst As DAO.Recordset

    strReportName = "rpt_Distribution_Areas_AllDistributors"
    strSQL = "SELECT Membership_Number FROM tbl_Distribution_Volunteers"
    Set db = CurrentDb
    Set rst = db.OpenRecordset(strSQL)
    Do Until rst.EOF
    str_MemberNumber = rst.Fields("Membership_Number")
    strWhere = "[Membership_Number]=""" & str_MemberNumber & """"
    strFileOut = "C:\Temp\" & str_MembershipNumber & ".lst"

    DoCmd.OpenReport strReportName, acPreview, , strWhere
    DoCmd.OutputTo acOutputReport, strReportName, acFormatPDF, strFileOut, True 'This line throws Runtime error 2501

    rst.MoveNext

    Loop 'Breakpoint inserted here for testing so only first report generated.

    rst.Close
    Set rst = Nothing
    Set db = Nothing

    End Sub

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,549
    Instead of using record set code, use a form.
    a list box has your changing params. (Members)
    the report qry reads the list box to pull the member via param.
    ...where [member] = forms!frmRpt!lstMem

    the code scans the list box changing to each value,
    the query sees the value
    the report prints only that member.

    i use this method for my PDF reports. It works.

  3. #3
    Dave D is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Jun 2014
    Posts
    45
    Try this; very similar to yours, but has slight modifications.

    I use this to bring up, as hidden, the report

    Code:
    DoCmd.OpenReport "rptName", acViewPreview, , strWhere, acHidden
    I put this code in a function within a module (modReports). The function takes the rptName as a string parameter. I can then use this same function to manage how different reports are displayed or stored by using a Select statement.

    Code:
    DoCmd.OutputTo acOutputReport, rptName, "*.pdf", strPath & "myFile.pdf", True
    Be sure to set the variable, strPath, to a valid file directory. I use this:
    Code:
    strPath = CreateObject("WScript.Shell").SpecialFolders("Desktop") + "\"
    Last edited by Dave D; 03-30-2015 at 07:50 AM. Reason: extra info

  4. #4
    jcc285 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2014
    Posts
    70

    Thumbs up Output Report to PDF file

    Yes, OK. I've moved to a List Box Selection on a Form. Then re-coded all to either display the report for viewing or Output to PDF File.

    Thanks for your suggestion

    Jcc285

  5. #5
    jcc285 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2014
    Posts
    70
    Thanks for your input but I have gone down the route suggested by ranman256.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 4
    Last Post: 04-23-2014, 04:35 PM
  2. Replies: 2
    Last Post: 03-16-2014, 02:12 PM
  3. variable filepath for output file
    By Jaron in forum Programming
    Replies: 6
    Last Post: 07-31-2013, 06:26 PM
  4. output file name
    By AdrianoG87 in forum Reports
    Replies: 4
    Last Post: 11-03-2011, 06:20 PM
  5. How would you output an XML file with an Ole Attachment?
    By techneophyte in forum Programming
    Replies: 7
    Last Post: 09-09-2010, 09:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums