Results 1 to 14 of 14
  1. #1
    wwconslt is offline Novice
    Windows 10 Access 2016
    Join Date
    Apr 2017
    Posts
    9

    Trying to Export Current Record Report to PDF From Form Button

    Hello Everyone,



    Access 2019

    I have a button on a form. When clicked, I would like the current record to open in a report I created and export to PDF format.

    When I click the button, I get "The output to was canceled." error message.

    Here is my code for the button:

    ==============

    Private Sub cmdPrint_Click()
    On Error GoTo Err_cmdPrint_Click
    If Me.Dirty = True Then
    DoCmd.RunCommand acCmdSaveRecord
    End If

    DoCmd.OpenReport "rptRecord", acViewReport, , "[MASTER_ID] = [Forms]![frmMain]![MASTER_ID]"
    DoCmd.OutputTo acOutputReport, "rptRecord", acFormatPDF, "C:\Users\Current Record.pdf", False
    DoCmd.Close acReport, "rptRecord"




    Exit_cmdPrint_Click:
    Exit Sub


    Err_cmdPrint_Click:
    MsgBox Err.Description
    Resume Exit_cmdPrint_Click

    End Sub

    =============

    Thank you for any and all help!

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,933
    Shouldn't that be
    Code:
    "[MASTER_ID] = " &  [Forms]![frmMain]![MASTER_ID]
    
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    If that doesn't work, then master id is probably not a number.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #4
    wwconslt is offline Novice
    Windows 10 Access 2016
    Join Date
    Apr 2017
    Posts
    9
    Thank you for your help. MASTER_ID is an autonumber in the table record source.

    When I use this code to preview the current record in the report it works fine.

    DoCmd.OpenReport "rpt_Record", acPreview, "", "[MASTER_ID]=[Forms]![frmMain]![MASTER_ID]"
    DoCmd.Maximize

    But the end user would like the report to be exported straight to a pdf file without having to preview it first.

  5. #5
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    Well, IMO it should not work at all when written like that. I would suggest you try suggestion in post 2 and see if it makes a difference when not actually opening the report.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,933
    Can you actually write to that folder?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  7. #7
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Maybe try to open the report hidden in print preview instead of report mode and save it in the same folder as the front-end (as you know you have permissions there):
    Code:
    DoCmd.OpenReport "rptRecord", acViewPreview, , "[MASTER_ID] = " & [Forms]![frmMain]![MASTER_ID], acHidden
    DoCmd.OutputTo acOutputReport, "rptRecord", acFormatPDF, CurrentProject.Path & "\Current_Record.pdf", False '"C:\Users\Current Record.pdf", False
    DoCmd.Close acReport, "rptRecord"
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  8. #8
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    Embedding a full path reference to control within quote marks does work. Access is able to evaluate the path. It was a surprise to me as well when I first discovered that.

    The acCmdSaveRecord should work but another version is:

    If Me.Dirty Then Me.Dirty = False

    Review http://allenbrowne.com/bug-01.html

    As already pointed out, folder permissions may be issue, otherwise, if you want to provide db for analysis, follow instructions at bottom of my post.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    wwconslt is offline Novice
    Windows 10 Access 2016
    Join Date
    Apr 2017
    Posts
    9
    I can write to the folder--as I have code to export the whole data set to the same folder. I'm also able to open and preview the report with the record selected on the form. It just won't export to pdf.

  10. #10
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    Well, cannot replicate issue with my db. So maybe provide yours as suggested?

    Have you tried to a different folder? from a different computer?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  11. #11
    wwconslt is offline Novice
    Windows 10 Access 2016
    Join Date
    Apr 2017
    Posts
    9
    Thanks everyone for your help. I was able to find some code that works:



    Private Sub cmdPrint_Click()


    Dim reportName As String
    Dim criteria As String
    Dim MSG As String
    Dim FolderPath As String, fileName As String

    DoCmd.Save
    DoCmd.RefreshRecord




    reportName = "rpt_Record"
    FolderPath = "C:\Users"
    fileName = FolderPath & Me.[MASTER_ID] & ".pdf"
    criteria = "[MASTER_ID] = " & Me.[MASTER_ID]

    'Exports report as PDF to Folder Path with Code Number as the file name

    DoCmd.OpenReport reportName, acViewPreview, , criteria, acHidden
    DoCmd.OutputTo acReport, "rpt_Record", acFormatPDF, fileName, False
    DoCmd.Close acReport, reportName, acSaveNo



    MsgBox "Your file has been downloaded to C:\Users."





    Exit_cmdPrint_Click:
    Exit Sub



    End Sub

  12. #12
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,933
    You have no trailing \ for the folder?, so you are writing to the root of C: ?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  13. #13
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    So the only real significant difference is a dynamic instead of static filename. Destination folder is still same.

    I had no problem with static filename. Output just replaced file.

    Still cannot explain why original code failed.


    Note that FolderPath = "C:\Users" line in your posted code is missing ending \ character. Enclose code in CODE tags and that would not be dropped. Please use CODE tags, especially with longer code snips, to retain indentation and readability and not lose \ character.

    Unless you really did not have a \ character in code and actually are saving to C: root in which case "Users" becomes part of the file name.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  14. #14
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Sometimes I found (but this was with much earlier versions of Access) that using the actual value of the acFormatPDF (which is "PDF Format (*.pdf)") solved some unexpected problems\errors. In the OP's case I suspect it was the folder as I agree, the code posted saves the file in the root of C:\. I always tend to test exports like that in the same folder as the front-end as I know I have read\write permissions there.

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

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

Similar Threads

  1. Replies: 6
    Last Post: 02-01-2022, 04:38 PM
  2. Replies: 1
    Last Post: 05-15-2016, 10:17 AM
  3. Replies: 9
    Last Post: 09-17-2013, 11:08 PM
  4. Replies: 6
    Last Post: 05-18-2013, 12:48 AM
  5. Open Current Report With Form Button
    By Rick5150 in forum Reports
    Replies: 8
    Last Post: 10-12-2011, 02:28 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