Wondering if you could help. I am populating a table on Excel which has an undetermined length (it grows as people add to it). This indeterminable amount of data needs to be added to a database in Access through the use of a button in Excel. I have produced the following code to try to alleviate this but get a
Run-time error '3709': The connection cannot be used to perform this operation. It is either closed or invalid in this context.
When I open up debugging it points to this line:
rs.Open sqlstr, DBCont
This can be found in the code below:
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
Apologies if the answer is fairly simplistic or I'm missing something crucial, I can't seem to get my head around what's wrong and whether it is possible to loop this type of process.
Many thanks for the responses in advance!