Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Data Entry Validation on a form

Status
Not open for further replies.

fchan

MIS
Jul 2, 2001
47
US
Hi I'm trying to write some code to validate user input in a text box on my form. The text box accepts up to 10 characters and the values can be a string of two letters and six numbers (AA1223456) or a string of three letters and six numbers (AAA123456) or a string of four letters and size numbers (AAAA123456).

I only want to allow the user to enter one of the three possibilities above. I tried to use an input mask, but that is too restrictive. I wrote the following sub procedure to validate the user input, but it is not completely full-proof. A user can enter three letters and seven numbers and they will not get the error message.

*********
Private Sub railCar_AfterUpdate()

Dim strRailCar, strLeft, strRight, strMsg As String
Dim iStrLen As Integer
strRailCar = Trim(Me.RailCarNumber)
iStrLen = Len(strRailCar)
strMsg = "You have entered an invalid car number."

Select Case iStrLen
Case Is < 8
MsgBox &quot;The car number you have entered is too short&quot;, vbInformation
SendKeys &quot;{Home}&quot;
Case 8
strLeft = Left(strRailCar, 2)
strRight = Right(strRailCar, 6)

If IsNumeric(strLeft) Or Not IsNumeric(strRight) Then
MsgBox strMsg, vbInformation
SendKeys &quot;{Home}&quot;
End If
Case 9
strLeft = Left(strRailCar, 3)
strRight = Right(strRailCar, 6)
If IsNumeric(strLeft) Or Not IsNumeric(strRight) Then
MsgBox strMsg, vbInformation
SendKeys &quot;{Home}&quot;
End If
Case 10
strLeft = Left(strRailCar, 4)
strRight = Right(strRailCar, 6)
If IsNumeric(strLeft) Or Not IsNumeric(strRight) Then
MsgBox strMsg, vbInformation
SendKeys &quot;{Home}&quot;
End If

End Select

End Sub
****************

Does anyone know if VB has a similar object to the regular expression (RegExp) object in JavaScript? Or is there any suggestions on how I can full-proof my code above?

Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top