By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,865 Members | 1,717 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,865 IT Pros & Developers. It's quick & easy.

Data validation - Mouse / key press

P: n/a
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


Dec 24 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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
"Bill nguyen" wrote:
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


Dec 24 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.