Results 1 to 3 of 3
  1. #1
    Pjarman is offline Novice
    Windows 10 Access 2016
    Join Date
    Nov 2020
    Posts
    6

    Save Report PDF using Table to build title

    I have a report and I am trying to use a button to select which report I need by a unique ID and have it saved using fields within my table the following code works to save the report.

    Private Sub Command119_Click()




    Const sReportName As String = "LandDipClearance"
    Const sFile As String = "S:\Defence\Restricted\7. Movements\Land DipClear Requests\PtD_Clearance.pdf"

    DoCmd.OpenReport sReportName, acViewPreview, , "[Enter PtD ID] = [PtD_ID]", acHidden
    DoCmd.OutputTo acOutputReport, sReportName, acFormatPDF, sFile
    DoCmd.Close acReport, sReportName, acSaveNo

    End Sub

    I have tried to add the build just using Unit to be included in the save file and I get "Run-time error '2501': The OutputTo action was cancelled." and when I debug the Docmd line is highlighted.

    Private Sub Command121_Click()

    Dim rs As DAO.Recordset
    Dim sFolder As String
    Dim sFile As String
    Const sReportName = "LandDipClearance"

    sFolder = Application.CurrentProject.Path & "S:\Defence\Restricted\7. Movements\Land DipClear Requests"

    Set rs = CurrentDb.OpenRecordset("SELECT DISTINCT PtD_ID, Unit FROM LandDipClearancetbl", dbOpenSnapshot)
    With rs
    .MoveFirst
    Do While Not .EOF
    sFile = sFolder & "" & Nz(![Unit], "") & ".pdf"
    DoCmd.OpenReport sReportName, acViewPreview, , "[Enter PtD ID No] = [PtD_ID]", acHidden
    DoCmd.OutputTo acOutputReport, sReportName, acFormatPDF, sFile
    DoCmd.Close acReport, sReportName, acSaveNo
    .MoveNext
    Loop
    End With


    Application.FollowHyperlink sFolder
    End Sub


    When I delete the sFile in the DoCmd.OutputTo row it gives me the option to save in the right folder with the file name LandDipClearance.pdf, it does this twice and then error message and the Application.FollowHyperlink sFolder is highlighted.

    The end result I am after is the report would be saved using PtD_ID, Unit and Date of Entry e.g. DS-REF-0001_25EngrGp_21/07/2021.pdf

    Any advice or assistance in this would be greatly appreciated.

    Regards

    Peter

  2. #2
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,001
    Quote Originally Posted by Pjarman View Post
    DoCmd.OpenReport sReportName, acViewPreview, , "[Enter PtD ID] = [PtD_ID]", acHidden
    I'm amazed this works as you aren't concatenating the string correctly.
    This would make more sense assuming PtD_ID is on the form

    Code:
    DoCmd.OpenReport sReportName, acViewPreview, , "[Enter PtD ID] = " & Me.[PtD_ID], acHidden
    In your recordset loop you will need something like

    Code:
    DoCmd.OpenReport sReportName, acViewPreview, , "[Enter PtD ID] = " & .Fields("PtD_ID") , acHidden
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    Regarding your error, put a message box or Debug.Print on the file variable after it's set. I suspect you'll see the problem.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Replies: 1
    Last Post: 10-16-2017, 07:38 AM
  2. Replies: 2
    Last Post: 07-15-2015, 08:35 PM
  3. Replies: 2
    Last Post: 03-20-2015, 06:30 AM
  4. Replies: 4
    Last Post: 11-14-2013, 04:23 PM
  5. report title
    By slimjen in forum Reports
    Replies: 4
    Last Post: 10-07-2013, 01:37 PM

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