Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA过程仅返回特定日期之前的结果_Excel_Ms Access_Vba_Export - Fatal编程技术网

Excel VBA过程仅返回特定日期之前的结果

Excel VBA过程仅返回特定日期之前的结果,excel,ms-access,vba,export,Excel,Ms Access,Vba,Export,各位。我有一个程序,每天从一个大表中提取某些数据,然后将这些数据粘贴到excel文件中。源表包含约200万条记录(从2015年10月1日开始到2016年9月9日),并以每月22万条记录的速度增长。以下程序成功提取了我只需要到2016年6月中旬的数据。从那时起,它无法提取任何数据。运行常规查询确实表明数据存在。打开表格也会显示相同的结果-数据截至2016年9月9日)。我认为代码还有其他一些问题,但它在另一个表上确实可以正常工作。它在这一方面也很有效,但想不出为什么它找不到2016年6月中旬以前的记

各位。我有一个程序,每天从一个大表中提取某些数据,然后将这些数据粘贴到excel文件中。源表包含约200万条记录(从2015年10月1日开始到2016年9月9日),并以每月22万条记录的速度增长。以下程序成功提取了我只需要到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

首先,我要感谢每一位做出贡献的人。
我的数据库是在Access 2016上创建的,我在另一台pc上运行它,使用Access 2013。这就是它只返回特定日期的记录的地方。在将其移动到具有Access 2016的pc上后,它按预期工作,但仅在后台运行excel,导出的文件中包含所有适用的记录。此时,我将行xl.Visible=true移到了行xl.workbooks.open下面。这很有魅力

是否因为您希望处理超过100万行,而Excel只能(在任何给定的工作表上)存储不超过1048576行?也许2016年6月Excel达到了100万条的上限?根据@Ralph的建议,您可以放置
rst.MoveLast
MsgBox rst.RecordCount
来查看记录集保存了多少条记录?或者,您可以使用'ADODB.recordset.GetRows()`方法获取可写入工作表的数据数组。该查询仅获取一天的数据,每月有220k条记录,相当于每天10k条记录,因此距离100万条记录还有很长的路要走。@Selim:那么您的查询为
start\u date
<“六月中旬”获取n条记录,为以后的日期获取零条记录?--为了确保参数数据类型没有问题,添加
参数start\u date DateTime在选择查询之前。