Private Sub txtExportCleanRecords_Click()
DoCmd.OutputTo acOutputQuery, "vCleanRecordsExport", "Excel97- Excel2003Workbook(*.xls)", "",
True, "", , acExportQualityPrint
End Sub
我在访问报告上创建了一个按钮,我正在尝试导出已创建的查询。查询的目的是收集已经过验证和批准的数据,这样数据库就不会变得很麻烦。报告按日期范围过滤批准的数据,我只需要导出与报告匹配的记录。因此查询ID必须与报告ID匹配,我只需要导出与报告ID匹配的记录。到目前为止,我的所有报告都在导出整个查询,我无法弄清楚如何在命令acOutputQuery上放置一个where子句来过滤导出。有关如何调整代码的任何想法,以便我可以根据与查询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
修复SQL语句后,一切正常,除了与vMayClean.LogID进行比较的数据。
"Where vMayClean.LogID = " & Me.ID
此行代码将查询ID与仅当前报告ID进行比较。我需要的是导出包含匹配所有报告ID的所有查询ID,而不仅仅是一个。如果我需要提供更多信息,请与我们联系。
新编辑
我添加了过滤报告的代码。该报告使用vCleanRecords查询,我正在导出vCleanRecordsExport查询,该查询应该具有来自vCleanRecords查询的所有记录,但是由于我正在导出以从数据库中删除,因此具有更多数据。根据与服务日期匹配的开始日期和结束日期过滤报告。
我理解你所说的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
答案 0 :(得分:0)
这不直接起作用。 DoCmd.OpenForm
或DoCmd.OpenReport
有一个参数WhereCondition
,但DoCmd.OutputTo
或DoCmd.TransferSpreadsheet
没有。
最简单的方法是使用&#34;导出查询&#34;,您可以将其SQL属性动态设置为已过滤的列表。使用伪SQL手动创建此查询,然后每次导出都会覆盖SQL。
改编自Allen Browne的newsgroup post:
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
修改强>
也许我开始明白你的意思&#34;报告ID&#34;和&#34;查询ID&#34;。
您的报告列出了一些记录,按日期过滤?
导出查询应该导出同一组记录(即具有相同ID的记录)?
然后:报告的记录来源是什么?
如果它像
那样SELECT Fields FROM Table WHERE someDate BETWEEN x AND y
然后您应该为导出查询使用相同的过滤器或WHERE子句(someDate BETWEEN x AND y
)。
如果我错了:报告的ID来自何处?
编辑2
所以你已经构建了一个WHERE字符串(strWhere),并将它作为OpenArgs传递给报告。
然后您只需使用该字符串而不是vMayClean.LogID = " & Me.ID
。
导出代码中的字段为[Date Of Service]
,
并在报告代码中为StrDateField = "[DateOfService]"
如果在两个查询中都无法使用相同的名称,则可以执行
strWhere = Replace(Me.OpenArgs, "[DateOfService]", "[Date Of Service]")
在导出代码中。