Results 1 to 10 of 10
  1. #1
    charly.csh is offline Competent Performer
    Windows 8 Access 2007
    Join Date
    Nov 2014
    Posts
    186

    Print Report as pdf file

    Hi everyone,

    I write today because I want to print a report from my MS Access as PDF File; sometime ago I had that with a button doing that automatically (wih code)

    I have not longer the application "PDF Creator". In fact I want to make that with the pdf converter installed by default.

    Your help will be highly appreciated

    This is my code:

    *********************************

    Private Sub ImprimirPDFcmd_Click()


    On Error GoTo Err_ImprimirPapelcmd_Click

    DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70

    Dim stDocName As String
    Dim stLinkCriteria As String

    stDocName = "Diagram_Cause_Efect"
    stLinkCriteria = "[ID]=" & "'" & Me.ID & "'"

    Set Application.Printer = Application.Printers("PDF Creator")

    'Application.Printer.Orientation = Orientacion
    'Application.Printer.PaperSize = Tamaņo

    DoCmd.OpenReport stDocName, acViewNormal, , stLinkCriteria
    Set Application.Printer = Nothing

    MsgBox "Report to PDF", vbInformation, "Exporting to PDF"

    Exit_ImprimirPapelcmd_Click:
    Exit Sub

    Err_ImprimirPapelcmd_Click:
    MsgBox Err.Description
    Resume Exit_ImprimirPapelcmd_Click



    End Sub

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,523
    vRpt = "rMyRpt
    vFile = "c:\temp" & vRpt & ".pdf"
    DoCmd.OutputTo acOutputReport, vRpt, acFormatPDF, vFILE

  3. #3
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    Hi there, can you try:
    Code:
    Private Sub ImprimirPDFcmd_Click()
    
    On Error GoTo Err_ImprimirPapelcmd_Click
    
    DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
    
    Dim stDocName As String
    Dim stLinkCriteria As String
    Dim stFileName as String
    
    stDocName = "Diagram_Cause_Efect_ByID"  'modify the report to include the criteria below in its record source and save it with a different name as suggested
    'stLinkCriteria = "[ID]=" & "'" & Me.ID & "'"
    
    'Set Application.Printer = Application.Printers("PDF Creator")
    
    'Application.Printer.Orientation = Orientacion
    'Application.Printer.PaperSize = Tamaņo
    stFileName=CurrentProject.path & "\" & stDocName & Format(Date,"YYYYMMDD") & ".pdf" 'set the file name for the new PDF
    'DoCmd.OpenReport stDocName, acViewNormal, , stLinkCriteria
    DoCmd.OutputTo acOutputReport, stDocName,  acFormatPDF, stFileName, True
    'Set Application.Printer = Nothing
    
    MsgBox "Report to PDF", vbInformation, "Exporting to PDF"
    
    Exit_ImprimirPapelcmd_Click:
    Exit Sub
    
    Err_ImprimirPapelcmd_Click:
    MsgBox Err.Description
    
    Resume Exit_ImprimirPapelcmd_Click
    End Sub
    
    
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  4. #4
    charly.csh is offline Competent Performer
    Windows 8 Access 2007
    Join Date
    Nov 2014
    Posts
    186
    Hi Ranman256,

    vRpt = "rMyRpt
    vFile = "c:\temp" & vRpt & ".pdf"

    should be declarated as Strings?

  5. #5
    charly.csh is offline Competent Performer
    Windows 8 Access 2007
    Join Date
    Nov 2014
    Posts
    186
    Vlad Cucinschi,

    It looks good however when I run it, it's shown a message as "Microsoft Access doesn't find the object "|1".

    Do you know why?

  6. #6
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    Did you read my comments in red and follow what I suggested?

  7. #7
    charly.csh is offline Competent Performer
    Windows 8 Access 2007
    Join Date
    Nov 2014
    Posts
    186
    Hi Gicu

    I had a mistake with the name of my report... I 've already modified it,
    One question I still have is how do I include the criteria as you suggested? I meant, this comment suggested by you in red

    'modify the report to include the criteria below in its record source and save it with a different name as suggested

    My criteria should be this "[ID]=" & "'" & Me.ID & "'"

    I do not know how to add my criteria to: DoCmd.OutputTo acOutputReport, stDocName, acFormatPDF, stFileName, True

    In fact when I took this out, it prints in pdf for all the records, (because I have not defined the criteria)

    ****************************

    Dim stDocName As String
    Dim stLinkCriteria As String
    Dim stFileName As String


    stDocName = "Diagrama_CausaEfecto_Report" 'modify the report to include the criteria below in its record source and save it with a different name as suggested
    stLinkCriteria = "[ID]=" & "'" & Me.ID & "'"
    stFileName = CurrentProject.Path & "" & stDocName & Format(Date, "YYYYMMDD") & "Diagrama Causa Efecto.pdf" 'set the file name for the new PDF


    DoCmd.OpenReport stDocName, acViewNormal, , stLinkCriteria
    DoCmd.OutputTo acOutputReport, stDocName, acFormatPDF, stFileName, True

  8. #8
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    Hi there,
    You need to open the original report in design view, open the Properties box and on the Data tab you will see the Record Source property. If could be a saved query or a SQL statement similar to SELECT * FROM tblYourTABLE;". Click the builder (the three dots) on the right of the property field and it will open the record source in design view (query design view). In here find the ID field and in the Criteria row add Forms!frmYourFormWhereYouUseThisCode![ID] and save it. Now save this updated report with a new name like Diagrama_CausaEfecto_Report_SelectedID.

    Now the code becomes:

    Code:
    
    
    Code:
    Dim stDocName As String
    Dim stLinkCriteria As String
    Dim stFileName As String
    
    
    
    
    stDocName = "Diagrama_CausaEfecto_Report_SelectedID" 'modify the report to include the criteria below in its record source and save it with a different name as suggested
    stLinkCriteria = "[ID]=" & "'" & Me.ID & "'"
    stFileName = CurrentProject.Path & "\" & Format(Date, "YYYYMMDD") & "_Diagrama Causa Efecto.pdf" 'the file name will be 20200401_Diagrama Causa Efecto.pdf and it be in the same folder as the front-end -you might want to include the ID in the file name
    
    
    
    
    'DoCmd.OpenReport stDocName, acViewNormal, , stLinkCriteria  'you do not want to print the report but save it as PDF on the next line
    DoCmd.OutputTo acOutputReport, stDocName, acFormatPDF, stFileName, True


    Cheers,
    Vlad
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  9. #9
    charly.csh is offline Competent Performer
    Windows 8 Access 2007
    Join Date
    Nov 2014
    Posts
    186
    WOW!!

    I didn't know that
    Today I learnt something new!!!

    Thank you very much!!!!!! great solution!!

  10. #10
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    You're very welcome!

    Stay safe!
    Vlad
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

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

Similar Threads

  1. Replies: 3
    Last Post: 04-28-2017, 09:51 AM
  2. Replies: 5
    Last Post: 08-06-2015, 03:26 PM
  3. Replies: 1
    Last Post: 02-21-2015, 11:35 PM
  4. Replies: 10
    Last Post: 09-17-2014, 08:23 AM
  5. Replies: 6
    Last Post: 03-01-2014, 07:07 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