Results 1 to 10 of 10
  1. #1
    templeowls is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2019
    Posts
    305

    Need help exporting criteria driven report via VBA

    Cross post from here. Just trying to get more exposure for this question. I've found a few guides online but none so far that are exporting based on a criteria



    I'm looking to have a button in a form which, when pressed, will pull the current form entry that the user is viewing into a preset report and then export that report to a preset folder. All without prompting the user to do anything I also want the file to have a dynamic name based upon the entry that is being exported. I have the below code which doesn't seem to be applying the criteria. So it's exporting every entry rather than the singular one that the user is viewing. Any ideas? I'm not fluent in VBA so this could be completely incorrectly coded...just as a heads up

    Code:
    Private Sub cmd_exportformPDF_Click()
        Dim reportName As String
        Dim criteria As String
        Dim strfolder As String
        Dim strfilename As String
        
        reportName = "CompletedForm"
        criteria = "[qryComplaints]![ComplaintNumber]=[Forms]![ComplaintDetails - 2021]![ComplaintNumber]"
        strfolder = "F:\Documents"
        strfilename = Me.ComplaintLastName & Me.ComplaintFirstName & "-" & Day(Now) & "-" & Month(Now) & "-" & Year(Now) & ".pdf"
        
        DoCmd.OpenReport reportName, acViewPreview, criteria, acHidden
        DoCmd.OutputTo acOutputReport, reportName, acFormatPDF, strfolder & strfilename
        DoCmd.Close acReport, reportName, acSaveNo
     End Sub

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,524
    the query of the report looks at the combo box (or other control) as criteria, and only
    shows data for that person.

    the report can then be saved to target folder & name:
    Code:
    sub btnPrintPdf_Click()
    vDir = "\\server\folder\"     '(or get it from a table)
    vName = "rInvoice_" & cboBox & ".pdf"
    vFile = vDir & vName
    docmd.OutputTo acOutputReport ,"rInvoice1Person",acformatpdf,vFile
    end sub

  3. #3
    templeowls is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2019
    Posts
    305
    I'm not sure what you mean. The query itself for the report does not utilize any criteria. Should it?

  4. #4
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,524
    yes. the combo would pick the client/person.
    and the query would make a report for only that person.

    select * from table where [clientID]=forms!fMyForm!cboBox

    make another button to output ALL persons if needed (a different report) ,then the combo is not needed.
    But you still get a report of all , or N reports for 1 person.

  5. #5
    templeowls is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2019
    Posts
    305
    I'm filtering by ComplaintNumber since that is always unique. That would work right?

    So basically, [ComplaintNumber]=forms!MyForm!ComplaintNumber
    ?

  6. #6
    templeowls is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2019
    Posts
    305
    So the code would be:

    Code:
    sub btnPrintPdf_Click()
    vDir = "F:\Documents"
    vName = "PatientLastName" & "PatientFirstname" & ".pdf"
    vFile = vDir & vName
    docmd.OutputTo acOutputReport ,"ComplaintReport",acformatpdf,vFile 
    end sub
    I'm honestly unsure of where my components fall into your example. I just want the file name to be "Last name, first name"

  7. #7
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    Can you please try this amended code (choose the line you need for the criteria based on the data type of the complaint number)? You need to isolate the form variable otherwise it is passed as the literal text
    Code:
    Private Sub cmd_exportformPDF_Click()
        Dim reportName As String
        Dim criteria As String
        Dim strfolder As String
        Dim strfilename As String
        
        reportName = "CompletedForm"
        criteria = "[ComplaintNumber]= " & [Forms]![ComplaintDetails - 2021]![ComplaintNumber] 'assumes complaint number is numeric
        'criteria = "[ComplaintNumber]= '" & [Forms]![ComplaintDetails - 2021]![ComplaintNumber] & "'" 'assumes complaint number is text
        strfolder = "F:\Documents"
        strfilename = Me.ComplaintLastName & Me.ComplaintFirstName & "-" & Day(Now) & "-" & Month(Now) & "-" & Year(Now) & ".pdf"
        
        DoCmd.OpenReport reportName, acViewPreview, criteria, acHidden
        DoCmd.OutputTo acOutputReport, reportName, acFormatPDF, strfolder & strfilename
        DoCmd.Close acReport, reportName, acSaveNo
     End Sub
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  8. #8
    templeowls is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2019
    Posts
    305
    That worked!! Thank you so much

  9. #9
    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!
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  10. #10
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,914
    Care to mark it solved in both forums?
    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

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

Similar Threads

  1. Replies: 2
    Last Post: 04-11-2018, 07:19 AM
  2. Date Driven If statement in VBA
    By OverPlayed in forum Modules
    Replies: 4
    Last Post: 03-01-2018, 03:03 PM
  3. Report driven by form combo box
    By Oscar.Ingalls in forum Reports
    Replies: 1
    Last Post: 01-01-2013, 01:28 AM
  4. Database driven website....new ground for me
    By avarusbrightfyre in forum SQL Server
    Replies: 1
    Last Post: 10-09-2011, 08:19 PM
  5. Problem with parameter driven combo box
    By clydet2 in forum Queries
    Replies: 0
    Last Post: 04-06-2009, 12:19 PM

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