Closed Thread
Results 1 to 6 of 6

UserForm TextBox validation and focus

  1. #1
    Indiana Epilepsy and Child Neurology
    Guest

    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.

  2. #2
    Chip Pearson
    Guest

    Re: UserForm TextBox validation and focus

    There really isn't a good solution to this. In general, I do all
    my controls validation when the user hits the OK/Submit button,
    and return focus to offending control if an error is found. The
    Exit event is near worthless. Since you don't know the control to
    which focus will be transferred, you can "exempt" certain
    controls.

    --
    Cordially,
    Chip Pearson
    Microsoft MVP - Excel
    Pearson Software Consulting, LLC
    www.cpearson.com





    "Indiana Epilepsy and Child Neurology"
    <BrainNoSpamChild@SkylerNoSpam.com> wrote in message
    news:ht5ta2ppf3m3vatdcavgm95926643p3k7p@4ax.com...
    > 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.




  3. #3
    Chip Pearson
    Guest

    Re: UserForm TextBox validation and focus

    > you can "exempt" certain
    should be
    you can't "exempt" certain


    --
    Cordially,
    Chip Pearson
    Microsoft MVP - Excel
    Pearson Software Consulting, LLC
    www.cpearson.com


    "Chip Pearson" <chip@cpearson.com> wrote in message
    news:%23AGb9NfoGHA.148@TK2MSFTNGP04.phx.gbl...
    > There really isn't a good solution to this. In general, I do
    > all my controls validation when the user hits the OK/Submit
    > button, and return focus to offending control if an error is
    > found. The Exit event is near worthless. Since you don't know
    > the control to which focus will be transferred, you can
    > "exempt" certain controls.
    >
    > --
    > Cordially,
    > Chip Pearson
    > Microsoft MVP - Excel
    > Pearson Software Consulting, LLC
    > www.cpearson.com
    >
    >
    >
    >
    >
    > "Indiana Epilepsy and Child Neurology"
    > <BrainNoSpamChild@SkylerNoSpam.com> wrote in message
    > news:ht5ta2ppf3m3vatdcavgm95926643p3k7p@4ax.com...
    >> 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.

    >
    >




  4. #4
    Registered User
    Join Date
    11-12-2011
    Location
    Mooloolaba,Australia
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: UserForm TextBox validation and focus

    I have a user form that has five object buttons, objectbutton 1 to 5, in a frame. The form also has text boxes and control buttons. My problem is that I do not know how to ensure that one of the object buttons is selected. I assume that code is able to detect the missing information when the control button is clicked. I would then like a message saying that an object button has not been selected and the form to wait until one is selected. Please give me a start.
    Secondly, I would like the initialize code to not focus on one text box. My code is

    Sub UserForm_Initialize()

    Dim ctl
    For Each ctl In Me.Controls
    If TypeOf ctl Is msforms.TextBox Then
    ctl.Text = ""
    End If
    Next ctl

    End Sub

    This code clears the text boxes but leaves the cursor in tb1

    Thanks for your assistance
    Peter

  5. #5
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: UserForm TextBox validation and focus

    Your post does not comply with Rule 2 of our Forum RULES. Don't post a question in the thread of another member -- start your own thread. If you feel it's particularly relevant, provide a link to the other thread.
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

  6. #6
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: UserForm TextBox validation and focus

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1