Results 1 to 5 of 5
  1. #1
    rwahdan1978 is offline Advanced Beginner
    Windows 11 Access 2016
    Join Date
    Jun 2024
    Posts
    57

    Save each record in report as pdf

    Hi

    I have a button in report footer that will save each record as pdf but nothing happens and i get error:

    Code:
    Private Sub Command1_Click()
    
    
        Dim rpt As Report
        Dim i As Integer
        Set rpt = Reports("Certificates") 'Replace "Report1" with the name of your report
        For i = 0 To rpt.Pages - 1
            rpt.PrintOut acSelection, , , , i  'print the current page
            Dim fileName As String
            fileName = rpt.Controls("canName").Value   'Replace "NameField" with the name of the field in the page header that you want to use as the file name
            DoCmd.OutputTo acOutputReport, "", acFormatPDF, fileName & ".pdf"
        Next i
    
    
    End Sub
    the error:
    Code:
    application defined or object defined error


  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,555
    Why not just issue a report for each record? before they are in the report?
    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
    rwahdan1978 is offline Advanced Beginner
    Windows 11 Access 2016
    Join Date
    Jun 2024
    Posts
    57
    Quote Originally Posted by Welshgasman View Post
    Why not just issue a report for each record? before they are in the report?
    Could you please explain or give an example? Do you mean from a form to print a record?

  4. #4
    Micron is online now Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    To do that you should have a form that contains the records that the report also uses. Or if you have a query that the report is based on that is fine too. If the report is based on a table, it will require more code. Which of these are you using to populate the report.

    BTW, it's great that you showed the error message instead of just then number like so many people do, but you really should define which line raises the error. I'm guessing that it's the output line, because you're passing a zero length string for the object name to be output. Don't think you can do that.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  5. #5
    madpiet is offline Expert
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    565

    Post

    [QUOTE=rwahdan1978;526935]Hi

    I have a button in report footer that will save each record as pdf but nothing happens and i get error:

    Code:
    Private Sub Command1_Click()
    
    
        Dim rpt As Report
        Dim i As Integer
        Set rpt = Reports("Certificates") 'Replace "Report1" with the name of your report
        For i = 0 To rpt.Pages - 1
            rpt.PrintOut acSelection, , , , i  'print the current page
            Dim fileName As String
            fileName = rpt.Controls("canName").Value   'Replace "NameField" with the name of the field in the page header that you want to use as the file name
            DoCmd.OutputTo acOutputReport, "", acFormatPDF, fileName & ".pdf"
        Next i
    
    
    End Sub
    The way I would do it is to use a recordset to loop over all the records you want to print out. (You only need the primary key of the records, because you're just using it as a filter). Then you do something like

    NOTE: This is aircode... I'm a terrible person and too lazy to test all the syntax, but this is the basic idea.

    dim rsToPrint as Recordset
    dim qdf as querydef '-- use this to get just the records you want (and only the ID's. You don't need the rest here)

    set qdf = Currentdb.Querydefs("QueryThatSelectsWhatToPrint")
    set rsToPrint = qdf.OpenRecordset()

    '-- Loop over recordset of records you want to print, pass the values one at a time to the report, print the report, close, go to next record
    while Not rsToPrint.EOF
    docmd.OpenReport "MyReport","[RecordID]=rsToPrint!RecordID
    DoCmd.OutputTo acOutputReport, "", acFormatPDF, fileName & ".pdf"
    docmd.Close acReport, "MyReport"
    rsToPrint.MoveNext
    Loop

    rsToPrint.Close
    Set rsToPrint = nothing

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

Similar Threads

  1. Replies: 4
    Last Post: 11-27-2014, 11:32 AM
  2. Save the record before runing the report
    By mavisyew in forum Access
    Replies: 6
    Last Post: 08-27-2014, 06:49 PM
  3. Replies: 28
    Last Post: 05-25-2014, 04:01 PM
  4. Replies: 3
    Last Post: 10-08-2012, 08:40 AM
  5. Replies: 8
    Last Post: 09-27-2012, 11:12 AM

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