【发布时间】:2018-06-25 10:28:42
【问题描述】:
想知道您是否可以提供帮助。我在 Excel 上填充了一个长度不确定的表格(它随着人们添加它而增长)。需要通过使用 Excel 中的按钮将这些无法确定的数据量添加到 Access 中的数据库中。我已经生成了以下代码来尝试缓解这种情况,但得到了
运行时错误“3709”:无法使用连接来执行此操作。在这种情况下,它要么是关闭的,要么是无效的。
当我打开调试它指向这一行:
rs.Open sqlstr, DBCont
这可以在下面的代码中找到:
Sub submittoDB()
Dim DBCont As Variant
Set DBCont = CreateObject("ADODB.connection")
Dim StrDBPath As String
StrDBPath = "PATH Here\Database1.accdb"
Dim sConn As String
sConn = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & StrDBPath & ";" & _
"Jet OLEDB:Engine Type=5;" & _
"Persist Security Info=False;"
DBCont.Open sConn
MsgBox "Open DB"
Dim rs As Object
Set rs = CreateObject("ADODB.Recordset")
Dim sqlstr As String
Dim endlimit As Integer
Dim i As Integer
endlimit = Cells(Rows.Count, "H").End(xlUp).Row + 1
'need to loop each line and remove test
For i = 5 To endlimit
sqlstr = "INSERT INTO DigiFinTracker (ProjectNo, ProjectName, Client, Tool, SuggAct, PercSav,PreDigCost,SuggSave,PropSav) VALUES ('" & Cells(6, 4) & "', '" _
& Cells(5, 4) & "', '" & Cells(4, 4) & "', '" & Cells(i, 8) & "', '" & Cells(i, 9) & "', '" & Cells(i, 10) _
& "', '" & Cells(i, 11) & "', '" & Cells(i, 12) & "', '" & Cells(i, 13) & "')"
rs.Open sqlstr, DBCont
DBCont.Close
Next i
DBCont.Close
End Sub
如果答案相当简单,或者我遗漏了一些重要的东西,我很抱歉,我似乎无法弄清楚出了什么问题以及是否可以循环这种类型的过程。
非常感谢您提前回复!
【问题讨论】:
-
您正在关闭每个循环的连接
标签: excel vba loops ms-access adodb