Run Time Interaction > Navigation and Scrolling > Restricting cell navigation |
You can always use the BeforeRowColChange event to prevent the user from moving to a different cell, regardless of whether the current cell is modified. Set the Cancel argument to True to keep another cell from becoming current.
If the current cell has been modified, you can use the BeforeColUpdate event to examine its value before moving to another grid cell. If the value entered is invalid, you can set the Cancel argument to True to prevent the current cell from changing, and optionally beep or display an error message for the user. The BeforeColUpdate event provides a flexible way to validate user input and restrict cell navigation.
Example Title |
Copy Code
|
---|---|
Private Sub TDBGrid1_BeforeColUpdate( _ ByVal ColIndex As Integer, _ OldValue As Variant, _ Cancel As Integer) Dim CharCode As Integer If ColIndex = 1 Then ' Data in Column 1 must start with upper case. CharCode = Asc(TDBGrid1.Columns(1).Text) If CharCode > 64 And CharCode < 91 Then Exit Sub ' Display warning message for user. MsgBox "Last name must start with upper case" ' Data validation fails, prohibit user from moving to another cell. Cancel = True End If End Sub |