INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

VBA Visual Basic for Applications (Microsoft) FAQ

VBA How To

Restrict data entry in userform text boxes by taupirho
Posted: 10 Jun 04 (Edited 19 May 05)


Sometimes it's useful to be able to restrict the entry of certain characters into your Userform text boxes. For instance to allow numeric characters only, restrict to uppercase etcĂ  Here's how you do it.

a) Allow uppercase letters only.

Private Sub textbox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
'
' allow uppercase letters only
'
If KeyAscii >= 97 And KeyAscii <= 122 Then
KeyAscii = KeyAscii - 32
End If

End Sub


b) Disallow all normal input (still allows special keys such as delete, CTRL-C, CTRL-V)

Private Sub textbox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

KeyAscii = 0

End Sub

c) Disallow all key input

Private Sub textbox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)

KeyCode = 0

End Sub

d) Allow numeric characters only

Private Sub textbox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
'
'
If KeyAscii < 48 or KeyAscii > 57 Then
KeyAscii = 0
End If

Note that copying and pasting text into a textbox circumvents the above checks so you should also add additional checking code on for example the change event for the textbox.

Back to VBA Visual Basic for Applications (Microsoft) FAQ Index
Back to VBA Visual Basic for Applications (Microsoft) Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close