Microsoft KB Archive/270118

= PRB: CancelUpdate Does Not Cancel ActiveX Data Objects Update =

Article ID: 270118

Article Last Modified on 8/23/2001

-

APPLIES TO


 * Microsoft ActiveX Data Objects 2.5
 * Microsoft ActiveX Data Objects 2.6
 * Microsoft ActiveX Data Objects 2.7
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



This article was previously published under Q270118



SYMPTOMS
When the value of a bound TextBox control is set through code and calls the ActiveX Data Objects (ADO) CancelUpdate method, it does not cause the update to be cancelled (for example, the changes are reflected in the database).



CAUSE
This behavior occurs because when the TextBox control value is set through code, the actual update to the database does not occur until MoveNext is called for the ADO recordset that the TextBox is bound to. The CancelUpdate call has no effect because there is not an update that can be cancelled.



RESOLUTION
To cause an update to occur before the MoveNext call, add code to call the SetFocus method of the TextBox immediately after the line of code that updates its value. After you do this, the control goes through a validation process when it loses focus. The validate code that runs under the covers causes an update to occur and the CancelUpdate has the effect of canceling that update.



MORE INFORMATION
By default, when the TextBox value is set in code, the update does not occur until a MoveNext is performed. Therefore, the CancelUpdate has no effect. If you execute the MoveNext prior to calling the CancelUpdate, then update is cancelled.

Steps to Reproduce the Problem
 Create a new Visual Basic Data Project, and then add a Project Reference to the ADO 2.5 library. On the main form, add two TextBox controls, add an ADO Data Control (ADODC), and then add two CommandButton controls. In the properties of the ADO Data Control, build a connection string that uses the Microsoft Jet 4.0 OLE DB Provider to connect to the sample Northwind database (Nwind.mdb) that is located in the Microsoft Visual Basic 98 folder. Label one CommandButton &quot;Cancel&quot; and the other CommandButton &quot;Copy.&quot;  Add the following code to the Click event handler for the &quot;Cancel&quot; Click event handler: Adodc1.recordset.CancelUpdate Adodc1.recordset.MoveNext Adodc1.recordset.MovePrevious   Add the following code to the Click event handler for the &quot;Copy&quot; CommandButton: Text1 = Text2 ' Text1.SetFocus      ' Uncomment this line to workaround the problem </li> Set the RecordSource property of Adodc1 to &quot;SELECT * FROM Employees.&quot;</li> Set the DataSource property of Text1 to Adodc1, and then set the DataField property to the LastName field.</li> Run the project. You should see &quot;Davolio&quot; in the bound TextBox, and &quot;text2&quot; in the unbound TextBox.</li> Make a change to the name, and then click Cancel. The name &quot;Davolio&quot; should now re-appear.</li> Click Copy, and then verify that the value in Text1 is replaced with the string &quot;Text2.&quot;</li> Click Cancel.</li></ol>

To prevent the behavior, uncomment the line of code in step 6 that calls the SetFocus method on Text1.

NOTE: The previous example uses the Microsoft Jet 4.0 OLE DB Provider, but the behavior is not dependent on use of the OLE DB Provider. The same behavior can occur with other providers.

Additional query words: cancel canceled programmatically

Keywords: kbprb KB270118

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.