Avatar of FaheemAhmadGul
FaheemAhmadGul
Flag for United Kingdom of Great Britain and Northern Ireland asked on

How to restrict a VBA text box to accept letters only

I have textBox on a VBA form named txtAnswer

I would like to restrict what can be entered in a VBA TextBox (txtAnswer) to letters only.
If the user presses a number key the number will not be typed into the TextBox but instead a Message Box will pop up showing the number key that has been pressed. Also if the user presses the Enter Key a Message Box should pop up saying “You pressed Enter Key”

So if the user starts typing “ a p p p l e  “ then the word “apple” will get typed into txtAnswer, but if the user presses a number key such 3 then the text in the txtAnswer will remain unchanged but a message box will pop saying “You pressed 3 ‘“
Thank you for your help.
Microsoft OfficeMicrosoft AccessVBAMicrosoft ExcelMicrosoft Word

Avatar of undefined
Last Comment
Roy Cox

8/22/2022 - Mon
SOLUTION
Roy Cox

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
John Tsioumpris

You can use the onChange event
ASKER CERTIFIED SOLUTION
Subodh Tiwari (Neeraj)

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
FaheemAhmadGul

ASKER
Thank you all experts for taking the time to answer my questions. Both Neeraj’s and Roy’s solution were very helpful and therefore I am accepting both.
Subodh Tiwari (Neeraj)

You're welcome Faheem! Glad we could help.
Thanks for the feedback!
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Roy Cox

Pleased to help