ADODB 连接为空,无法读取 CSV 文件
ADODB Connection is empty, Unable to read CSV file
我得到了下面的代码,可以处理除 CSV 文件之外的所有文件类型。
我正在尝试将 CSV 文件复制到工作表中,但我弄糊涂了。我们将不胜感激。
调试器显示:
rsConn = ""
szSQL = "SELECT * FROM export.csv"
当我点击
rsData.Open szSQL, rsCon, 0, 1, 1
我转到 myError
错误处理程序
我已将问题归结为这些步骤,但我找不到答案。
szSQL = "SELECT * FROM export.cvs"
Set rsCon = CreateObject("ADODB.Connection")
Set rsData = CreateObject("ADODB.Recordset")
rsCon.Open szConnect
rsData.Open szSQL, rsCon, 0, 1, 1
'These are the values being passed in.
GetData("export.csv" ,"A1:BE", "BirdFeet", "A1", "sku", True, True
完整代码在这里:
Public Sub GetData(SourceFile As Variant, SourceSheet As String, SourceRange As String, _
TargetSheet As String, TargetRange As String, _
TargetSortColumn As String, _
HaveHeader As Boolean, UseHeaderRow As Boolean)
Dim lColumn As Long
Dim lCount As Long
Dim lRow As Long
Dim rsCon As Object
Dim rsData As Object
Dim szConnect As String
Dim szSQL As String
lRow = Range(TargetRange).Row
lColumn = Range(TargetRange).Column
' Create the connection string.
If HaveHeader = False Then 'No there is NOT a header row.
If Val(Application.Version) < 12 Then
szConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & SourceFile & ";" & _
"Extended Properties=""Excel 8.0;HDR=No"";"
Else
szConnect = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & SourceFile & ";" & _
"Extended Properties=""Excel 12.0;HDR=No"";"
End If
Else 'Yes there is a Header Row
If Val(Application.Version) < 12 Then
szConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & SourceFile & ";" & _
"Extended Properties=""Excel 8.0;HDR=Yes"";"
Else
If (Right(SourceSheet, 4) = ".csv") Then
szConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & SourceFile & ";" & _
"Extended Properties=""text; HDR=Yes; FMT=Delimited; IMEX=1;"""
Else
szConnect = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & SourceFile & ";" & _
"Extended Properties=""Excel 12.0;HDR=Yes"";"
End If
End If
End If
If SourceSheet = "" Then
'Create query strings
szSQL = "SELECT * FROM " & SourceRange$ & " ORDER BY sku;"
ElseIf SourceSheet = "DiamondAvian" Or SourceSheet = "export.csv" Then
szSQL = "SELECT * FROM export.csv"
Else
szSQL = "SELECT * FROM [" & SourceSheet$ & "$" & SourceRange$ & "] WHERE sku <> NULL ORDER BY " & TargetSortColumn & ";" 'THIS WORKS FOR DICIONARY
End If
On Error GoTo SomethingWrong
Set rsCon = CreateObject("ADODB.Connection")
Set rsData = CreateObject("ADODB.Recordset")
rsCon.Open szConnect
rsData.Open szSQL, rsCon, 0, 1, 1
' Check to make sure we received data and copy the data
If Not rsData.EOF Then
If HaveHeader = False Then
Cells(1, 1).CopyFromRecordset rsData
Else
'Add the header cell in each column if the last argument is True
If UseHeaderRow Then
For lCount = 0 To rsData.Fields.Count - 1 'Builds the Header row one column at a time.
Cells(lRow, lColumn + lCount).value = rsData.Fields(lCount).Name 'lcount determines the Column to paste header info in.
Next lCount
Cells(lRow + 1, lColumn).CopyFromRecordset rsData 'This is the step that copies and Pastes the data.
Else
Cells(lRow + 1, lColumn).CopyFromRecordset rsData
End If
End If
Else
MsgBox "No records returned from : " & SourceFile, vbCritical
End If
rsData.Close ' Clean up our Recordset object.
Set rsData = Nothing
rsCon.Close
Set rsCon = Nothing
Exit Sub
SomethingWrong:
MsgBox "The file name, Sheet name or Range is invalid of : " & SourceFile, vbExclamation, "Error"
On Error GoTo 0
End Sub
连接字符串为空,因为向 GetData 发送了错误的参数。看看这些代码片段:
SourceSheet
是调用中的 second 参数,但您将其作为 first 传递参数在这里:
' Here's the call to GetData
GetData("export.csv" ,"A1:BE", ...
文件名作为第一个参数...但这里是 GetData
的声明
' But look at the declaration...SourceSheet is 2nd param, not 1st...
Public Sub GetData(SourceFile As Variant, SourceSheet As String,...
现在,查看确定连接字符串的代码:
' later in code
If (Right(SourceSheet, 4) = ".csv") Then ' It contains "A1:BE", so your logic goes awry and your connection string ends up incorrect...
认为解决打嗝会解决你的问题。
我得到了下面的代码,可以处理除 CSV 文件之外的所有文件类型。
我正在尝试将 CSV 文件复制到工作表中,但我弄糊涂了。我们将不胜感激。
调试器显示:
rsConn = ""
szSQL = "SELECT * FROM export.csv"
当我点击
rsData.Open szSQL, rsCon, 0, 1, 1
我转到 myError
错误处理程序
我已将问题归结为这些步骤,但我找不到答案。
szSQL = "SELECT * FROM export.cvs"
Set rsCon = CreateObject("ADODB.Connection")
Set rsData = CreateObject("ADODB.Recordset")
rsCon.Open szConnect
rsData.Open szSQL, rsCon, 0, 1, 1
'These are the values being passed in.
GetData("export.csv" ,"A1:BE", "BirdFeet", "A1", "sku", True, True
完整代码在这里:
Public Sub GetData(SourceFile As Variant, SourceSheet As String, SourceRange As String, _
TargetSheet As String, TargetRange As String, _
TargetSortColumn As String, _
HaveHeader As Boolean, UseHeaderRow As Boolean)
Dim lColumn As Long
Dim lCount As Long
Dim lRow As Long
Dim rsCon As Object
Dim rsData As Object
Dim szConnect As String
Dim szSQL As String
lRow = Range(TargetRange).Row
lColumn = Range(TargetRange).Column
' Create the connection string.
If HaveHeader = False Then 'No there is NOT a header row.
If Val(Application.Version) < 12 Then
szConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & SourceFile & ";" & _
"Extended Properties=""Excel 8.0;HDR=No"";"
Else
szConnect = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & SourceFile & ";" & _
"Extended Properties=""Excel 12.0;HDR=No"";"
End If
Else 'Yes there is a Header Row
If Val(Application.Version) < 12 Then
szConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & SourceFile & ";" & _
"Extended Properties=""Excel 8.0;HDR=Yes"";"
Else
If (Right(SourceSheet, 4) = ".csv") Then
szConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & SourceFile & ";" & _
"Extended Properties=""text; HDR=Yes; FMT=Delimited; IMEX=1;"""
Else
szConnect = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & SourceFile & ";" & _
"Extended Properties=""Excel 12.0;HDR=Yes"";"
End If
End If
End If
If SourceSheet = "" Then
'Create query strings
szSQL = "SELECT * FROM " & SourceRange$ & " ORDER BY sku;"
ElseIf SourceSheet = "DiamondAvian" Or SourceSheet = "export.csv" Then
szSQL = "SELECT * FROM export.csv"
Else
szSQL = "SELECT * FROM [" & SourceSheet$ & "$" & SourceRange$ & "] WHERE sku <> NULL ORDER BY " & TargetSortColumn & ";" 'THIS WORKS FOR DICIONARY
End If
On Error GoTo SomethingWrong
Set rsCon = CreateObject("ADODB.Connection")
Set rsData = CreateObject("ADODB.Recordset")
rsCon.Open szConnect
rsData.Open szSQL, rsCon, 0, 1, 1
' Check to make sure we received data and copy the data
If Not rsData.EOF Then
If HaveHeader = False Then
Cells(1, 1).CopyFromRecordset rsData
Else
'Add the header cell in each column if the last argument is True
If UseHeaderRow Then
For lCount = 0 To rsData.Fields.Count - 1 'Builds the Header row one column at a time.
Cells(lRow, lColumn + lCount).value = rsData.Fields(lCount).Name 'lcount determines the Column to paste header info in.
Next lCount
Cells(lRow + 1, lColumn).CopyFromRecordset rsData 'This is the step that copies and Pastes the data.
Else
Cells(lRow + 1, lColumn).CopyFromRecordset rsData
End If
End If
Else
MsgBox "No records returned from : " & SourceFile, vbCritical
End If
rsData.Close ' Clean up our Recordset object.
Set rsData = Nothing
rsCon.Close
Set rsCon = Nothing
Exit Sub
SomethingWrong:
MsgBox "The file name, Sheet name or Range is invalid of : " & SourceFile, vbExclamation, "Error"
On Error GoTo 0
End Sub
连接字符串为空,因为向 GetData 发送了错误的参数。看看这些代码片段:
SourceSheet
是调用中的 second 参数,但您将其作为 first 传递参数在这里:
' Here's the call to GetData
GetData("export.csv" ,"A1:BE", ...
文件名作为第一个参数...但这里是 GetData
的声明' But look at the declaration...SourceSheet is 2nd param, not 1st...
Public Sub GetData(SourceFile As Variant, SourceSheet As String,...
现在,查看确定连接字符串的代码:
' later in code
If (Right(SourceSheet, 4) = ".csv") Then ' It contains "A1:BE", so your logic goes awry and your connection string ends up incorrect...
认为解决打嗝会解决你的问题。