在Excel VBA中,我使用AdoDB连接通过SQL Select从.csv文件中检索数据。 .csv文件具有超过255个字段。
不幸的是,它仅适用于.csv文件的前255个字段。
有什么办法可以处理更多字段?
我已经学会了一种我不想使用的方法。它包括在单个字符串中选择所有字段,然后将其拆分,然后使用集合。就我的目的而言,这不是一个好方法。我正在寻找一种更通用的解决方案,该解决方案可以处理超过255个字段。
谢谢。
这是代码:
Public Function Ep(et As Variant, myep As Variant, fc As Date, ent As Variant, gi As String, Optional myid As String)
Dim myfc As String
Dim xlcon As ADODB.Connection
Dim xlrs As ADODB.Recordset
Set xlcon = New ADODB.Connection
Set xlrs = New ADODB.Recordset
Dim currentDataFilePath As String
Dim currentDataFileName As String
Dim nextRow As Integer
currentDataFilePath = "C:\TRABAJO\Epis"
currentDataFileName = "" & gi & et & ""
myfc = DateToText(fc)
xlcon.Provider = "Microsoft.Jet.OLEDB.4.0"
xlcon.ConnectionString = "Data Source=" & currentDataFilePath & ";" & "Extended Properties=""text;HDR=Yes;"""
xlcon.Open
If myid = "" Then
MyQuery = "SELECT " & myep & " AS DATO FROM [" & currentDataFileName & ".csv] WHERE ENT=" & ent & " AND FECHA='" & myfc & "'"
ElseIf myid <> "" Then
MyQuery = "SELECT " & myep & " AS DATO FROM [" & currentDataFileName & ".csv] WHERE ENT=" & ent & " AND FECHA='" & myfc & "' AND ID=" & myid & ""
End If
Set xlrs = xlcon.Execute(MyQuery)
resultado = xlrs.Fields(0)
xlrs.Close
Set xlrs = Nothing
xlcon.Close
Set xlcon = Nothing
Ep = resultado
结束功能