MrExcel Publishing
Your One Stop for Excel Tips & Solutions

Cancel


Posted by Cory on July 16, 2001 2:59 PM

Could someone tell me what's wrong with this code?:

Private Sub TextBox1_Change()
If len(TextBox1.Value) > 5 Then
MsgBox "Your entry is too long, Try again."
Cancel
End If
End Sub

It's the "cancel" part that's stopping the code, but this was taken directly from a VBA book. I was having a .SetFocus problem and it was suggested that I just cancel instead of setting the focus in order to return to the textbox in event the user entered something I didn't want them to.

Basically, if a user puts something wrong in my textbox, I want a message to pop up telling them this, then return to that textbox to fix the error. Seems simple enough, right?

Please! Thank You!!

Cory


Posted by faster on July 16, 2001 3:04 PM

Try this

Private Sub TextBox1_Change()
If Len(TextBox1.Value) > 5 Then
MsgBox "Your entry is too long, Try again."
Exit Sub
End If
End Sub