Private Sub txtExportCleanRecords_Click()
DoCmd.OutputTo acOutputQuery, "vCleanRecordsExport", "Excel97- Excel2003Workbook(*.xls)", "",
True, "", , acExportQualityPrint
End Sub
我在訪問報告上創建了一個按鈕,並試圖導出已經創建的查詢。查詢的目的是收集已經驗證和批准的數據,因此數據庫不會變得麻煩。該報告按日期範圍篩選已批准的數據,我只需導出與報告匹配的記錄。因此,查詢ID必須與報告ID匹配,並且我只需要導出與報告ID匹配的記錄。到目前爲止,我的報告所做的是導出整個查詢,我無法弄清楚如何在Command acOutputQuery中放置一個where子句來過濾導出。關於如何調整我的代碼以便我可以根據與查詢ID相匹配的報告ID導出的任何想法?之後我定的SQL語句,不同的是,vMayClean.LogID正的數據相比從報告中導出查詢以訪問excel參數
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
一切工作正常。
"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
那麼,如果沒有錯誤,那是件好事,不是嗎? :p ---除了笑話,有兩個明顯的可能性:1.您沒有創建選擇查詢「ExportQuery」,2.您沒有修改SQL。 '「WHERE QueryID =」&Me.ReportID'只是我的猜測。 – Andre
CurrentDb.QueryDefs(strcExportQuery).SQL = S會導致以下錯誤:在此集合中找不到運行時錯誤'3265'此外,Me.ReportID會導致報告僅調用頂部ID,而不是所有報告ID @ Andre451 –
我的評論中都提到了這兩個問題。如果您需要2的幫助,則需要發佈有關表格和查詢的更多信息以及一些示例數據。我們現在所能做的只是猜測。 – Andre