title: Export query from a report in access to excel with parameters
body: <pre><code>Private Sub txtExportCleanRecords_Click()
  
  DoCmd.OutputTo acOutputQuery, "vCleanRecordsExport", "Excel97- Excel2003Workbook(*.xls)", "", 
  True, "", , acExportQualityPrint    
  
  End Sub
  </code></pre>
  
  <p>I created a button on an access report and I am trying to export a query that is already created.  The intention of the query is gather data that has already been validated and approved so the database does not become cumbersome.  The Report filters approved data by a date range and I need to export only the records that match the report.  So the query ID must match the report ID and I only need the records exported that match the Report ID.  So far all my report is doing is exporting the entire query and I cannot figure out how to put a where clause on the Command acOutputQuery to filter the export.  Any ideas on how I can tweak my code so that I can export based on the report ID matching the query ID?</p>
  
  <pre><code>Private Sub txtExportCleanRecords_Click()
  
  Const strcExportQuery = "ExportQuery"    ' Name of the query for exports.
  Dim S As String
  Dim strFile As String
  
  ' Adapt to your actual fields / filter
  S = "SELECT vMayClean.LogID As 'CleanRecordsID', vMayClean.[Mark Read],    vMayClean.[Report Available], vMayClean.[Has Images], vMayClean.[Performing Resource], vMayClean.Org, " &amp; _
  "vMayClean.[Exam Date/Time], vMayClean.[Patient Nam], vMayClean.[Accession  #], vMayClean.MRN, vMayClean.Modality, vMayClean.[Exam Description], vMayClean.[Exam Status], " &amp; _
  "vMayClean.[Referring Phys], vMayClean.[Ordering Location], vMayClean.COMMENTS, vMayClean.STATUS, vMayClean.Exceptions, vMayClean.CR, vMayClean.[CPU/ Spreadsheets], vMayClean.PACs, vMayClean.Facility, " &amp; _
  "CodeRyteID, [Patient MRN], [Patient Name], [Account Number], [Patient DOB],[Signing Physician Location], [Referring Physician], [Pri Ins Name], " &amp; _
  "[Accident Code], [Accident Event], [Accident Date], [Admit Date], [Signing Physician], [Date Of Service], Service, [Stay Type], " &amp; _
  "[Accession Number], [Order CPT], [CPT Annotation], CPT4, Modifiers, ICD1, ICD2, ICD3, ICD4, [ICD10-1], [ICD10-2], " &amp; _
  "[ICD10-3], [ICD10-4], Units, [Override Queue Level], [Manual Route Queue], [Note Handle] FROM vCleanRecordsExport " &amp; _
  "WHERE vMayClean.LogID = " &amp; Me.ID
  ' Set SQL of export query. This is all it takes, no explicit "Save" needed.
  CurrentDb.QueryDefs(strcExportQuery).SQL = S
  
  strFile = "T:\Users\Reuben\Reconciliation Testing\ExportQuery"
  DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9,   strcExportQuery, strFile
  DoCmd.OutputTo acOutputQuery, "ExportQuery", "Excel97-Excel2003Workbook (*.xls)", "", True, "", , acExportQualityPrint
  
  DoCmd.OpenQuery "vCleanRecordsDelete", acViewNormal, acEdit
  
  End Sub
  </code></pre>
  
  <p>Everything is working properly after I fixed the SQL statement, except for the data that the vMayClean.LogID is being compared to.  </p>
  
  <pre><code>"Where vMayClean.LogID = " &amp; Me.ID
  </code></pre>
  
  <p>This line of code compares the Query ID to only the current Report ID.  What I need is for the export to contain all the query ID's that match all the Report ID's not just one.  Let me know if I need to provide additional information.</p>
  
  <p><em>New EDIT</em></p>
  
  <p>I have added the code that filters the report. The report uses the vCleanRecords Query and I am exporting the vCleanRecordsExport Query which is supposed to have all the records from the vCleanRecords query, but with more data because I am exporting to delete out of the database.  The report is filtered based on a start and end date that matches the date of service.  </p>
  
  <p>I understand what you are saying about having the where clause match the date filter where clause, but how can I tweak it so it works for the report?</p>
  
  <pre><code>Dim strReport As String
  Dim StrDateField As String
  Dim strWhere As String
  Const strcDateConst = "\#mm\/dd\/yyyy\#"
  
  strReport = "rptCleanRecords"
  StrDateField = "[DateOfService]"
  
  If IsDate(Me.txtStartDate) Then
  strWhere = "(" &amp; StrDateField &amp; " &gt;= " &amp; Format(Me.txtStartDate,  strcDateConst) &amp; ")"
  End If
  
  If IsDate(Me.txtEndDate) Then
  If strWhere &lt;&gt; vbNullString Then
      strWhere = strWhere &amp; " AND "
  End If
  strWhere = strWhere &amp; "(" &amp; StrDateField &amp; " &lt; " &amp; Format(Me.txtEndDate + 1, strcDateConst) &amp; ")"
  End If
  </code></pre>
  
  <p>DoCmd.OpenReport strReport, acViewReport, , strWhere, , strWhere</p>