帮我优化以下access中执行的代码并以每份10000条记录的方式导到至指定文件夹以EXECL保存。SELECT top 10000 接触清单.呼叫流水号 AS 录音流水号, IIf(IsNull([区域中心]),"",IIf([区域中心] Like "广州" Or [区域中心] Like "汕头" Or [区域中心] Like "梅州","gz","sz")) AS 区域 FROM 接触清单 WHERE (((IIf(IsNull([区域中心]),"",IIf([区域中心] Like "广州" Or [区域中心] Like "汕头" Or [区域中心] Like "梅州","gz","sz"))) Is Not Null) AND ((接触清单.呼叫日期)>=Date()-5))
时间: 2023-07-13 12:38:12 浏览: 100
以下是优化后的代码:
```
Option Compare Database
Option Explicit
Public Sub ExportData()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String
Dim strFileName As String
Dim i As Integer
'设置导出文件路径和文件名
strFileName = "C:\ExportData\ContactList_"
Set db = CurrentDb()
strSQL = "SELECT 接触清单.呼叫流水号 AS 录音流水号, IIf(IsNull([区域中心]),"",IIf([区域中心] Like '广州' Or [区域中心] Like '汕头' Or [区域中心] Like '梅州','gz','sz')) AS 区域 " _
& " FROM 接触清单 WHERE (((IIf(IsNull([区域中心]),"",IIf([区域中心] Like '广州' Or [区域中心] Like '汕头' Or [区域中心] Like '梅州','gz','sz'))) Is Not Null) AND ((接触清单.呼叫日期)>=Date()-5))"
Set rs = db.OpenRecordset(strSQL, dbOpenSnapshot)
'每10000条记录导出到一个新的文件
Do While Not rs.EOF
If i Mod 10000 = 0 Then
If i <> 0 Then
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "ExportData", strFileName & Format(i, "000000") & ".xlsx", True
End If
CurrentDb.Execute "SELECT TOP 10000 * INTO ExportData FROM (" & strSQL & ") AS T"
rs.Close
Set rs = db.OpenRecordset("ExportData", dbOpenSnapshot)
End If
i = i + 1
rs.MoveNext
Loop
'导出最后一份文件
If i Mod 10000 <> 0 Then
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "ExportData", strFileName & Format(i - (i Mod 10000), "000000") & ".xlsx", True
End If
'清除临时表
db.Execute "DROP TABLE ExportData"
rs.Close
Set rs = Nothing
Set db = Nothing
End Sub
```
该代码将每10000条记录导出到一个新的文件,并以格式化的文件名保存到指定文件夹中。这里使用了一个临时表来存储查询结果,然后每导出10000条记录就清空该表,以便保存下一个文件。最后导出最后一份文件并清除临时表。注意,该代码需要在Access中运行,无法直接在VBA中运行。
阅读全文