MS Access vba导出表单结果优于where条件

时间:2017-02-14 23:05:10

标签: excel vba excel-vba ms-access

我有一个包含很多字段的表,然后是一个只包含其中几个字段的表单。我在表单上有一个搜索按钮,您可以在其中选择某些记录。在VBA中是否有一种方法可以从表单中导出结果,但包括表中的所有字段。

以下是我发现的一些代码的尝试:

Private Sub Command49_Click()
     Dim strWhere As String
     Dim strFile As String
     Const strcStub = "SELECT * FROM tblMaster " & vbCrLf
     Const strcTail = "ORDER BY ID;"
     Const strcExportQuery = "Query1"    'Name of the query for exports.

'Keyword
If Nz(Me.tKW, "") <> "" Then
    strWhere = strWhere & "[iavmtitle] Like '*" & Replace(Me.tKW, " '", "''") & "*' AND "
End If

'Release Date From
If Nz(Me.tRF, "") <> "" Then
   strWhere = strWhere & "[releaseDate] between " & "#" & Me.tRF & "# AND #" & Me.tRT & "#" & " AND "
End If

'Expliots
If Nz(Me.cmbExploits, "") <> "" Then
    strWhere = strWhere & "[knownExploits] = '" & Me.cmbExploits & "' AND "
End If

'Incidents
If Nz(Me.cmdIncidents, "") <> "" Then
    strWhere = strWhere & "[knownDodIncidents] = '" & Me.cmdIncidents & "' AND "
End If

'Release Date From
If Nz(Me.txtSaveSend, "") <> "" Then
   strWhere = strWhere & "[lastSaved] > " & "#" & Me.txtSaveSend & "#" & " AND "
End If

If strWhere <> "" Then
    strWhere = Left(strWhere, Len(strWhere) - 5) 'Remove the extra AND
    Me.Filter = strWhere
    Me.FilterOn = True
Else
    Me.Filter = ""
    Me.FilterOn = False
End If
     If Me.FilterOn Then
         strWhere = "WHERE " & Me.Filter & vbCrLf
     End If
     CurrentDb.QueryDefs(strcExportQuery).SQL = strcStub & strWhere & strcTail

     strFile = "C:\Data\MyExport.xls"
     DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, _
         strcExportQuery, strFile
End Sub

0 个答案:

没有答案