Results 1 to 3 of 3
  1. #1
    Lounger
    Join Date
    Mar 2001
    Location
    Feltham, Middlesex, England
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Problem Setting Focus (VBA - Excel 2003)

    Hello,

    I don't have a lot of experience with User Forms, but I'm trying to set one up as part of a new process and am having a lot of trouble controlling the focus.

    I want to check the value of a text box when it is exited and, if it does not validate, inform the user with a message, blank the text box and return the focus to it for re-entry.

    I have tried using the Exit code from the text box, but everything happens as I want it to except that the focus still goes to the next control in sequence.

    I also tried setting an error variable if the entry didn't validate and testing that in the Entry code of the next control (not ideal anyway, because the user could move anywhere if it's done with the mouse) but setting focus back to the first control is ignored there also.

    I imagine this is a common requirement, but I can't get it to work. I'd appreciate any guidance anyone can give me.

    Thanks,

    Mike Jones

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Problem Setting Focus (VBA - Excel 2003)

    If the value of the text box doesn't meet requirements, set the Cancel argument of the On Exit event procedure to True. This will prevent the user from exiting the text box:

    Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    If <Me.TextBox1 is not valid> Then
    MsgBox "Please enter a correct value!", vbExclamation
    Cancel = True
    End If
    End Sub

    Replace TextBox1 with the name of the text box, and <Me.TextBox1 is not valid> with your condition to validate the text box.

  3. #3
    Lounger
    Join Date
    Mar 2001
    Location
    Feltham, Middlesex, England
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Problem Setting Focus (VBA - Excel 2003)

    Thanks, Hans.

    Just what I needed. Easy when you know how, isn't it?

    Many thanks again,

    Mike

Posting Permissions

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