Hi btmtdk,
Excellent question.
When checking for a string you might want to:
a. Eliminate leading and trailing blanks if they are not allowed
b. Flag numbers if they are not allowed
c. Flag numbers if they are not allowed in a certain position in the string
d. Flag alpha text if not allowed in a certain position in the string
e. Flag other characters if not allowed (e.g. %, $, #, etc.)
When checking for a number:
a. Make sure the field is not blank (if blank not allowed)
b. Make sure the value is numeric
c. Flag the value if it is not within the expected range
There are many ways to process the value, including
a. With each keystroke
b. When the TextBox loses focus
c. When user selects a CommandButton to process all data on the UserForm
My personal preference is to do CommandButton driven processing. Any TextBox that has a bad value is color coded appropriately.
Even though TextBoxes always return strings, I like to access the .Text attribute rather than the .Value attribute. I do this because when I access data in spreadsheets, I use analogous code.
There is a little know attribute for UserForm controls called the .Tag attribute. It can be used to do anything you want. I like to use it to keep the current value. When the command button is activated, I can then compare all TextBox values to their previous values to see if any value has changed.
Here is typical code I would use:
Lewis
Bookmarks