The only way I know to force them to do it right is to put it in a perpetual
loop until the text box value = 7. See my post with the InputBox and the
GoTo statement. It will not let them past that point until
Len(TextBox1.Text) = 7. I inserted a suggested method below. You can play
with it to suit your purposes.
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii = vbKeyTab Then
If TextBox1 = "" Then
MsgBox "Please enter Accounting Unit Code (7 Digits)"
RETRY:
Application.SendKeys ("{BS}")
TextBox1.Activate
If Len(TextBox1.Text) <> 7 Then
MsgBox "Accounting Unit Code must contain only 7 Digits"
GoTo RETRY:
End If
Exit Sub
End If
KeyAscii = 0
TextBox3.Activate
End If
End Sub
LRay67 said:
I inserted the If Len(TextBox1.Text) <> 7 Then - below the first
TextBox1.Activate code. It does show me the message that I inserted for the
field to contain only 7 digits. But it does not stop the user from entering
more then 7 and will continue onto the next textbox. I even tried without my
code, just inserting what you gave me with the lostfocus. I would like to
keep all code pertaining to TextBox1 in the same area. Any Suggestions??
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii = vbKeyTab Then
If TextBox1 = "" Then
MsgBox "Please enter Accounting Unit Code (7 Digits)"
RETRY:
Application.SendKeys ("{BS}")
TextBox1.Activate
If Len(TextBox1.Text) <> 7 Then
MsgBox "Accounting Unit Code must contain only 7 Digits">
GoTo RETRY:
End If
Exit Sub