JumboCactuar
Well-known Member
- Joined
- Nov 16, 2016
- Messages
- 788
- Office Version
- 365
- Platform
- Windows
Hi,
the code i have below works fine -
Updating multiple records from spreadsheet data where the ID (Column A) matches the record
But it can be slow with a lot of records, which is understandable opening and closing the recordset each time
Is there a better way to achieve this?
Thanks
the code i have below works fine -
Updating multiple records from spreadsheet data where the ID (Column A) matches the record
But it can be slow with a lot of records, which is understandable opening and closing the recordset each time
Is there a better way to achieve this?
VBA Code:
Sub UpdateRecords()
Dim cnn As ADODB.Connection
Dim MyConn
Dim rs As ADODB.Recordset
Dim i As Long, j As Long
Dim Rw As Long
Dim sSQL As String
Rw = Range("A10000").End(xlUp).Row
Set cnn = New ADODB.Connection
cnn.Open (ConnectionString)
Set rst = New ADODB.Recordset
rst.CursorLocation = adUseServer
'Update one field in each record of the table. First record is in Row 2.
For i = 9 To Rw
sSQL = "SELECT * FROM MYTABLE WHERE MY_ID =" & Cells(i, 1).Value
rst.Open Source:=sSQL, _
ActiveConnection:=cnn, _
CursorType:=adOpenKeyset, _
LockType:=adLockOptimistic
rst("DB_STATUS") = "Record Changed"
rst("DB_CDATE") = CDbl(Format(Date, "YYYYMMDD"))
rst("DB_CTIME") = CDbl(Format(Now, "HHMMSS"))
rst.Update
rst.Close
Next i
' Close the connection
cnn.Close
Set rst = Nothing
Set cnn = Nothing
End Sub
Thanks