I keep getting Run-time Error 3709 in Microsoft Visual Basic, "The connection cannot be used to perform this operation. It is either closed or invalide in this context." I am trying to write a macro in Microsft 2010 to ammend an Access Table in ACCESS 2010. Below is the line that keeps causing this run-time error.
rs.Open "FY18 Fee Review Mod Cost Table Data", cn, adOpenKeyset, adLockOptimistic, adCmdTable"
Code_Goes_Here
Sub ADOFromExcelToAccess()
' exports data from the active worksheet to a table in an Access database
' this procedure must be edited before use
Dim cn As ADODB.Connection, rs As ADODB.Recordset, r As Long
' connect to the Access database
Set cn = New ADODB.Connection
cn.Open "provider = Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=R:\Modular Cost Tables\FY 2018A\RCA Database Prep\Terry's Updates\FY 2018 AOP RCA Database_062717_SCOPS Locations.accdb;"
' open a recordset
Set rs = New ADODB.Recordset
rs.Open "FY18 Fee Review Mod Cost Table Data", cn, adOpenKeyset, adLockOptimistic, adCmdTable"
' all records in a table
r = 2 ' the start row in the worksheet
Do While Len(Range("A" & r).Formula) > 0
' repeat until first empty cell in column A
With rs
.AddNew ' create a new record
' add values to each field in the record
.Fields("Project_ID") = Range("A" & r).Number
.Fields("Item") = Range("B" & r).Text
.Fields("Object_Class") = Range("C" & r).Text
.Fields("OC_Name") = Range("D" & r).Text
.Fields("ProjectTask") = Range("E" & r).Text
.Fields("Fund") = Range("F" & r).Text
.Fields("Prog") = Range("G" & r).Text
.Fields("Object") = Range("H" & r).Text
.Fields("FeeReviewOrgDash") = Range("I" & r).Text
.Fields("FeeReviewOrgName") = Range("J" & r).Text
.Fields("Request_Category") = Range("K" & r).Text
.Fields("Cost_Type") = Range("L" & r).Text
.Fields("Obj_Type") = Range("M" & r).Text
.Fields("FY_2018_Total") = Range("N" & r).Currency
.Fields("FY_2019_Total") = Range("O" & r).Currency
.Fields("FY_2020_Total") = Range("P" & r).Currency
.Fields("Locality") = Range("Q" & r).Text
.Fields("Office") = Range("R" & r).Text
' add more fields if necessary
.Update ' stores the new record
End With
r = r + 1 ' next row
Loop
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub