Runtime error 3021

vavs

Well-known Member
Joined
Jul 2, 2004
Messages
514
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

*/
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple

Forum statistics

Threads
1,203,502
Messages
6,055,772
Members
444,822
Latest member
Hombre

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top