PRB: CancelUpdate Does Not Cancel ActiveX Data Objects Update (270118)



The information in this article applies to:

  • ActiveX Data Objects (ADO) 2.5
  • ActiveX Data Objects (ADO) 2.6
  • ActiveX Data Objects (ADO) 2.7
  • Microsoft Visual Basic Professional Edition for Windows 6.0
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0

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

  1. Create a new Visual Basic Data Project, and then add a Project Reference to the ADO 2.5 library.
  2. On the main form, add two TextBox controls, add an ADO Data Control (ADODC), and then add two CommandButton controls.
  3. 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.
  4. Label one CommandButton "Cancel" and the other CommandButton "Copy."
  5. Add the following code to the Click event handler for the "Cancel" Click event handler:
    Adodc1.recordset.CancelUpdate
    Adodc1.recordset.MoveNext
    Adodc1.recordset.MovePrevious
    					
  6. Add the following code to the Click event handler for the "Copy" CommandButton:
    Text1 = Text2
    ' Text1.SetFocus       ' Uncomment this line to workaround the problem
    					
  7. Set the RecordSource property of Adodc1 to "SELECT * FROM Employees."
  8. Set the DataSource property of Text1 to Adodc1, and then set the DataField property to the LastName field.
  9. Run the project. You should see "Davolio" in the bound TextBox, and "text2" in the unbound TextBox.
  10. Make a change to the name, and then click Cancel. The name "Davolio" should now re-appear.
  11. Click Copy, and then verify that the value in Text1 is replaced with the string "Text2."
  12. Click Cancel.
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.

Modification Type:MajorLast Reviewed:8/23/2001
Keywords:kbprb KB270118