簡體   English   中英

從Access數據表導出過濾行到Excel

[英]Export Filtered Rows from Access Datasheet to Excel

我正在使用下面的VBA代碼將Access數據表中的可見列導出到Excel。 工作得很好,但我還想根據用戶為每列過濾的內容僅導出過濾的行。 我可以用一些幫助來弄清楚如何做到這一點。 任何幫助表示贊賞,lq

 Public Function ExportToExcel()
 On Error GoTo myErr

     Dim strDestFolder As String
     Dim strSQL As String
     Dim qdf As dao.QueryDef
     Dim ctl As Control
     Dim strRandomString As String

     strDestFolder = "MyDestinationPath"
     strRandomString = "AQueryDefName"

     For Each ctl In Me.Child.Form.Controls

         If Not ctl.ColumnHidden Then
             If Len(strSQL) = 0 Then
                 strSQL = "SELECT " & ctl.ControlSource & ", "
             Else
                 strSQL = strSQL & ctl.ControlSource & ", "
             End If
         End If

     Next ctl

     If Len(strSQL) > 0 Then
         strSQL = Left(strSQL, Len(strSQL) - 2)
         strSQL = strSQL & " FROM tblMyTableName WHERE Something = '" & Me.Something & "'"
     End If

     Set qdf = CurrentDb.CreateQueryDef(strRandomString, strSQL)

     DoCmd.OutputTo acOutputQuery, strRandomString, "MicrosoftExcel (*.xls)", strDestFolder & ".xls", True

 myExit:
     CurrentDb.QueryDefs.Delete strRandomString
     Exit Function
 myErr:
     MsgBox Err.Number & " - " & Err.Description, vbCritical, "VBA Error"
     Resume myExit
 End Function

子窗體的filter屬性應返回適合在where語句中使用的行。 例如:

([My subform].[ID] Not In (1,2,4,6))

當我選擇下拉列表並選擇一些數字時返回。 也:

([My subform].[ID] In (719,720))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM