HOWTO: Control Your Updates in ADO Via "Update Criteria"ID: Q190727
|
The ADO Client Cursor Engine allows you to control how it builds the action queries that update the database according to the changes you make to the recordset object. This article is designed to help you understand how to control how ADO performs these updates.
When you open a recordset against the Customers table in the Northwind
database (NWind.MDB) and use a client side cursor, ADO retrieves enough
information about the structure of the table in order to use an action
query to update the table.
An action query is a query that modifies a database and does not return
data. For example, "UPDATE Customers SET CompanyName = 'Acme' WHERE
CustomerID = 17" is an action query.
ADO determines which field, or set of fields, is the primary key and uses
that information to make sure it can find the correct row in the database
to update. If you are going to perform updates with the client cursor
engine, make sure you have a primary key defined in your table. If you
don't, you may accidentally update more rows than you intended.
When you use a client side recordset, ADO exposes a property in the
recordset's Properties collection called "Update Criteria." This property
allows you to control the information in the WHERE clause in the action
query that ADO builds to update the database. The default value for this
property is 2 - adCriteriaUpdCols. By default, ADO will use the primary key
and all fields being updated in the WHERE clause of the action query. For
example:
rsCustomers.CursorLocation = adUseClient
rsCustomers.Open "SELECT * FROM Customers", cnNWind, _
adOpenStatic, adLockOptimistic, adCmdText
rsCustomers.Fields("CompanyName").Value = "Acme"
rsCustomers.Update
UPDATE Customers SET CompanyName = 'Acme'
WHERE CustomerID = 'ALFKI' AND CompanyName = 'Alfreds Futterkiste'
rsCustomers.CursorLocation = adUseClient
rsCustomers.Properties("Update Criteria").Value = adCriteriaAllCols
rsCustomers.Open "SELECT * FROM Customers", cnNWind, _
adOpenStatic, adLockOptimistic, adCmdText
rsCustomers.Fields("CompanyName").Value = "Acme"
rsCustomers.Update
adCriteriaKey = 0
Uses only the primary key
adCriteriaAllCols = 1
Uses all columns in the recordset
adCriteriaUpdCols = 2 (Default)
Uses only the columns in the recordset that have been modified
adCriteriaTimeStamp = 3
Uses the timestamp column (if available) in the recordset
Additional query words: kbdse kbDSupport kbVBp kbVBp500 kbVBp600 kbADO200 kbADO
Keywords :
Version :
Platform : WINDOWS
Issue type : kbhowto
Last Reviewed: May 20, 1999