2012-09-15 90 views
3

我正在使用下面的VBA代碼將Access數據表中的可見列導出到Excel。效果很好,但我也想根據用戶爲每列篩選的內容僅導出已過濾的行。 我可以使用一些幫助,找出如何做到這一點。 任何幫助表示讚賞, LQ從Access數據庫導出篩選行到Excel

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 

回答

1

子窗體的篩選器屬性應該返回適​​合的線路在WHERE語句。例如:

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

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

([My subform].[ID] In (719,720)) 
相關問題