Data validation - Mouse / key press

  • Thread starter Thread starter Bill nguyen
  • Start date Start date
B

Bill nguyen

I use the sub below to detect data entry error. The problem is that users
are still able to move out of the textbox AFTER the sub is run (and error
msg popped up) without having to correct the invalid data. In order to trap
all possible movements, I have to run the sub in Key events and Mouse
events. This is a lot of work.

I guess there must be a way to catch all key/mouse in and out the text box
so that we can run validation once and still be able to force the user to
supply valid data before leaving the textbox.

Any help is greatly appreciated.

Bill

--------------------

Private Sub txtBgross1_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles txtBgross1.TextChanged

Dim bValid As Boolean

bValid = ValidateDataEntry(1, Trim(txtBgross1.Text), "BOL Gross")

If bValid = False Then

txtBgross1.Focus()

End If

End Sub



Private Function ValidateDataEntry(ByVal entryType As Integer, _

ByVal entryVal As String, _

ByVal entrySource As String) As Boolean

Dim bAnsw As Boolean = True

Select Case entryType

Case 1

If IsNumeric(entryVal) = False Then

Beep()

MsgBox(entrySource & " must be numeric!", MsgBoxStyle.OKOnly, "Data Entry
Validation")

bAnsw = False

End If

End Select

Return bAnsw

End Function
 
Bill,

One option is to perform input validation in a textbox’s Validating event.

When the user changes the focus from a textbox (or other control) the
textbox’s Validating event occurs.

In the Validating event you can write code to validate the data in the
textbox. If the user has entered invalid data you can inform the user and
cancel the Validating event.

Canceling the textbox’s Validating event prevents the focus from leaving the
textbox. This forces the user to enter good data into the textbox in order to
continue.

Kerry Moorman
 
Back
Top