【发布时间】:2020-03-14 20:20:00
【问题描述】:
我正在使用 Excel VBA 代码通过 ADODB 连接连接到 CSV 文件(24,179,689 行)。宏运行并使用一列上的特定过滤器从 excel 中获取数据。使用我现在尝试的这个过滤器,它应该返回大约 1500 行数据。
我已经通过在其他地方手动加载 CSV 来检查这一点,数据实际上就在那里。但是当我使用 ADODB 连接时,我的记录集仍然是空的。
我对完整的 CSV 文件做了一些额外的测试:count(*),我看到了错误:它只返回 155,535 行。因此,我正在应用的特定过滤器可能不在该数据中,因此它返回 0 行。
这是我的代码:
Public adoConn As ADODB.Connection
Public adoRS As ADODB.Recordset
Sub getdata()
Set adoConn = New ADODB.Connection
Set adoRS = New ADODB.Recordset
Dim rawFile As String
Dim strSQL As String
'The xlsx file to treat as a database
rawFile = "myPathName"
'Open a connection to the workbook
sconnect = "Provider=Microsoft.ACE.OLEDB.16.0;Data Source=" & rawFile & ";Extended Properties='text;HDR=YES;FMT=Delimited'"
'Write the SQL necessary to get the data you want
sql2 = "SELECT count(*) from [MyFileName.csv]"
'Now we open up our recordset using the connection and the sql statement
adoRS.Open sql2, adoConn, adOpenStatic
Debug.Print (adoRS.EOF)
'Last, we dump the results in this viz sheet
Blad1.Range("A1").CopyFromRecordset adoRS
adoRS.Close
adoConn.Close
End Sub
然后它返回 155,535。
我还尝试通过创建 ADODB 命令而不使用上述连接。或连接超时。没有结果。
这是内存问题还是其他问题?如何解决?
【问题讨论】:
-
你见过这个吗? stackoverflow.com/questions/26441875 - 可能有帮助?
-
我尝试设置为 adOpenForwardOnly 和设置 adoRS.cursurlocation = adUseServer.. 没有改变任何东西