Results 1 to 5 of 5
  1. #1
    3 Star Lounger
    Join Date
    Apr 2001
    Location
    Levin, Manawatu-Wanganui, New Zealand
    Posts
    324
    Thanks
    9
    Thanked 28 Times in 26 Posts

    .setfocus not focusing (Word 2k)

    Hi folks

    all I am trying to do is to check that first & second names are entered and separated by a space
    ----------------------------------------------------
    Private Sub tbname_afterUpdate()
    If InStr(tbName.Text, Chr(32)) = 0 Then
    MsgBox ("Enter both christian & surname separated by a space")
    frmProfile.tbName.SetFocus
    End If
    frmProfile.tbName.SetFocus
    End Sub
    ------------------------------------------------------


    If no space then the tbName should receive the focus again.
    Can someone please explain why this is not happening

    The cursor momentarily flashes back to tbName but then the tab order seems to kick in and redirects it on its merry way
    Regards
    Geof

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: .setfocus not focusing (Word 2k)

    May I assume that your event procedure is triggered by the user trying to exit the field? I think that unless you cancel that navigation, it still will occur. But you will note that AfterUpdate does not let you cancel the Exit.

    Based on a brief experiment, the events for a TextBox seem to fire in this order when modifying the contents and then exiting (either with Tab or mouse):

    Change
    BeforeUpdate
    AfterUpdate
    Exit

    Thus, it seems to make the most sense to use the Exit event to conduct your test. However, BeforeUpdate also offers a Cancel event. Not sure if it has advantages.

  3. #3
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: .setfocus not focusing (Word 2k)

    P.S. You might want to work around the possibility of a leading or trailing space (or just a space), by using Trim(value) in your comparison.

  4. #4
    3 Star Lounger
    Join Date
    Apr 2001
    Location
    Levin, Manawatu-Wanganui, New Zealand
    Posts
    324
    Thanks
    9
    Thanked 28 Times in 26 Posts

    Re: .setfocus not focusing (Word 2k)

    Hi Jefferson
    Thanks for the thoughts.
    I got around the problem by including the validation check on the OK click.
    By putting it early in the routine I was able to test and if necessary reset focus and exit sub. leaving the form open.

    The different events I tried on the tbName produced identical results. Is there a funny here or just bad practice to validate on the fly in VBA.
    Many thanks

    Geof

  5. #5
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: .setfocus not focusing (Word 2k)

    I doubt there is a rule (Howard will correct me if I am wrong) that you must choose certain events at certain times. I think you're best off to be praagmatic and use the one that works the best under the circumstances. Regarding using the OK button, that's fine with me. In fact, it might be less annoying to the user than validating on exit, as the person might want to edit something else before putting in the name, and forcing them to do it in a particular order can seem needlessly picky. <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

Posting Permissions

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