vba中的ADODB.Connection:关闭对象时不允许操作

时间:2015-10-21 15:00:26

标签: vba excel-vba ado excel

我正在最终确定一个vba宏,并想知道是否有人可以帮助解决以下错误消息:

run-time error '3704'
operation is not allowed when the object is closed

代码:

================================

Sub upload()
'
' upload Macro
'
Dim adoCN As ADODB.Connection
Dim sConnString As String
Dim sSQL As String
Dim lRow As Long
Dim i As Integer

sConnString = "Provider=sqloledb;Server=xxx;User Id=xxx;Password=xxx"
Set adoCN = CreateObject("ADODB.Connection")
Sheets("sql").Activate
Range(Cells(1, 1), Cells(1, 1).End(xlDown)).Select
lRow = Selection.Rows.Count
For i = 2 To lRow
sSQL = "INSERT INTO tblData (ISN, Date, Px_last) " & _
        " VALUES (" & _
        "'" & Cells(i, 1) & "', " & _
        "'" & Cells(i, 2) & "', " & _
        "'" & Cells(i, 3) & "')"
adoCN.Execute sSQL
Next i
adoCN.Close
Set adoCN = Nothing
End Sub

最佳,

1 个答案:

答案 0 :(得分:3)

您需要先打开连接才能使用它。

sConnString = "Provider=sqloledb;Server=xxx;User Id=xxx;Password=xxx"
Set adoCN = CreateObject("ADODB.Connection")
adoCN.Open