帮我优化以下代码,实现将表"导出数据"每次以10000条导出,直至数据记录导完,ExportData为临时表,导出完成后需要删除。 Dim db As DAO.Database Dim rs As DAO.Recordset Dim strSQL As String Dim strFileName As String Dim i As Long Dim batchSize As Long '每批次导出的记录数 Dim batchCount As Long '批次计数器 Dim exportCount As Long '导出计数器 '设置导出文件路径和文件名 strFileName = "C:\Users\icd\Desktop\流水分割" batchSize = 10000 '每批次导出的记录数 Set db = CurrentDb() strSQL = "SELECT 导出数据.* FROM 导出数据" Set rs = db.OpenRecordset(strSQL, dbOpenSnapshot) '循环导出数据 Do While Not rs.EOF If exportCount Mod batchSize = 0 Then '每批次导出到一个新的文件 If exportCount <> 0 Then DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "ExportData", strFileName & Format(batchCount, "000000") & ".xlsx", True End If batchCount = batchCount + 1 End If exportCount = exportCount + 1 rs.MoveNext Loop '导出最后一份文件 If exportCount Mod batchSize <> 0 Then DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "导出数据", strFileName & Format(batchCount, "000000") & ".xlsx", True End If rs.Close Set rs = Nothing Set db = Nothing
时间: 2024-03-15 09:45:28 浏览: 100
数据表导出
以下是优化后的代码:
```
Dim strSQL As String
Dim strFileName As String
Dim i As Long
Dim batchSize As Long '每批次导出的记录数
Dim batchCount As Long '批次计数器
Dim exportCount As Long '导出计数器
'设置导出文件路径和文件名
strFileName = "C:\Users\icd\Desktop\流水分割"
batchSize = 10000 '每批次导出的记录数
DoCmd.SetWarnings False '暂时关闭Access的警告信息
DoCmd.RunSQL "SELECT 导出数据.* INTO ExportData FROM 导出数据" '将表导入临时表ExportData
DoCmd.SetWarnings True '重新开启Access的警告信息
Set rs = CurrentDb.OpenRecordset("SELECT * FROM ExportData", dbOpenSnapshot) '打开临时表ExportData
'循环导出数据
Do While Not rs.EOF
If exportCount Mod batchSize = 0 Then
'每批次导出到一个新的文件
If exportCount <> 0 Then
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "ExportData", strFileName & Format(batchCount, "000000") & ".xlsx", True
End If
batchCount = batchCount + 1
End If
exportCount = exportCount + 1
rs.MoveNext
Loop
'导出最后一份文件
If exportCount Mod batchSize <> 0 Then
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "ExportData", strFileName & Format(batchCount, "000000") & ".xlsx", True
End If
rs.Close
Set rs = Nothing
DoCmd.SetWarnings False '暂时关闭Access的警告信息
DoCmd.RunSQL "DROP TABLE ExportData" '删除临时表ExportData
DoCmd.SetWarnings True '重新开启Access的警告信息
```
优化说明:
1. 将表导入临时表ExportData,避免每次都从原表导出数据,提高导出效率。
2. 暂时关闭Access的警告信息,避免一些不必要的提示信息。
3. 添加注释,提高代码可读性。
4. 删除临时表ExportData,避免数据冗余。
阅读全文