Page 1 of 2 12 LastLast
Results 1 to 15 of 29
  1. #1
    ekryez is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    May 2014
    Posts
    25

    How to Publish and Save each Record Report in Separate File? Access VBA Code Below


    Trying to make this code work, don't know how to filter as it prints identical all reports.
    Private Sub cmdExportPDF_Click()

    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim MyPath As String
    MyPath = "C:\Reports"

    If Len(Dir(MyPath, vbDirectory)) < 1 Then
    MkDir (MyPath)
    End If

    Dim sProjectNumb As String
    Dim sProjectName As String

    Set db = CurrentDb()
    Set rs = db.OpenRecordset("SELECT ProjectNumb, ProjectName FROM Master", dbOpenSnapshot)

    If rs.RecordCount = 0 Then Exit Sub
    Do While Not rs.EOF
    sProjectNumb = rs![ProjectNumb]
    sProjectName = rs![ProjectName]
    MyFileName = sProjectNumb & " " & sProjectName & ".PDF"
    DoCmd.OpenReport "Total Report", acViewDesign, , "[ProjectNumb]=" & temp
    DoCmd.OutputTo acOutputReport, "", acFormatPDF, MyPath & MyFileName
    DoCmd.Close acReport, "Total Report"
    rs.MoveNext
    Loop
    rs.Close
    Set rs = Nothing
    Set db = Nothing
    End Sub

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Where is this code - in button Click event? What is temp - a field on form?
    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.

  3. #3
    ekryez is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    May 2014
    Posts
    25
    Please see attached
    db1.zip

  4. #4
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    So you do not have any idea what temp is?
    "[ProjectNumb]=" & temp

  5. #5
    ekryez is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    May 2014
    Posts
    25
    Quote Originally Posted by ItsMe View Post
    So you do not have any idea what temp is?
    "[ProjectNumb]=" & temp
    That was a mistake.

    Private Sub PrintAll_Click()
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim MyPath As String
    Dim MyFileName As String
    Dim MyProjectNumber As String
    Dim MyProjectName As String
    MyPath = "C:\Reports\"
    Set db = CurrentDb()
    Set rs = db.OpenRecordset("SELECT ProjectNumber,ProjectName FROM Master", dbOpenDynaset)
    With rs
    Do While Not .EOF
    MyProjectNumber = !ProjectNumber
    MyProjectName = !ProjectName
    MyFileName = MyProjectNumber & " " & MyProjectName & ".PDF"
    'DoCmd.OpenReport "Total Report" 'You don't HAVE to open the report
    DoCmd.OutputTo acOutputReport, "Total Report", acFormatPDF, MyPath & MyFileName
    'DoCmd.Close acReport, "Total Report"
    .MoveNext
    Loop
    End With
    Set rs = Nothing
    Set db = Nothing
    End Sub

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    You do have to open the report if you want to apply filter criteria using the WHERE CONDITION of OpenReport method.

    DoCmd.OpenReport "Total Report", acViewPreview, , "ProjectNumber='" & !ProjectNumber & "'"
    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.

  7. #7
    ekryez is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    May 2014
    Posts
    25
    Quote Originally Posted by June7 View Post
    You do have to open the report if you want to apply filter criteria using the WHERE CONDITION of OpenReport method.

    DoCmd.OpenReport "Total Report", acViewPreview, , "ProjectNumber='" & !ProjectNumber & "'"
    Already done that

  8. #8
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Then what is the issue? I tested your procedure after editing that line and enabling the two commented lines and it works.
    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
    ekryez is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    May 2014
    Posts
    25
    I'm trying to print all records separately in one shot, instead of printing one by one, if you go on C:\Reports(my specified path) you see that when print all inside instead of only one record are all of them

  10. #10
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    I don't understand. If you want a separate PDF for each ProjectNumber then you must open the report filtered to ProjectNumber, output report, close report, repeat for next. This is exactly what happens with the Print All button. The Print Project button creates PDF for the current record on form.
    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.

  11. #11
    ekryez is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    May 2014
    Posts
    25
    Quote Originally Posted by June7 View Post
    If you want a separate PDF for each ProjectNumber then you must open the report filtered to ProjectNumber, output report, close report, repeat for next..
    Can you please show me how to do this? Automatically?

  12. #12
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    You already have the code doing that.
    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.

  13. #13
    ekryez is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    May 2014
    Posts
    25
    Did you get the DB?

  14. #14
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    I downloaded your db, fixed the code as described, and it works.
    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.

  15. #15
    ekryez is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    May 2014
    Posts
    25
    where did you uploaded please?

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Save a report file without preview
    By alex__ice in forum Reports
    Replies: 2
    Last Post: 04-16-2013, 08:13 AM
  2. Run Access code, from separate excel...
    By redbull in forum Programming
    Replies: 1
    Last Post: 10-16-2012, 08:50 AM
  3. Replies: 2
    Last Post: 08-05-2012, 06:32 PM
  4. Replies: 7
    Last Post: 11-23-2011, 08:14 PM
  5. How do you file save an 'Attachment' content through code?
    By morespamforya in forum Programming
    Replies: 3
    Last Post: 08-06-2010, 08:58 AM

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