Microsoft KB Archive/288346

= BUG: BeforeUpdate Event of the DataGrid Does Not Always Fire =

Article ID: 288346

Article Last Modified on 6/28/2004

-

APPLIES TO


 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition Service Pack 3
 * Microsoft Visual Basic 6.0 Enterprise Edition Service Pack 4
 * Microsoft Visual Basic 6.0 Enterprise Edition Service Pack 5

-



This article was previously published under Q288346



SYMPTOMS
The documentation for the BeforeUpdate event of the Visual Basic 6.0 DataGrid control states:

&quot;When the user moves to another row or the Recordset object's Update method is executed, data is moved from the DataGrid control's copy buffer to the Data control's copy buffer and written to the database.&quot;

In fact, the BeforeUpdate event does not fire for at least half of the methods by which a user can &quot;move to another row&quot; after editing a DataGrid cell or cells in the current row, as described later in this article.



CAUSE
The DataGrid only fires the BeforeUpdate event when the user moves from one row to another; therefore, the event does not fire if, after editing a row, the user moves off the DataGrid entirely by selecting another object on the form. In addition, if the user changes rows by clicking the Record Selector (the gray left margin), the AfterUpdate event fires first, preventing the firing of the BeforeUpdate event.



RESOLUTION
If the users of the application cannot be constrained to navigate the DataGrid by using one of the methods for which the BeforeUpdate event reliably fires, then another event or another approach must be used to validate entries or to take other action when the user moves from one row to another or moves off the grid.



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.



MORE INFORMATION
The BeforeUpdate of the Record Selector event does not fire for at least half of the methods by which a user can move off the current row after editing a cell or cells in the DataGrid.

The BeforeUpdate event fires if you edit a row in the grid, and then move off the row by:
 * using the TAB key to move to another field, and then using an ARROW key to move off the row.
 * clicking on another field, and then using an ARROW key to move off the row.
 * clicking on another row in the data (white) portion of the grid.

The BeforeUpdate event does not fire if you edit a row in the grid, and then move off the row by:
 * clicking the Record Selector (the gray left margin) of another row in the grid.
 * clicking on an unused (gray) portion of the grid background.
 * moving off the grid by clicking on another control on the form.

In all of the preceding cases, the changed data is saved to the database as expected.

Steps to Reproduce Behavior
 Create a new Standard EXE project in Visual Basic. Form1 is created by default. On the Project menu, click to select References, and then set a reference to Microsoft ActiveX Data Objects (ADO).  Copy and paste the following code into the form's code module. (As written, this code places the necessary controls on the form dynamically.) Option Explicit

Private cn As ADODB.Connection Private rs As ADODB.Recordset Private WithEvents grd As VB.VBControlExtender Private WithEvents txt As VB.TextBox Private WithEvents cmd As VB.CommandButton

Private Sub Form_Load Set cn = New ADODB.Connection cn.Open &quot;Provider=Microsoft.Jet.OLEDB.4.0;&quot; & _ &quot;Data Source=C:\Program Files\Microsoft Visual Studio\Vb98\nwind.mdb&quot; Set rs = New ADODB.Recordset rs.CursorLocation = adUseClient rs.Open &quot;SELECT TOP 6 EmployeeID, LastName, FirstName FROM Employees&quot;, cn, adOpenStatic, adLockOptimistic, adCmdText Licenses.Add &quot;MSDataGridLib.DataGrid&quot;, &quot;&quot; Set grd = Form1.Controls.Add(&quot;MSDataGridLib.DataGrid&quot;, &quot;grd&quot;) With grd .Visible = True .Top = 72 .Left = 72 .Width = Form1.Width - 288 .Height = Form1.Height - 1440 End With grd.object.WrapCellPointer = True grd.object.tabaction = 2 Set grd.object.DataSource = rs Set txt = Form1.Controls.Add(&quot;VB.Textbox&quot;, &quot;txt&quot;) With txt .Visible = True .Top = 2400 .Left = 72 .Width = 2400 .Text = &quot;Click here to move focus off grid&quot; End With Set cmd = Form1.Controls.Add(&quot;VB.CommandButton&quot;, &quot;cmd&quot;) With cmd .Visible = True .Top = 2400 .Left = 3000 .Width = 1440 .Caption = &quot;Close&quot; End With End Sub

Private Sub grd_ObjectEvent(Info As EventInfo) Select Case Info Case &quot;BeforeUpdate&quot; MsgBox &quot;BeforeUpdate event fired!&quot;, vbExclamation + vbOKOnly, &quot;BeforeUpdate Event&quot; End Select End Sub

Private Sub cmd_Click Unload Me End Sub  Adjust the database connection string, if necessary. It should work &quot;as is&quot; if Visual Basic was installed to the default location. Run the project.</li> Test each of the six methods in the &quot;More Information&quot; section for moving off the current row after editing a field, and note that only three of the six methods of moving off the changed current row fire the BeforeUpdate event.</li></ol>

Keywords: kbbug kbdatabinding kbdatabase kbmdacnosweep kbpending KB288346

-

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

© Microsoft Corporation. All rights reserved.