In many client/server applications, optimistic updates occur on a record-by-record basis. This usually happens with the following series of events:
Although this process works well for many applications, it is often more efficient to have the user edit multiple records that are cached locally and then submit these records to the server in a single batch for updating. This process is called batch optimistic updating. Note that this differs from performing transactions with the BeginTrans and CommitTrans methods. When using the BeginTrans and CommitTrans methods, data is sent to the server to be cached every time you use the AddNew or Update methods within the transaction. Then, data is written to the server database from the server’s cache when you use the CommitTrans method to commit the transaction.
Û To use batch optimistic updating
Note When you don’t use the options argument, which precedes the lockedits argument, you must specify 0 for options.
Note If you attempt a batch update while a record in that Recordset object is being edited by the user, the record being edited will automatically be updated before the batch update begins.
The following example illustrates how to use batch optimistic updating.
Function RunInBatch() Dim wrk As Workspace, cnn As Connection, rst As Recordset Dim strConnect As String ' Create ODBCDirect workspace. Set wrk = DBEngine.CreateWorkspace("ODBCDirect", "Admin", "", dbUseODBC) ' Set default cursor driver to dbUseClientBatchCursor. wrk.DefaultCursorDriver = dbUseClientBatchCursor ' Create connection string. strConnect = "ODBC;DSN=Pubs;DATABASE=Pubs;UID=sa;PWD=;" ' Open connection. Set cnn = wrk.OpenConnection("", dbDriverNoPrompt, False, strConnect) ' Open recordset on connection. Set rst = _ cnn.OpenRecordset("SELECT * FROM sales", dbOpenDynaset, 0, dbOptimisticBatch) ' Change all records in local recordset. With rst Do Until .EOF .Edit !qty = !qty + 1 .Update .MoveNext Loop ' Update all records in data source. .Update dbUpdateBatch End With End Function
If multiple records have been edited locally, and you want to update the current record before you perform the batch update, you can call the Update method and specify the dbUpdateCurrentRecord constant for the type argument. This writes the current record to the data source without writing any other batch updates. This is illustrated in the following example.
' Edit and update first record. ' Only first record is written back to data source. With rst .MoveFirst .Edit !qty = !qty + 2 .Update dbUpdateCurrentRecord ' Update remaining records in data source. .Update dbUpdateBatch End With