I am trying to update an Access database from an excel spreadsheet. In the beginning of the process, the users will be entering unique records into the sheet(ProspectName). Subsequently they will be modifying the records. Here is the code that I have used before (modified for this workbook). I am getting a runtime error 3021 which I think is caused by the fact that the SQL query returns no records. I guess the solution will need to test for this condition and then either update or insert the record.
/*
Sub AlterAllRecords()
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim fld As ADODB.Field
Dim MyConn
Dim lngRow As Long
Dim lngID, LR, Upd, SM
Dim j As Long
Dim sSQL As String
Worksheets("2012 Prospects").Select
LR = Range("A" & Rows.Count).End(xlUp).Row
lngRow = 3
Do While lngRow <= LR
lngID = Cells(lngRow, 1).Value
SM = Cells(1, 1).Value
'sSQL = "SELECT * FROM Base WHERE [P-O#] = " & lngID
'sSQL = "SELECT * FROM Base WHERE (((Base.[P-O#])=" & lngID & "))"
sSQL = "SELECT * FROM tblProjection2012 WHERE (((tblProjection2012.[Salesman])=" & "'" & SM & "'" & " AND (tblProjection2012.[ProspectName])=" & "'" & lngID & "'" & "));"
Set cnn = New ADODB.Connection
'MyConn = ThisWorkbook.Path & Application.PathSeparator & TARGET_DB
MyConn = "Provider = Microsoft.ACE.OLEDB.12.0;" & _
"Data Source = S:\Public Folder\Projections\SalesProjections.accdb"
With cnn
' .Provider = "Microsoft.Jet.OLEDB.4.0"
.Provider = "Microsoft.ACE.OLEDB.12.0"
.Open MyConn
End With
Set rst = New ADODB.Recordset
rst.CursorLocation = adUseServer
rst.Open sSQL, ActiveConnection:=cnn, _
CursorType:=adOpenKeyset, LockType:=adLockOptimistic
'Load all records from Excel to Access.
' rst(Cells(1, j).Value) = Cells(lngRow, j).Value
'Next j
With rst
.Fields("Salesman") = Cells(1, 1).Value
.Fields("ProspectName") = Cells(lngRow, 2).Value
.Fields("January") = Cells(lngRow, 3).Value
.Fields("February") = Cells(lngRow, 4).Value
.Fields("March") = Cells(lngRow, 5).Value
.Fields("April") = Cells(lngRow, 6).Value
.Fields("May") = Cells(lngRow, 7).Value
.Fields("June") = Cells(lngRow, 8).Value
.Fields("July") = Cells(lngRow, 9).Value
.Fields("August") = Cells(lngRow, 10).Value
.Fields("September") = Cells(lngRow, 11).Value
.Fields("October") = Cells(lngRow, 12).Value
.Fields("November") = Cells(lngRow, 13).Value
.Fields("December") = Cells(lngRow, 14).Value
rst.Update
End With
' Close the connection
rst.Close
cnn.Close
Set rst = Nothing
Set cnn = Nothing
lngRow = lngRow + 1
Loop
End Sub
*/
/*
Sub AlterAllRecords()
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim fld As ADODB.Field
Dim MyConn
Dim lngRow As Long
Dim lngID, LR, Upd, SM
Dim j As Long
Dim sSQL As String
Worksheets("2012 Prospects").Select
LR = Range("A" & Rows.Count).End(xlUp).Row
lngRow = 3
Do While lngRow <= LR
lngID = Cells(lngRow, 1).Value
SM = Cells(1, 1).Value
'sSQL = "SELECT * FROM Base WHERE [P-O#] = " & lngID
'sSQL = "SELECT * FROM Base WHERE (((Base.[P-O#])=" & lngID & "))"
sSQL = "SELECT * FROM tblProjection2012 WHERE (((tblProjection2012.[Salesman])=" & "'" & SM & "'" & " AND (tblProjection2012.[ProspectName])=" & "'" & lngID & "'" & "));"
Set cnn = New ADODB.Connection
'MyConn = ThisWorkbook.Path & Application.PathSeparator & TARGET_DB
MyConn = "Provider = Microsoft.ACE.OLEDB.12.0;" & _
"Data Source = S:\Public Folder\Projections\SalesProjections.accdb"
With cnn
' .Provider = "Microsoft.Jet.OLEDB.4.0"
.Provider = "Microsoft.ACE.OLEDB.12.0"
.Open MyConn
End With
Set rst = New ADODB.Recordset
rst.CursorLocation = adUseServer
rst.Open sSQL, ActiveConnection:=cnn, _
CursorType:=adOpenKeyset, LockType:=adLockOptimistic
'Load all records from Excel to Access.
' rst(Cells(1, j).Value) = Cells(lngRow, j).Value
'Next j
With rst
.Fields("Salesman") = Cells(1, 1).Value
.Fields("ProspectName") = Cells(lngRow, 2).Value
.Fields("January") = Cells(lngRow, 3).Value
.Fields("February") = Cells(lngRow, 4).Value
.Fields("March") = Cells(lngRow, 5).Value
.Fields("April") = Cells(lngRow, 6).Value
.Fields("May") = Cells(lngRow, 7).Value
.Fields("June") = Cells(lngRow, 8).Value
.Fields("July") = Cells(lngRow, 9).Value
.Fields("August") = Cells(lngRow, 10).Value
.Fields("September") = Cells(lngRow, 11).Value
.Fields("October") = Cells(lngRow, 12).Value
.Fields("November") = Cells(lngRow, 13).Value
.Fields("December") = Cells(lngRow, 14).Value
rst.Update
End With
' Close the connection
rst.Close
cnn.Close
Set rst = Nothing
Set cnn = Nothing
lngRow = lngRow + 1
Loop
End Sub
*/