MS Access save record as PDF using ID











up vote
0
down vote

favorite












My organization uses a MS Access database to store incident reports. I have a report called "Case Details" and I want to have the option to save the report that's currently open as a PDF. I've got it working for the most part, the only issue is that when saving the PDF, it's saving all of the records, not just the record that was being viewed.



How do I fix it so that it only saves the record being viewed, using the record's ID?



Here's the code for the save button.



Private Sub Command1626_Click()
DoCmd.OutputTo acOutputReport, "Case Details", acFormatPDF, "G:PoliceRestrictedSaved Reports " & Me.txtPDFRef & ".pdf", True
End Sub


The "txtPDFRef" box on the report contains the following code, which is used to name the report. (Case Number - Officer - Date Saved)



="CR" & Format(Date(),"yy") & "-000" & [ID] & " - " & [ReportingOfficer] & " - " & Format(Date(),"yyyymmdd")









share|improve this question


























    up vote
    0
    down vote

    favorite












    My organization uses a MS Access database to store incident reports. I have a report called "Case Details" and I want to have the option to save the report that's currently open as a PDF. I've got it working for the most part, the only issue is that when saving the PDF, it's saving all of the records, not just the record that was being viewed.



    How do I fix it so that it only saves the record being viewed, using the record's ID?



    Here's the code for the save button.



    Private Sub Command1626_Click()
    DoCmd.OutputTo acOutputReport, "Case Details", acFormatPDF, "G:PoliceRestrictedSaved Reports " & Me.txtPDFRef & ".pdf", True
    End Sub


    The "txtPDFRef" box on the report contains the following code, which is used to name the report. (Case Number - Officer - Date Saved)



    ="CR" & Format(Date(),"yy") & "-000" & [ID] & " - " & [ReportingOfficer] & " - " & Format(Date(),"yyyymmdd")









    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      My organization uses a MS Access database to store incident reports. I have a report called "Case Details" and I want to have the option to save the report that's currently open as a PDF. I've got it working for the most part, the only issue is that when saving the PDF, it's saving all of the records, not just the record that was being viewed.



      How do I fix it so that it only saves the record being viewed, using the record's ID?



      Here's the code for the save button.



      Private Sub Command1626_Click()
      DoCmd.OutputTo acOutputReport, "Case Details", acFormatPDF, "G:PoliceRestrictedSaved Reports " & Me.txtPDFRef & ".pdf", True
      End Sub


      The "txtPDFRef" box on the report contains the following code, which is used to name the report. (Case Number - Officer - Date Saved)



      ="CR" & Format(Date(),"yy") & "-000" & [ID] & " - " & [ReportingOfficer] & " - " & Format(Date(),"yyyymmdd")









      share|improve this question













      My organization uses a MS Access database to store incident reports. I have a report called "Case Details" and I want to have the option to save the report that's currently open as a PDF. I've got it working for the most part, the only issue is that when saving the PDF, it's saving all of the records, not just the record that was being viewed.



      How do I fix it so that it only saves the record being viewed, using the record's ID?



      Here's the code for the save button.



      Private Sub Command1626_Click()
      DoCmd.OutputTo acOutputReport, "Case Details", acFormatPDF, "G:PoliceRestrictedSaved Reports " & Me.txtPDFRef & ".pdf", True
      End Sub


      The "txtPDFRef" box on the report contains the following code, which is used to name the report. (Case Number - Officer - Date Saved)



      ="CR" & Format(Date(),"yy") & "-000" & [ID] & " - " & [ReportingOfficer] & " - " & Format(Date(),"yyyymmdd")






      vba ms-access






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 9 at 1:13









      MikeM

      1




      1
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote













          It seems the only way is to filter the report to the record to be printed.
          So it's essential to determine the ID of the record to be printed.



          It's not clear for me, if your button is located on the report itself or maybe on a form.



          Button on the report itself



          I tried to get it running when the button is on the report itself.
          To determine the current ID it seems to be necessary that the calling button is located in the detail section of the report.



          Then you can filter the report, export it to pdf and restore the maybe before applied filter.



          The only backdraw is, that I found no way to view the last viewed record in the report after the code executed.
          Instead you are on the first record.



          This would be the code for the button:



          Dim currentFilter As String
          currentFilter = Me.Filter

          Dim currentFilterOn As Boolean
          currentFilterOn = Me.FilterOn

          'If field type of ID is a number use this:
          Me.Filter = "[ID] = " & Me.ID
          'If field type of ID is a string use this:
          'Me.Filter = "[ID] = '" & Me.ID & "'"

          Me.FilterOn = True

          DoCmd.OutputTo acOutputReport, Me.Name, acFormatPDF, "G:PoliceRestrictedSaved Reports " & Me.txtPDFRef & ".pdf", True

          Me.Filter = currentFilter
          Me.FilterOn = currentFilterOn


          Button on a form



          If the button is for example on a form and you just want to print the regarding report, then you could use this code in this button:



          Const REPORT_NAME As String = "Case Details"

          Dim condition As String
          'If field type of ID is a number use this:
          condition = "[ID] = " & Me.ID
          'If field type of ID is a string use this:
          'condition = "[ID] = '" Me.ID & "'"

          DoCmd.OpenReport REPORT_NAME, acViewPreview, , condition, acHidden

          'You would have to provide txtPDFRef in another way, because the report isn't open yet.
          DoCmd.OutputTo acOutputReport, REPORT_NAME, acFormatPDF, "G:PoliceRestrictedSaved Reports " & Me.txtPDFRef & ".pdf", True

          DoCmd.Close acReport, REPORT_NAME


          It closes the report after saving to pdf. Be aware that you maybe have to provide txtPDFRef in a different way. But maybe it exists on the form too with the same name, that would make it run immediately.






          share|improve this answer




























            up vote
            0
            down vote













            Simplest solutions:



            Option 1

            Open the report in preview with a filter, then use outputTo without the ObjectName argument.



            docmd.openReport "Case Details",,"id = " & id
            docmd.outputTo objectType:=acOutputReport, outputFormat:=acFormatPDF, outputFile:= somefileName
            docmd.close acOutputReport, "Case Details"


            Option 2 (even simpler)

            In design view, change the reports'filter property to refer to a control in the calling form.

            Something like id = forms!someForm.idControl






            share|improve this answer





















              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














               

              draft saved


              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53218429%2fms-access-save-record-as-pdf-using-id%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              0
              down vote













              It seems the only way is to filter the report to the record to be printed.
              So it's essential to determine the ID of the record to be printed.



              It's not clear for me, if your button is located on the report itself or maybe on a form.



              Button on the report itself



              I tried to get it running when the button is on the report itself.
              To determine the current ID it seems to be necessary that the calling button is located in the detail section of the report.



              Then you can filter the report, export it to pdf and restore the maybe before applied filter.



              The only backdraw is, that I found no way to view the last viewed record in the report after the code executed.
              Instead you are on the first record.



              This would be the code for the button:



              Dim currentFilter As String
              currentFilter = Me.Filter

              Dim currentFilterOn As Boolean
              currentFilterOn = Me.FilterOn

              'If field type of ID is a number use this:
              Me.Filter = "[ID] = " & Me.ID
              'If field type of ID is a string use this:
              'Me.Filter = "[ID] = '" & Me.ID & "'"

              Me.FilterOn = True

              DoCmd.OutputTo acOutputReport, Me.Name, acFormatPDF, "G:PoliceRestrictedSaved Reports " & Me.txtPDFRef & ".pdf", True

              Me.Filter = currentFilter
              Me.FilterOn = currentFilterOn


              Button on a form



              If the button is for example on a form and you just want to print the regarding report, then you could use this code in this button:



              Const REPORT_NAME As String = "Case Details"

              Dim condition As String
              'If field type of ID is a number use this:
              condition = "[ID] = " & Me.ID
              'If field type of ID is a string use this:
              'condition = "[ID] = '" Me.ID & "'"

              DoCmd.OpenReport REPORT_NAME, acViewPreview, , condition, acHidden

              'You would have to provide txtPDFRef in another way, because the report isn't open yet.
              DoCmd.OutputTo acOutputReport, REPORT_NAME, acFormatPDF, "G:PoliceRestrictedSaved Reports " & Me.txtPDFRef & ".pdf", True

              DoCmd.Close acReport, REPORT_NAME


              It closes the report after saving to pdf. Be aware that you maybe have to provide txtPDFRef in a different way. But maybe it exists on the form too with the same name, that would make it run immediately.






              share|improve this answer

























                up vote
                0
                down vote













                It seems the only way is to filter the report to the record to be printed.
                So it's essential to determine the ID of the record to be printed.



                It's not clear for me, if your button is located on the report itself or maybe on a form.



                Button on the report itself



                I tried to get it running when the button is on the report itself.
                To determine the current ID it seems to be necessary that the calling button is located in the detail section of the report.



                Then you can filter the report, export it to pdf and restore the maybe before applied filter.



                The only backdraw is, that I found no way to view the last viewed record in the report after the code executed.
                Instead you are on the first record.



                This would be the code for the button:



                Dim currentFilter As String
                currentFilter = Me.Filter

                Dim currentFilterOn As Boolean
                currentFilterOn = Me.FilterOn

                'If field type of ID is a number use this:
                Me.Filter = "[ID] = " & Me.ID
                'If field type of ID is a string use this:
                'Me.Filter = "[ID] = '" & Me.ID & "'"

                Me.FilterOn = True

                DoCmd.OutputTo acOutputReport, Me.Name, acFormatPDF, "G:PoliceRestrictedSaved Reports " & Me.txtPDFRef & ".pdf", True

                Me.Filter = currentFilter
                Me.FilterOn = currentFilterOn


                Button on a form



                If the button is for example on a form and you just want to print the regarding report, then you could use this code in this button:



                Const REPORT_NAME As String = "Case Details"

                Dim condition As String
                'If field type of ID is a number use this:
                condition = "[ID] = " & Me.ID
                'If field type of ID is a string use this:
                'condition = "[ID] = '" Me.ID & "'"

                DoCmd.OpenReport REPORT_NAME, acViewPreview, , condition, acHidden

                'You would have to provide txtPDFRef in another way, because the report isn't open yet.
                DoCmd.OutputTo acOutputReport, REPORT_NAME, acFormatPDF, "G:PoliceRestrictedSaved Reports " & Me.txtPDFRef & ".pdf", True

                DoCmd.Close acReport, REPORT_NAME


                It closes the report after saving to pdf. Be aware that you maybe have to provide txtPDFRef in a different way. But maybe it exists on the form too with the same name, that would make it run immediately.






                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  It seems the only way is to filter the report to the record to be printed.
                  So it's essential to determine the ID of the record to be printed.



                  It's not clear for me, if your button is located on the report itself or maybe on a form.



                  Button on the report itself



                  I tried to get it running when the button is on the report itself.
                  To determine the current ID it seems to be necessary that the calling button is located in the detail section of the report.



                  Then you can filter the report, export it to pdf and restore the maybe before applied filter.



                  The only backdraw is, that I found no way to view the last viewed record in the report after the code executed.
                  Instead you are on the first record.



                  This would be the code for the button:



                  Dim currentFilter As String
                  currentFilter = Me.Filter

                  Dim currentFilterOn As Boolean
                  currentFilterOn = Me.FilterOn

                  'If field type of ID is a number use this:
                  Me.Filter = "[ID] = " & Me.ID
                  'If field type of ID is a string use this:
                  'Me.Filter = "[ID] = '" & Me.ID & "'"

                  Me.FilterOn = True

                  DoCmd.OutputTo acOutputReport, Me.Name, acFormatPDF, "G:PoliceRestrictedSaved Reports " & Me.txtPDFRef & ".pdf", True

                  Me.Filter = currentFilter
                  Me.FilterOn = currentFilterOn


                  Button on a form



                  If the button is for example on a form and you just want to print the regarding report, then you could use this code in this button:



                  Const REPORT_NAME As String = "Case Details"

                  Dim condition As String
                  'If field type of ID is a number use this:
                  condition = "[ID] = " & Me.ID
                  'If field type of ID is a string use this:
                  'condition = "[ID] = '" Me.ID & "'"

                  DoCmd.OpenReport REPORT_NAME, acViewPreview, , condition, acHidden

                  'You would have to provide txtPDFRef in another way, because the report isn't open yet.
                  DoCmd.OutputTo acOutputReport, REPORT_NAME, acFormatPDF, "G:PoliceRestrictedSaved Reports " & Me.txtPDFRef & ".pdf", True

                  DoCmd.Close acReport, REPORT_NAME


                  It closes the report after saving to pdf. Be aware that you maybe have to provide txtPDFRef in a different way. But maybe it exists on the form too with the same name, that would make it run immediately.






                  share|improve this answer












                  It seems the only way is to filter the report to the record to be printed.
                  So it's essential to determine the ID of the record to be printed.



                  It's not clear for me, if your button is located on the report itself or maybe on a form.



                  Button on the report itself



                  I tried to get it running when the button is on the report itself.
                  To determine the current ID it seems to be necessary that the calling button is located in the detail section of the report.



                  Then you can filter the report, export it to pdf and restore the maybe before applied filter.



                  The only backdraw is, that I found no way to view the last viewed record in the report after the code executed.
                  Instead you are on the first record.



                  This would be the code for the button:



                  Dim currentFilter As String
                  currentFilter = Me.Filter

                  Dim currentFilterOn As Boolean
                  currentFilterOn = Me.FilterOn

                  'If field type of ID is a number use this:
                  Me.Filter = "[ID] = " & Me.ID
                  'If field type of ID is a string use this:
                  'Me.Filter = "[ID] = '" & Me.ID & "'"

                  Me.FilterOn = True

                  DoCmd.OutputTo acOutputReport, Me.Name, acFormatPDF, "G:PoliceRestrictedSaved Reports " & Me.txtPDFRef & ".pdf", True

                  Me.Filter = currentFilter
                  Me.FilterOn = currentFilterOn


                  Button on a form



                  If the button is for example on a form and you just want to print the regarding report, then you could use this code in this button:



                  Const REPORT_NAME As String = "Case Details"

                  Dim condition As String
                  'If field type of ID is a number use this:
                  condition = "[ID] = " & Me.ID
                  'If field type of ID is a string use this:
                  'condition = "[ID] = '" Me.ID & "'"

                  DoCmd.OpenReport REPORT_NAME, acViewPreview, , condition, acHidden

                  'You would have to provide txtPDFRef in another way, because the report isn't open yet.
                  DoCmd.OutputTo acOutputReport, REPORT_NAME, acFormatPDF, "G:PoliceRestrictedSaved Reports " & Me.txtPDFRef & ".pdf", True

                  DoCmd.Close acReport, REPORT_NAME


                  It closes the report after saving to pdf. Be aware that you maybe have to provide txtPDFRef in a different way. But maybe it exists on the form too with the same name, that would make it run immediately.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 9 at 8:46









                  Unhandled Exception

                  62446




                  62446
























                      up vote
                      0
                      down vote













                      Simplest solutions:



                      Option 1

                      Open the report in preview with a filter, then use outputTo without the ObjectName argument.



                      docmd.openReport "Case Details",,"id = " & id
                      docmd.outputTo objectType:=acOutputReport, outputFormat:=acFormatPDF, outputFile:= somefileName
                      docmd.close acOutputReport, "Case Details"


                      Option 2 (even simpler)

                      In design view, change the reports'filter property to refer to a control in the calling form.

                      Something like id = forms!someForm.idControl






                      share|improve this answer

























                        up vote
                        0
                        down vote













                        Simplest solutions:



                        Option 1

                        Open the report in preview with a filter, then use outputTo without the ObjectName argument.



                        docmd.openReport "Case Details",,"id = " & id
                        docmd.outputTo objectType:=acOutputReport, outputFormat:=acFormatPDF, outputFile:= somefileName
                        docmd.close acOutputReport, "Case Details"


                        Option 2 (even simpler)

                        In design view, change the reports'filter property to refer to a control in the calling form.

                        Something like id = forms!someForm.idControl






                        share|improve this answer























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          Simplest solutions:



                          Option 1

                          Open the report in preview with a filter, then use outputTo without the ObjectName argument.



                          docmd.openReport "Case Details",,"id = " & id
                          docmd.outputTo objectType:=acOutputReport, outputFormat:=acFormatPDF, outputFile:= somefileName
                          docmd.close acOutputReport, "Case Details"


                          Option 2 (even simpler)

                          In design view, change the reports'filter property to refer to a control in the calling form.

                          Something like id = forms!someForm.idControl






                          share|improve this answer












                          Simplest solutions:



                          Option 1

                          Open the report in preview with a filter, then use outputTo without the ObjectName argument.



                          docmd.openReport "Case Details",,"id = " & id
                          docmd.outputTo objectType:=acOutputReport, outputFormat:=acFormatPDF, outputFile:= somefileName
                          docmd.close acOutputReport, "Case Details"


                          Option 2 (even simpler)

                          In design view, change the reports'filter property to refer to a control in the calling form.

                          Something like id = forms!someForm.idControl







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 9 at 8:57









                          Patrick Honorez

                          18.6k563116




                          18.6k563116






























                               

                              draft saved


                              draft discarded



















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53218429%2fms-access-save-record-as-pdf-using-id%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Guess what letter conforming each word

                              Port of Spain

                              Run scheduled task as local user group (not BUILTIN)