• .setfocus not focusing (Word 2k)

    Author
    Topic
    #388432

    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

    Viewing 1 reply thread
    Author
    Replies
    • #681907

      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.

    • #681911

      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.

      • #681998

        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

        • #682382

          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. smile

    Viewing 1 reply thread
    Reply To: .setfocus not focusing (Word 2k)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: