LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default UserForm TextBox validation and focus

My googling shows this issue has been discussed a couple times but I
haven't found any real solutions.

Sometimes I need the value of a TextBox to be checked when the focus
moves away from the TextBox to another control on the UserForm, except
for certain specific other controls. Examples of those "exceptional"
controls might be a command button which discards the UserForm
contents, or one which sets the TextBox value to a known valid value;
however, the "exceptional" control may not be a CommandButton, so
TakeFocusOnClick=False doesn't solve the problem (and produces unusual
UI behavior which the user may not expect, which I consider
undesirable).

If the TextBox value does get checked and is invalid, I need to return
the focus to the TextBox (possibly, but not necessarily, after
displaying a message or sounding an alarm). If the TextBox value gets
checked and is valid, or if the "target" control is one of the
"exceptional" controls so the value does not get checked, the focus
change to the "target" control should be allowed.

The TextBox Exit and BeforeUpdate events allow the focus change to be
aborted by setting the Cancel parameter to True, but there seems to be
no way to tell what control is being moved to during those events.
ActiveControl still refers to the TextBox, and the tab order does not
help because a mouse click won't necessarily be to the next or
previous control in the tab order.

The Enter event of the target control gave me some hope. With some
work I was able to defer checking the TextBox value until the Enter
event, where I could decide whether to skip validation. The Enter
event doesn't have a Cancel parameter, so if the validation fails I
tried invoking the SetFocus method of the TextBox. Single-stepping
through the code shows it works, except that when the Enter event
subroutine finishes, the focus is set to the new control regardless of
what happens inside the handler!

In short, the problem is that before the Enter event I can't tell
where the focus is going so I can't tell whether to validate or not,
and in the Enter event I can't prevent the focus change if validation
fails. By the time I can do one part of my validation logic, I can't
do another part of it. For my purposes I can't wait until an entire
form gets closed to validate, nor can I prevent invalid entries via
the TextBox Change event.

For a CommandButton I could conceivably do something in the Click or
MouseDown event; coupled with some kind of contortion with the TextBox
KeyDown event I might be able to catch moving off the TextBox via
keyboard (using the tab order to predict the target control) and
mouse, by using two entirely different validation systems. Other
controls don't have a Click event, but usually do have a MouseDown. I
haven't explored this yet, because I consider this kind of code
complication to be a VERY BAD THING(tm), and I'm not even sure it will
work - the Exit event by itself, and the Enter event scheme both
looked like they would work until I actually tried them. I've even
considered some kind of timer-based solution (shudder!). If only
there were an event with a Cancel parameter, which fires after the
ActiveControl has changed! Like, an Enter(ByVal Cancel As
ReturnBoolean) event would do the trick.

Please, somebody tell me there's a reasonable solution I just haven't
come up with yet!
--
Don Stauffer, Office Manager
Indiana Epilepsy and Child Neurology, Inc.
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Textbox Validation not retaining focus tdw Excel Programming 0 June 21st 06 05:34 PM
Retaining focus on a textbox / listbox item pair after validation = false. chatterbox Excel Programming 2 April 4th 06 11:42 AM
putting validation to TextBox in the userform salihyil[_6_] Excel Programming 1 February 26th 04 12:10 PM
putting validation to TextBox in the userform salihyil[_8_] Excel Programming 1 February 26th 04 10:59 AM
Userform Textbox in Password Validation golf4 Excel Programming 3 July 28th 03 02:27 PM


All times are GMT +1. The time now is 03:06 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"