excelnewcomer123
New Member
- Joined
- Dec 16, 2014
- Messages
- 8
I found a code that I have edited to update access records via Excel (Code below). I have tested it and it works perfectly, however it takes 3 minutes to update 6 records (rows). Is there any way to fix this and make it much faster?
The code searches for the primary key in an excel row, and updates fields 9-12 for that uniqueID to access. Realistically, I only need rows that were edited after the latest save to be updated, but my VBA knowledge is limited and I do not know how to do this, let alone if it is possible.
thank you
The code searches for the primary key in an excel row, and updates fields 9-12 for that uniqueID to access. Realistically, I only need rows that were edited after the latest save to be updated, but my VBA knowledge is limited and I do not know how to do this, let alone if it is possible.
Code:
Sub UpdateAccess()
On Error Resume Next
Application.ScreenUpdating = False ' Prevents screen refreshing.
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
Dim j As Long
Dim sSQL As String
Const TARGET_DB = "Access Database.accdb"
LR = Range("A" & Rows.Count).End(xlUp).Row
Upd = LR - 1
lngRow = 1
Do While lngRow <= LR
lngID = Cells(lngRow, 13).Value
sSQL = "SELECT * FROM QuoteDatabase WHERE UniqueID = " & [lngID]
Set cnn = New ADODB.Connection
MyConn = ThisWorkbook.Path & Application.PathSeparator & TARGET_DB
With cnn
.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.
With rst
.Fields("Field9") = Cells(lngRow, 9).Value
.Fields("Field10") = Cells(lngRow, 10).Value
.Fields("Field11") = Cells(lngRow, 11).Value
.Fields("Field12") = Cells(lngRow, 12).Value
rst.Update
End With
' Close the connection
rst.Close
cnn.Close
Set rst = Nothing
Set cnn = Nothing
lngRow = lngRow + 1
Loop
MsgBox "You just updated " & Upd & " records"
End Sub
thank you