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 "The car number you have entered is too short", vbInformation
SendKeys "{Home}"
Case 8
strLeft = Left(strRailCar, 2)
strRight = Right(strRailCar, 6)
If IsNumeric(strLeft) Or Not IsNumeric(strRight) Then
MsgBox strMsg, vbInformation
SendKeys "{Home}"
End If
Case 9
strLeft = Left(strRailCar, 3)
strRight = Right(strRailCar, 6)
If IsNumeric(strLeft) Or Not IsNumeric(strRight) Then
MsgBox strMsg, vbInformation
SendKeys "{Home}"
End If
Case 10
strLeft = Left(strRailCar, 4)
strRight = Right(strRailCar, 6)
If IsNumeric(strLeft) Or Not IsNumeric(strRight) Then
MsgBox strMsg, vbInformation
SendKeys "{Home}"
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.
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 "The car number you have entered is too short", vbInformation
SendKeys "{Home}"
Case 8
strLeft = Left(strRailCar, 2)
strRight = Right(strRailCar, 6)
If IsNumeric(strLeft) Or Not IsNumeric(strRight) Then
MsgBox strMsg, vbInformation
SendKeys "{Home}"
End If
Case 9
strLeft = Left(strRailCar, 3)
strRight = Right(strRailCar, 6)
If IsNumeric(strLeft) Or Not IsNumeric(strRight) Then
MsgBox strMsg, vbInformation
SendKeys "{Home}"
End If
Case 10
strLeft = Left(strRailCar, 4)
strRight = Right(strRailCar, 6)
If IsNumeric(strLeft) Or Not IsNumeric(strRight) Then
MsgBox strMsg, vbInformation
SendKeys "{Home}"
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.