Question

I have the following update statements which are execute via VBA. It seems to be sub-optimal to have multiple update statements. How do i consolidate these into a single statement? The statements update an Access database.

strSqlLoc = "UPDATE table1 AS type SET type.Value='" & Range("C" & i).Value & "' WHERE PropertyID=" & Range("B" & i).Value & ";"
strSqlEnv = "UPDATE table1 AS type SET type.Value='" & Range("E" & i).Value & "' WHERE PropertyID=" & Range("D" & i).Value & ";"
strSqlClass = "UPDATE table1 AS type SET type.Value='" & Range("G" & i).Value & "' WHERE PropertyID=" & Range("F" & i).Value & ";"

Set rs = cn.Execute(strSqlLoc)
Set rs = cn.Execute(strSqlEnv)
Set rs = cn.Execute(strSqlClass)
Was it helpful?

Solution

Another Update

For multiple row updates it looks like Batch Update could do the trick.

Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset

Dim strSQL As String
Dim lngUpdated As Long

rst.ActiveConnection = CurrentProject.Connection
rst.CursorType = adOpenKeyset
rst.CursorLocation = adUseClient
rst.LockType = adLockBatchOptimistic
rst.Open ("Select * from table1 ")

lngUpdated = 1

rst.Find "PropertyID=" & Range("B1")

Do Until rst.EOF
    rst("Value") = Range("C" & lngUpdated)
    lngUpdated = lngUpdated + 1
    rst.Find "PropertyID=" & Range("B" & lngUpdated), 1, adSearchForward
Loop

// Try repeating above for the other two columns?

rst.UpdateBatch

rst.Close
Set rst = Nothing

Can't really get much better than this, unless the data you are updating is already somewhere in the DB. You could try the case statement technique described here

Update:

I think my intuition behind that batch upload thought was that if you can somehow get the data on the server side in one shot, you could then join on it and update another table in one update statement. if you are only updating three values - look into creating a query on access side and passing in your six parameters in a single call, then use the technique described here inside the query

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top