0
Private Sub txtExportCleanRecords_Click()

DoCmd.OutputTo acOutputQuery, "vCleanRecordsExport", "Excel97- Excel2003Workbook(*.xls)", "", 
True, "", , acExportQualityPrint    

End Sub

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?

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, " & _
"vMayClean.[Exam Date/Time], vMayClean.[Patient Nam], vMayClean.[Accession  #], vMayClean.MRN, vMayClean.Modality, vMayClean.[Exam Description], vMayClean.[Exam Status], " & _
"vMayClean.[Referring Phys], vMayClean.[Ordering Location], vMayClean.COMMENTS, vMayClean.STATUS, vMayClean.Exceptions, vMayClean.CR, vMayClean.[CPU/ Spreadsheets], vMayClean.PACs, vMayClean.Facility, " & _
"CodeRyteID, [Patient MRN], [Patient Name], [Account Number], [Patient DOB],[Signing Physician Location], [Referring Physician], [Pri Ins Name], " & _
"[Accident Code], [Accident Event], [Accident Date], [Admit Date], [Signing Physician], [Date Of Service], Service, [Stay Type], " & _
"[Accession Number], [Order CPT], [CPT Annotation], CPT4, Modifiers, ICD1, ICD2, ICD3, ICD4, [ICD10-1], [ICD10-2], " & _
"[ICD10-3], [ICD10-4], Units, [Override Queue Level], [Manual Route Queue], [Note Handle] FROM vCleanRecordsExport " & _
"WHERE vMayClean.LogID = " & 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

Everything is working properly after I fixed the SQL statement, except for the data that the vMayClean.LogID is being compared to.

"Where vMayClean.LogID = " & Me.ID

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.

New EDIT

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.

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?

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 = "(" & StrDateField & " >= " & Format(Me.txtStartDate,  strcDateConst) & ")"
End If

If IsDate(Me.txtEndDate) Then
If strWhere <> vbNullString Then
    strWhere = strWhere & " AND "
End If
strWhere = strWhere & "(" & StrDateField & " < " & Format(Me.txtEndDate + 1, strcDateConst) & ")"
End If

DoCmd.OpenReport strReport, acViewReport, , strWhere, , strWhere

1 Answer 1

0

This doesn't work directly. DoCmd.OpenForm or DoCmd.OpenReport have a parameter WhereCondition, but DoCmd.OutputTo or DoCmd.TransferSpreadsheet don't.

The easiest way is to have an "Export query", where you dynamically set its SQL property to the filtered list. Create this query manually with a dummy SQL, afterwards the SQL will be overwritten with each export.

Adapted from a newsgroup post by Allen Browne:

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 * FROM vCleanRecordsExport " & _
    "WHERE QueryID = " & Me.ReportID
' Set SQL of export query. This is all it takes, no explicit "Save" needed.
CurrentDb.QueryDefs(strcExportQuery).SQL = S

strFile = "C:\Data\MyExport.xls"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, strcExportQuery, strFile

Edit:
Maybe I'm starting to understand what you mean by "Report ID" and "Query ID".
Your report lists a number of record, filtered by date?
And the export query should export the same set of records (i.e. the records with the same ID)?

Then: what is the record source for the report?

If it's something like

SELECT Fields FROM Table WHERE someDate BETWEEN x AND y

then you should use the same filter or WHERE clause for your export query (someDate BETWEEN x AND y).

If I'm wrong: where do the IDs for the report come from?


Edit 2
So you are already constructing a WHERE string (strWhere), and passing it as OpenArgs to the report.

Then you can simply use that string instead of vMayClean.LogID = " & Me.ID.

You have the field in the export code as [Date Of Service],
and in the report code as StrDateField = "[DateOfService]".
If it's not possible to use the same name in both queries, you can do

strWhere = Replace(Me.OpenArgs, "[DateOfService]", "[Date Of Service]")

in the Export code.

5
  • Well, if there isn't an error, that's a good thing, no? :p --- Jokes aside, there are two obvious possibilities: 1. you didn't create a select query "ExportQuery" and 2. you didn't adapt the SQL. "WHERE QueryID = " & Me.ReportID was just my guess.
    – Andre
    Sep 1, 2015 at 17:22
  • CurrentDb.QueryDefs(strcExportQuery).SQL = S causes the following error: Run-time error '3265' Item not found in this collection Also, Me.ReportID causes the report to only call the top ID not all report ID's @Andre451 Sep 1, 2015 at 17:36
  • Both problem are addressed in my comment. If you need help for 2., you need to post more information about the table(s) and query, and some sample data. All we can do at the moment is guess.
    – Andre
    Sep 1, 2015 at 17:40
  • I provided an edit with more detailed information on what is working properly and what I still need. Thank you so much for assisting with this issue. @Andre451 Sep 1, 2015 at 21:06
  • Edited with a suggestion and more questions. I think we are getting somewhere. :)
    – Andre
    Sep 1, 2015 at 22:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.