Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleUse the DBGrid control's BeforeUpdate event
KeywordsDBGrid, BeforeUpdate, validation
CategoriesControls, Database
 
Use the DBGrid's BeforeUpdate event to validate changes before they are accepted.

If you cancel a change, watch for the event canceled error in the Error event handler and ignore it so the user does not see a system generated message box.

 
' Make sure that the EmployeeId field is
' a valid long integer.
Private Sub DBGrid1_BeforeUpdate(Cancel As Integer)
Dim test_value As Long

    If DBGrid1.Col = 2 Then
        On Error GoTo NotNumeric
        test_value = CInt(DBGrid1.Text)
        On Error GoTo 0
    End If
    
    ' It's Ok. Leave Cancel alone (false).
    Exit Sub

NotNumeric:
    ' Invalid value. Cancel the update.
    Cancel = True
    
    ' Display an error message.
    MsgBox "EmployeeId must be an integer."
End Sub

' Ignore canceled update errors.
Private Sub DBGrid1_Error(ByVal DataError As Integer, _
    Response As Integer)
    If DataError = 16389 Then Response = vbDataErrContinue
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated