View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Auric__ Auric__ is offline
external usenet poster
 
Posts: 538
Default how to error-check text box values

Michael Soyka wrote:

I have a UserForm with multiple Text Boxes and a "Run" command button.
The Text Box values can only be integers. I would prefer to check for
non-integer values as they are being typed as opposed to validating them
in the "Run" button's "click" handler.

To this end, I've setup "Change" event handlers for the Text Boxes that
do the validation and display a MsgBox for illegal entries. Although
this works as desired, the user is under no obligation to fix the entry-
leaving it unchanged will not trigger a "Change" event. As a result,
the "run" button could try to process bad data and an unhandled
exception might occur.

I suppose I could just disable the "Run" button if any Text Box contains
uncorrected bad data but I'm thinking there is a better design. Any
suggestions will be welcome.


Add this to your userform:

Private Function verify(what As MSForms.ReturnInteger) As Integer
Select Case what
Case Asc("0") To Asc("9")
verify = what
Case Else
verify = 0
End Select
End Function

Then add this line to the KeyPress event (e.g. TextBox1_KeyPress) of each
textbox that needs limiting:

KeyAscii = verify(KeyAscii)

--
Some questions have no answers.