【发布时间】:2016-10-11 20:09:49
【问题描述】:
每个人。我有一个程序可以每天从一个大表中提取某些数据,然后将这些数据粘贴到一个 Excel 文件中。源表包含大约 200 万条记录(从 01.10.2015 到 09.09.2016),并且以每月 220k 条记录的速度增长。以下过程成功提取了我需要的数据,直到 2016 年 6 月中旬。从那时起,它无法提取任何数据。运行常规查询确实表明数据在那里。打开表格也会显示相同的结果 - 数据截至 2016 年 9 月 9 日可用)。我认为代码还有其他问题,但它确实在另一个表上正常工作。它也适用于这个,但想不出为什么它在 2016 年 6 月中旬之后找不到记录的原因。我索引了日期字段,这是我用于提取记录的参数,但这没有帮助。非常感谢任何帮助和建议。谢谢你的时间。这是我的代码:
Dim xl As Excel.Application
Dim wb As Excel.Workbook
Dim xs As Excel.Worksheet
Dim rng As Excel.Range
Dim c_range As Excel.Range
Dim dbs As DAO.Database
Dim qdf As DAO.QueryDef
Dim rst As DAO.Recordset
Dim strsql As String
strsql = "SELECT tblTraderExecutions.exec_id, tblTraderExecutions.exec_date, tblTraderExecutions.exec_time, tblTraderExecutions.empty, tblTraderExecutions.exec_string, tblTraderExecutions.internal_order_id, tblTraderExecutions.trading_account, " & _
"tblTraderExecutions.trading_account, tblTraderExecutions.operation_bg, tblTraderExecutions.empty, tblTraderExecutions.empty, tblTraderExecutions.symbol, tblTraderExecutions.symbol, tblTraderExecutions.price, tblTraderExecutions.quantity, " & _
"tblTraderExecutions.currency, ROUND(tblTraderExecutions.total_comm,2), tblTraderExecutions.empty, Round((tblTraderExecutions.quantity * tblTraderExecutions.price),2), Round((tblTraderExecutions.quantity * tblTraderExecutions.price),2), " & _
"tblTraderExecutions.trade_broker, tblTraderExecutions.contra, tblTraderExecutions.accept_broker FROM tblTraderExecutions " & _
"WHERE [exec_date] = [start_date] ORDER BY [tblTraderExecutions].[exec_id]"
Set dbs = CurrentDb
Set qdf = dbs.CreateQueryDef(vbNullString, strsql)
qdf.Parameters("start_date").Value = Forms!frmPropReports!txtDate
Set rst = qdf.OpenRecordset
Set xl = New Excel.Application
xl.Visible = True
Set wb = xl.Workbooks.Open("D:\mpuls\trader_executions_export.xlsx")
Set ws = wb.Worksheets("DNEVNICI")
Set rng = ws.Range("a8")
rng.CopyFromRecordset rst
Set c_range = ws.Range("c8:c65000")
c_range.NumberFormat = "h:mm:ss;@"
rst.Close
qdf.Close
Set qdf = Nothing
Set rst = Nothing
Set dbs = Nothing
【问题讨论】:
-
可能是因为您希望处理超过 100 万行,而 Excel 只能存储(在任何给定的工作表上)不超过 1,048,576 行吗?也许 2016 年 6 月是 Excel 达到 100 万限制的地方?
-
根据@Ralph 的建议,您能否放置
rst.MoveLast和MsgBox rst.RecordCount以查看记录集保存了多少条记录? -
或者,您可以使用 `ADODB.Recordset.GetRows()` 方法获取可以写入工作表的数据数组。
-
查询只获取一天的数据,每月有22万条记录,相当于每天1万条记录,距离100万条还有很长的路要走。
-
@Selim:所以您的查询获得了
start_datePARAMETERS start_date DateTime;。
标签: excel ms-access vba export