ComboBox Selection to PDF

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Curious27
    New Member
    • Aug 2022
    • 37

    #1

    ComboBox Selection to PDF

    I am trying to save a report to a PDF file based on a Combobox selection of reports, but can't figure out
    how to place a combobox selection into a docmd.output line of code?

    Ex:
    Replace "Summary" with a Combobox selection, this way I'm not hardcoding report names.

    Code:
    DoCmd.OutputTo acOutputReport, "Summary", acFormatPDF, "C:\Reports" & ".pdf"
    And can it also be used as a selection in a case statement? Case 1, 2 and 4 are working in my project.
    Case 1
    Print Report
    Case 2
    View Report
    Case 3
    Code:
     DoCmd.OutputTo acOutputReport, "Summary", acFormatPDF, "C:\Reports" & ".pdf"
    Case 4
    Email Report

    Any ideas?

    Thank you
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32669

    #2
    Hi Curious.

    Simply use a reference to the ComboBox control on your form.

    EG.
    Code:
    DoCmd.OutputTo acOutputReport, {ComboBoxRef}.Value, acFormatPDF, "C:\Reports.pdf"

    Comment

    • Curious27
      New Member
      • Aug 2022
      • 37

      #3
      Hi NeoPa

      Doing good I hope.

      I had already tried that before I posted but my mistake was I was trying cboPrintReport. Column(1) and that obviously didn't work. I tried your suggestion with .Value and that was a error also. The error is a Run-time error '2501': The OutputTo action was created. When I click Debug it shows my line of code as follows.
      Code:
       DoCmd.OutputTo acOutputReport, "cboPrintReports.value", acFormatPDF, "C:\Reports" & ".pdf"
      Last edited by Curious27; Nov 8 '22, 07:47 PM. Reason: Corrected a mistake.

      Comment

      • isladogs
        Recognized Expert Moderator Contributor
        • Jul 2007
        • 483

        #4
        Windows blocks files being saved to the root c:\ drive and cancels the operation resulting in error 2501
        Try saving to another allowed folder e.g. your MyDocuments folder

        Comment

        • Curious27
          New Member
          • Aug 2022
          • 37

          #5
          Looked to see if the file path may be wrong but it is correct. I did try one thing I removed the file path completely all the way up to acFormatPDF even the comma and the code run through, the File Explorer popped up with the title "Output To" and the report name was what I selected.
          I clicked OK and the report was saved as a PDF file and I opened it in Acrobat.

          Why is the file path a problem and why is the extension ".pdf" needed as acFormatPDF already assigns this extension?

          Going bald from scratching my head to much...

          Comment

          • Curious27
            New Member
            • Aug 2022
            • 37

            #6
            isladogs

            That path doesn't work either, as my original path looks like this: "C:\Users\Admin istrator\Docume nts\Access Databases\Repor ts". That path is were my Access Database is and all the Access Security Trust Center Setting are pointed to this path along with it's Subfolders.
            I even tried sending to D:\Reports with no luck.

            Thanks for looking at it.

            Comment

            • Curious27
              New Member
              • Aug 2022
              • 37

              #7
              I found out what may the one problem.

              The problem is a lazy rookie programmer (ME), I copied this code from a report I created a while back which I placed a Command Button on to convert to a PDF so I could eMail. Well being in a rush and not reviewing what I copied, I just found this Dim statement buried at the top.

              Code:
              Dim fileName As String, fldrPath As String, filePath As String
              Not supposed to be there. PERIOD.
              Last edited by Curious27; Nov 8 '22, 09:27 PM. Reason: Posted to quick

              Comment

              • isladogs
                Recognized Expert Moderator Contributor
                • Jul 2007
                • 483

                #8
                Hi
                Glad you have a solution
                In answer to your earlier questions:
                a) MS blocks applications saving to certain folders for various reasons including
                - folders considered vital to Windows operating correctly e.g. c:\Windows\Syst em32
                - the root folder c:\ - I think this is to ensure that contains as few files as possible to speed up Windows loading times...but I may be wrong
                b) Using acFormatPDF ensures the file is saved as a PDF file. It doesn't provide the file extension
                Specifying the file suffix as .pdf ensures Windows will open it with the default application for PDF files e.g. Acrobat or your web browser.
                You could use any file suffix you like e.g. .XYZ but then Windows will ask you what application to open it with!

                Hope that helps clarify things

                Comment

                • NeoPa
                  Recognized Expert Moderator MVP
                  • Oct 2006
                  • 32669

                  #9
                  Hi Curious.

                  Have a look at the two lines of code. First, the one I posted :
                  Code:
                  DoCmd.OutputTo acOutputReport, {ComboBoxRef}.Value, acFormatPDF, "C:\Reports.pdf"
                  and then your own attempt at applying that :
                  Code:
                  DoCmd.OutputTo acOutputReport, "cboPrintReports.value", acFormatPDF, "C:\Reports" & ".pdf"
                  Did you notice the differences?

                  The second is more obvious but doesn't cause a problem with the code - it just indicates a lack of understanding of what you're doing & why. "C:\Reports " & ".pdf" & "C:\Reports.pdf " both produce exactly the same result. The first is just complicating matters for no reason.

                  The more important issue is that you've converted a reference to a string (, {ComboBoxRef}.V alue,), into an actual string of that reference (, "cboPrintReport s.value",), which completely obliterates its use as a reference. In your case it should probably be , cboPrintReports .Value,. Maybe you've already come up with that solution already.

                  Comment

                  • Curious27
                    New Member
                    • Aug 2022
                    • 37

                    #10
                    NeoPa
                    I did catch that part of the cmbPrintReports .

                    Well, removing that Dim statement did nothing for my problem. I have a Report where I put a command button on to save it as a pdf file and the file path is identical to the one I'm having an issue with. Running that code saves the report as a pdf an places it in the same folder and I can't figure out why. The only difference is the code behind the command button contains a Dim statement as follows, Dim fldrPath As String.

                    My question is do I need a Dim statement for this path and if so how would I reference it in the DoCmd statement?
                    Last edited by Curious27; Nov 8 '22, 11:50 PM. Reason: Correction

                    Comment

                    • Curious27
                      New Member
                      • Aug 2022
                      • 37

                      #11
                      I figured it out.

                      I changed the code a little to include these Dim's.
                      Code:
                      Dim fileName As String, fldrPath As String, filePath As String
                      Then added a different way to get the file path to work.
                      Code:
                          fileName = Me.cboPrintReports.value
                          fldrPath = "C:\Reports"
                          filePath = fldrPath & "\" & fileName & ".pdf"
                      Then Case 3
                      Code:
                      DoCmd.OutputTo acOutputReport, cboPrintReports.value, acFormatPDF, outputFile:=filePath
                      And yes it writes to the C:\ root directory.

                      Just needed someone to talk it out with, thank you guys.

                      Comment

                      Working...