简体   繁体   English

从报表中导出查询以使用参数访问Excel

[英]Export query from a report in access to excel with parameters

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. 因此,查询ID必须与报告ID匹配,并且我只需要导出与报告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. 到目前为止,我的报告所做的只是导出整个查询,我无法弄清楚如何在acOutputQuery命令上放置where子句以过滤导出。 Any ideas on how I can tweak my code so that I can export based on the report ID matching the query ID? 关于如何调整代码以便可以根据与查询ID匹配的报表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. 修复SQL语句后,除了将vMayClean.LogID与之进行比较的数据外,其他所有内容均正常运行。

"Where vMayClean.LogID = " & Me.ID

This line of code compares the Query ID to only the current Report ID. 此行代码将查询ID仅与当前报告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. 我需要的是导出包含与所有报告ID匹配的所有查询ID,而不仅仅是一个。 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. 该报告使用vCleanRecords查询,并且我正在导出vCleanRecordsExport查询,该查询应该具有vCleanRecords查询中的所有记录,但是包含更多数据,因为我正在导出以从数据库中删除。 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? 我了解您在说让where子句与日期过滤器where子句匹配,但是我该如何调整它使其适用于报表呢?

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 DoCmd.OpenReport strReport,acViewReport,strWhere,strWhere

This doesn't work directly. 这不直接起作用。 DoCmd.OpenForm or DoCmd.OpenReport have a parameter WhereCondition , but DoCmd.OutputTo or DoCmd.TransferSpreadsheet don't. DoCmd.OpenFormDoCmd.OpenReport具有参数WhereCondition ,但DoCmd.OutputToDoCmd.TransferSpreadsheet没有。

The easiest way is to have an "Export query", where you dynamically set its SQL property to the filtered list. 最简单的方法是拥有一个“导出查询”,您可以在其中将其SQL属性动态设置为过滤后的列表。 Create this query manually with a dummy SQL, afterwards the SQL will be overwritten with each export. 使用虚拟SQL手动创建此查询,此后每次导出都会覆盖该SQL。

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". 也许我开始理解“报告ID”和“查询ID”的含义。
Your report lists a number of record, filtered by date? 您的报告列出了一些记录,按日期过滤?
And the export query should export the same set of records (ie the records with the same ID)? 导出查询应该导出相同的记录集(即具有相同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 ). 那么您应该对导出查询使用相同的过滤器或WHERE子句( someDate BETWEEN x AND y )。

If I'm wrong: where do the IDs for the report come from? 如果我错了:报告的ID来自何处?


Edit 2 编辑2
So you are already constructing a WHERE string (strWhere), and passing it as OpenArgs to the report. 因此,您已经在构造WHERE字符串(strWhere),并将其作为OpenArgs传递给报表。

Then you can simply use that string instead of vMayClean.LogID = " & Me.ID . 然后,您可以简单地使用该字符串而不是vMayClean.LogID = " & Me.ID

You have the field in the export code as [Date Of Service] , 您在导出代码中的字段为[Date Of Service]
and in the report code as StrDateField = "[DateOfService]" . 在报告代码中为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. 在导出代码中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM