Results 1 to 9 of 9
  1. #1
    g4tv4life is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jan 2014
    Posts
    50

    Creating PDF for each page of a report?

    Hey everyone, I have a access database that generates PDF form letters (or at least should if I wasn't crap at coding)
    The code is supposed to take the query, and loop through creating a pdf of each entry and saves them by the officer name and date of the incident.here's the code I have so far:


    Code:
    Private Sub Command75_Click()
    Dim MyDB As DAO.Database
    Dim MyRS As DAO.Recordset
    Dim strSQL As String
    Dim strRptName As String
    Dim count As Integer
     
    strRptName = "Report"
    strSQL = "Select [Main Table].[ID] From [Police Departments Query];"
    
    Set MyDB = CurrentDb
    Set MyRS = MyDB.OpenRecordset(strSQL, dbOpenForwardOnly)
    
    With MyRS
    
    Do While Not MyRS.EOF
       DoCmd.OpenReport strRptName, acViewPreview, , "[ID]=" & ![ID]
       DoCmd.OutputTo acOutputReport, strRptName, acFormatPDF, [Application].[CurrentProject].[path] & "\Aed letters\" & "Aed Letter " & [Officer Name] & " " & Format([Date of Incident], "MMMM dd, yyyy") & ".pdf"
       DoCmd.Close acReport, strRptName, acSaveNo
       .MoveNext
    Loop
    
    End With
    MyRS.Close
    Set MyRS = Nothing
    End Sub



    Now it loops through, however it doesn't seem to change the query so it just creates the same pdf
    Any suggestions on how to make this work or links to working code? thanks!

  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 don't see anything in the path that would change for each record in the loop. If it was supposed to be [Officer Name], it would have to be ![Officer Name].
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    NOTE: Use / instead of \ in CODE tag.

    Where are [Officer Name] and [Date of Incident] coming from? They won't come from the report RecordSource.

    Are you sure it's the same data output or is it just the same file name being overwritten? Are multiple reports output?
    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.

  4. #4
    g4tv4life is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jan 2014
    Posts
    50
    the officer name and date of incident fields are being pulled from the query, its overwriting the same report with the 1st person, and the report isn't changing each time. It is only 1 report. I'm extremely bad at coding so you have to bear with me here, The only problem I see is that when it loops through its not moving to the next record.
    Quote Originally Posted by June7 View Post
    NOTE: Use / instead of \ in CODE tag.

    Where are [Officer Name] and [Date of Incident] coming from? They won't come from the report RecordSource.

    Are you sure it's the same data output or is it just the same file name being overwritten? Are multiple reports output?

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    The query has only one field - ID, so how can it provide the name and date values?

    Is this code behind a bound form? Are [Officer Name] and [Date of Incident] fields on the form? The code is just pulling the values from the record that has focus, not from the VBA recordset (which doesn't have them anyway).

    I think the code is looping the recordset but not changing the officer name and date of incident values because of incorrect referencing.
    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.

  6. #6
    g4tv4life is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jan 2014
    Posts
    50
    Heres how it works, I have a bound form that inputs each officer as a record, I have a report where each page is a form letter and the fields are referenced in the letter. I have a query that connects 2 tables, the one with the officer data, the other with the police department address data. The query has all the fields but I'm not sure how to reference them properly in my code as I'm mostly self taught. I believe my referencing is to blame, any suggestions how to properly reference the 2 fields? (Officer Name and Date of Incident are both fields, in the query)

    Quote Originally Posted by June7 View Post
    The query has only one field - ID, so how can it provide the name and date values?

    Is this code behind a bound form? Are [Officer Name] and [Date of Incident] fields on the form? The code is just pulling the values from the record that has focus, not from the VBA recordset (which doesn't have them anyway).

    I think the code is looping the recordset but not changing the officer name and date of incident values because of incorrect referencing.

  7. #7
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Code:
    if MyRS.eof = true then
    msgbox "No Records found. Exiting Procedure!"
    MyRS.close
    set MyRS = nothing
    exit sub
    end if
    
    With MyRS
    
    .movefirst    'It is important to move to the first record 
    'so you go through all records while using .MoveNext
    
    Do While Not .EOF   'You are using With so no need to type MyRS here
    
    
       DoCmd.OpenReport strRptName, acViewPreview, , "[ID]=" & ![ID]
       DoCmd.OutputTo acOutputReport, strRptName, acFormatPDF, [Application].[CurrentProject].[path] & "\Aed letters\" & "Aed Letter " & [Officer Name] & " " & Format([Date of Incident], "MMMM dd, yyyy") & ".pdf"
       DoCmd.Close acReport, strRptName, acSaveNo
       .MoveNext
    Loop
    
    End With

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    The fields might be in [Police Departments Query] but they aren't in the VBA recordset. Can use a wildcard to select all fields.

    SELECT * FROM [Police Departments Query];

    Specify fields with comma separation.

    SELECT ID, [Officer Name], [Date of Incident] FROM [Police Departments Query];

    Then reference the recordset fields with the ! (bang)

    DoCmd.OutputTo acOutputReport, strRptName, acFormatPDF, [Application].[CurrentProject].[path] & "\Aed letters\" & "Aed Letter " & ![Officer Name] & " " & Format(![Date of Incident], "MMMM dd, yyyy") & ".pdf"
    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
    g4tv4life is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jan 2014
    Posts
    50
    June, you are a SAINT!
    but you've heard that enough times to be canonized for real.

    I didn't realize I hadn't create a record-set properly, Now I know =D

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

Similar Threads

  1. Replies: 1
    Last Post: 03-05-2013, 06:38 PM
  2. Creating Summary Page in Access Report
    By jakeman in forum Reports
    Replies: 5
    Last Post: 10-11-2011, 06:25 PM
  3. Creating an HTML report as a single page
    By Harle in forum Access
    Replies: 2
    Last Post: 01-22-2011, 11:18 PM
  4. Page break on report inserts empty page between
    By Galadrielle in forum Reports
    Replies: 0
    Last Post: 07-07-2010, 04:18 AM
  5. Replies: 1
    Last Post: 05-22-2010, 08:30 PM

Tags for this Thread

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