Results 1 to 9 of 9
  1. #1
    Star Lounger
    Join Date
    Nov 2003
    Location
    Germany
    Posts
    75
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Find Focus (2003)

    Hi,

    how can I find out in VBA which textbox on a userform has the focus?

    TIA

    Wolf

  2. #2
    WS Lounge VIP sdckapr's Avatar
    Join Date
    Jul 2002
    Location
    Pittsburgh, Pennsylvania, USA
    Posts
    11,225
    Thanks
    14
    Thanked 342 Times in 335 Posts

    Re: Find Focus (2003)

    What do you want to do with this? When would you run it and how would it be called?

    Steve

  3. #3
    Star Lounger
    Join Date
    Nov 2003
    Location
    Germany
    Posts
    75
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Find Focus (2003)

    I just want to read the value of the textbox which has the focus.

    Wolf

  4. #4
    WS Lounge VIP sdckapr's Avatar
    Join Date
    Jul 2002
    Location
    Pittsburgh, Pennsylvania, USA
    Posts
    11,225
    Thanks
    14
    Thanked 342 Times in 335 Posts

    Re: Find Focus (2003)

    I am confused, When the userform is running it can be seen. It will either have a value you enter and put into a specific textbox, or if someone else is running it, you won't have access to it to run the code.

    Typically with a userform, you would read the specific textboxes and "react" as particular ones are changed

    What are you trying to accomplish? How do you envision calling this code you want run? If you put a button on the form to read what has the focus, you will learn that the button you pressed has the focus.

    Steve

  5. #5
    Star Lounger
    Join Date
    Nov 2003
    Location
    Germany
    Posts
    75
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Find Focus (2003)

    <<I am confused,>>
    Hmm.

    <<It will either have a value you enter and put into a specific textbox, or if someone else is running it, you won't have access to it to run the code.Typically with a userform, you would read the specific textboxes and "react" as particular ones are changed>>
    This is certainly not all you can do with a userform and textboxes.


    <<If you put a button on the form to read what has the focus, you will learn that the button you pressed has the focus.>>
    Not always true. It can be avoided.

    In the meantime I found the answer

    It's
    <Userform>.Activecontrol

    Thanks

    Wolf

  6. #6
    WS Lounge VIP sdckapr's Avatar
    Join Date
    Jul 2002
    Location
    Pittsburgh, Pennsylvania, USA
    Posts
    11,225
    Thanks
    14
    Thanked 342 Times in 335 Posts

    Re: Find Focus (2003)

    I am glad you found what you are after. I was only trying to understand what you were trying to do. I know there are many other things that can be done with a userform and its objects.

    The activecontrol is not always a textbox so you must be careful in using it to avoid runtime error if you look at protperties that other controls do not have.

    Steve

  7. #7
    Star Lounger
    Join Date
    Nov 2003
    Location
    Germany
    Posts
    75
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Find Focus (2003)

    Hi Steve,

    thanks for the answer.
    I was actually doing something like in the attached file (select a box in the userform and click "go")
    Without knowing userform.activecontrol this is not possible I think. I was completely lost because I was searching for something like "textbox.hasfocus", that is why I posted.
    I guess my question was just too simple. Sometimes it is difficult to ask the right question when you do not know the answer. <g>.

    <<The activecontrol is not always a textbox so you must be careful in using it to avoid runtime error if you look at properties that other controls do not have.>>
    Thanks for reminding me. I had one button on the userform which was not excluded.

    BTW: I am using this all for quite a complex budgeting system

    Wolf

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

    Re: Find Focus (2003)

    You can check this way:

    If TypeName(Me.ActiveControl) = "TextBox" Then
    ...
    End If

    or

    If TypeOf Me.ActiveControl Is MSForms.TextBox Then
    ...
    End If

  9. #9
    Star Lounger
    Join Date
    Nov 2003
    Location
    Germany
    Posts
    75
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Find Focus (2003)

    Thanks Hans,

    that helps

    Wolf

Posting Permissions

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