Results 1 to 15 of 15
  1. #1
    templeowls is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2019
    Posts
    305

    Issue with export button on a report

    Hi all. I've got a button on a report that allows the user to quickly export the report via PDF for their use outside of Access. It works great but has minor hiccup. The report is linked to a query that uses criteria. Basically its a report that allows the user to show all complaints from a particular customer. The user just has to type in the Customer ID.



    The issue is that the user has to put the ID in when opening the report and then after pressing the export button and selecting a file location to save, Access prompts the user again to input the customer ID. It would be ideal for it to not ask on the second time but I can't seem to figure out why its doing that or how to get it to stop.

    Here's the code for the button:

    Code:
    Private Sub btnExportReport_Click()    
        Dim reportName As String
        Dim fd As Object
        Dim filename As String
       
        reportName = "rptCustomerID"
    
        Set fd = Application.FileDialog(2)
        filename = "Customer Complaint History" & ".pdf"
        With fd
            .InitialFileName = "\Documents\" & filename
            .Show
        End With
    
        DoCmd.OpenReport reportName, acViewPreview, , acHidden
        DoCmd.OutputTo acOutputReport, reportName, acFormatPDF, filename
        DoCmd.Close acReport, reportName, acSaveNo
    End Sub

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I suspect the OutputTo re-renders the report. Most of us use forms to gather user input. You have more control and it would solve this problem.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    ssanfu is offline Master of Nothing
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    @templeowls,
    Did you study the DoCmd.OutputTo command syntax? It took me a while to understand what was happening (it was a few years ago).

    I've cut out the non-necessary lines in your sub for this explanation.

    Code:
    Private Sub btnExportReport_Click()
       
        reportName = "rptCustomerID"
    
    1    DoCmd.OpenReport reportName, acViewPreview, , acHidden
    2    DoCmd.OutputTo acOutputReport, reportName, acFormatPDF, filename
        DoCmd.Close acReport, reportName, acSaveNo
    End Sub
    Line 1 - opens the report named "rptCustomerID" hidden. This report is now the active object. Apparently the Report has a parameter query as the report record source, which prompts for the Customer ID.

    Line 2 - the Output command - since you have the ObjectName paraneter as "reportName", the Output command re-opens the same report, resulting in the parameter request of the Customer ID (again).


    What worked for me is leaving the ObjectName blank as below.

    Code:
    Private Sub btnExportReport_Click()
       
        reportName = "rptCustomerID"
    
        DoCmd.OpenReport reportName, acViewPreview, , acHidden
                  'OutputTo(ObjectType,   ObjectName, OutputFormat, OutputFile
        DoCmd.OutputTo acOutputReport,                 , acFormatPDF, filename   '<<-- ObjectName parameter is blank
        DoCmd.Close acReport, reportName
    End Sub


    From Help: ObjectName parameter - A string expression that's the valid name of an object of the type selected by the ObjectType argument. If you want to output the active object, specify the object's type for the ObjectType argument and leave this argument blank



    Also......
    Code:
    DoCmd.Close acReport, reportName
    The "
    , acSaveNo" is not needed - it for saving programmatic changes to the design of the object (in this case the report).




    Hope I am clear in my explanation....

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Somehow my eye thought the OpenReport had a wherecondition, thus making it a necessary line. My eye was wrong.

    I wouldn't think you'd need anything but the OutputTo line since there's no wherecondition.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    templeowls is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2019
    Posts
    305
    Steve, I gave that a try but no luck. It's still requesting the customer ID after I save the export. Here's the code I used:

    Code:
    Private Sub btnExportReport_Click()        Dim reportName As String
        Dim fd As Object
        Dim filename As String
       
        reportName = "rptCustomerID"
    
    
        Set fd = Application.FileDialog(2)
        filename = "Customer Complaint History" & ".pdf"
        With fd
            .InitialFileName = "\Documents" & filename
            .Show
        End With
    
    
        DoCmd.OpenReport reportName, acViewPreview, , acHidden
        DoCmd.OutputTo acOutputReport,                 , acFormatPDF, filename
        DoCmd.Close acReport, reportName
    End Sub

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Have you tried deleting the OpenReport and Close lines?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    templeowls is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2019
    Posts
    305
    Quote Originally Posted by pbaldy View Post
    Have you tried deleting the OpenReport and Close lines?
    That worked!! Thank you!

    One more minor question...do you know of any way to get the dialog box to inherently 'know' that you're saving a PDF file? I ask cause when manually saving a PDF the dialog box has some unique settings shown. When exporting this report however, the dialog box isn't recognizing that its a pdf. It states file type as "all files"

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Shot in the dark:

    .Filters.Add "PDF files", "*.PDF"
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    templeowls is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2019
    Posts
    305
    I added it but am getting '438' error. See below code. No worries though, it's just a small issue

    Code:
    Private Sub btnExportReport_Click()        
        Dim reportName As String    Dim fd As Object
        Dim filename As String
       
        reportName = "rptCustomerID"
    
    
        Set fd = Application.FileDialog(2)
        filename = "Customer Complaint History" & ".pdf"
        With fd
            .InitialFileName = "\Documents" & filename
            .Filters.Add "PDF files", "*.PDF"
            .Show
        End With
    
    
        DoCmd.OutputTo acOutputReport,                 , acFormatPDF, filename
    End Sub

  10. #10
    ssanfu is offline Master of Nothing
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    After doing some searching, I found out that the msoFileDialogSaveAs dialog does NOT support file filters (msoFileDialogSaveAs=2) See this

    This code works for me with the report I created
    Code:
    Private Sub btnExportReport_Click()
        Dim fd As Object
        Dim reportName As String
        Dim filename As String
        Dim k As Integer
    
        reportName = "rptCustomerID"
        '    reportName = "Report1"  '<<-- my test report
    
        Set fd = Application.FileDialog(2)   ' 2 = msoFileDialogSaveAs
        filename = "Customer Complaint History" & ".pdf"
    
        With fd
            .Title = "Save to PDF"
            .InitialFileName = "\Documents\" & filename
            If .Show = -1 Then
                filename = fd.SelectedItems(1)
                'check for ext errors
                If InStr(filename, ".") = 0 Then   'no extension entered
                    filename = filename & ".pdf"
                ElseIf Right(filename, 4) <> ".pdf" Then
                    'change the current ext to pdf if ext <> ".pdf"
                    k = InStrRev(filename, ".") - 1
                    filename = Left(filename, k)
                    filename = filename & ".pdf"
                End If
    
            End If
        End With
    
        DoCmd.OutputTo acOutputReport, reportName, acFormatPDF, filename
    
        MsgBox "Report saved to " & filename
    
        'clean up
        Set fd = Nothing
    End Sub

  11. #11
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I guess it worked for me because I'm using the file picker (value 3). Curiously, according to this:

    https://docs.microsoft.com/en-us/off...ion.FileDialog

    the save as option isn't supported in Access:

    The msoFileDialogOpen and msoFileDialogSaveAs constants are not supported in Microsoft Access.

    Obviously your test says otherwise.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  12. #12
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Or I wonder if that means you can't use msoFileDialogSaveAs, but you can use 2.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  13. #13
    ssanfu is offline Master of Nothing
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    From what I have read at different times, sometimes the MsoFileDialogType enumeration name is not available, but the VALUE (2) will work.

    Also in the link I posted:

    - The open file dialog will in fact not open any files! It will just allow the user to select files to open. You need to open the files for reading / writing yourself.
    - The save file dialog will in fact not save any files! It will just allow the user to select a filename for the file. You need to open the files for reading / writing yourself.
    - The msoFileDialogSaveAs dialog does NOT support file filters


    I don't use the FileDialog functions enough to remember what I can and can't do. I end up reading and searching for a while until I get something to work.

  14. #14
    templeowls is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2019
    Posts
    305
    Ssanfu...I tried your code but no luck. It still is not considering it a PDF for whatever reason

  15. #15
    ssanfu is offline Master of Nothing
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Do you get any errors?
    Have you stepped through the code to see the values of the variables?
    I created 5 test pdf's no problem.

    Maybe post your dB for analysis.....

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

Similar Threads

  1. Issue with simple Export to excel
    By greatwhite in forum Programming
    Replies: 5
    Last Post: 08-22-2019, 06:23 AM
  2. Replies: 4
    Last Post: 02-12-2014, 12:49 PM
  3. Export Report to PDF with Button
    By agure in forum Import/Export Data
    Replies: 24
    Last Post: 02-09-2014, 04:59 PM
  4. Replies: 2
    Last Post: 01-29-2013, 07:01 PM
  5. Export query issue
    By Shaba in forum Queries
    Replies: 2
    Last Post: 10-20-2011, 09:20 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