Consulting

Results 1 to 10 of 10

Thread: Solved: Which control had focus?

  1. #1
    VBAX Regular
    Joined
    Jul 2004
    Location
    Cambridge, England
    Posts
    23
    Location

    Solved: Which control had focus?

    Hi all, help please.

    I have a UserForm with loads of controls (various types) on 2 tabs. I need a cmd_Click that will ID which control had focus last.

    I've established that I need to set the cmd TakeFocusOnClick to False, but "Range(whatever) = ActiveControl.Name" only gives me "MultiPage1", like it's in front or something. Any suggestions?

    TIA,
    Andy

  2. #2
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,710
    Location
    Multipage will be the active control regardless of what controls on the multipage are active. You can try to setup a global variable that can track the current control. Set the Enter sub for each control to change the variable to the control.

    [VBA]
    Option Explicit

    Dim Ctl As Control

    Private Sub CommandButton1_Enter()

    Set Ctl = UserForm1.CommandButton1

    End Sub

    Private Sub CommandButton2_Enter()

    Set Ctl = UserForm1.CommandButton2

    End Sub

    Private Sub CommandButton5_Enter()

    Set Ctl = UserForm1.CommandButton5

    End Sub
    [/VBA]

  3. #3
    VBAX Regular
    Joined
    May 2004
    Location
    Sydney, Australia
    Posts
    36
    Location
    Hi,

    You can get the active control on a Multipage by using something like this:-
    [VBA]
    MsgBox ActiveControl.SelectedItem.ActiveControl.Name
    [/VBA]

    HTH
    Dan

  4. #4
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,897
    Location
    Hey Dan, nice one. I have a question regarding the syntax of that one: why designate the object twice? I haven't worked with controls much, but that is just bizarre. While quite short, is there a way to bypass referencing the object multiple times for one call? Maybe you could set me straight on this one.

  5. #5
    VBAX Tutor jamescol's Avatar
    Joined
    May 2004
    Location
    Charlotte, NC
    Posts
    251
    Location
    You may need to modify Jacob's code to keep track of the last control to have focus. Otherwise I think you will end up with the current control.

    [vba]
    Option Explicit

    Dim PrevCtl As Control

    Private Sub CommandButton1_Exit()

    Set PrevCtl = UserForm1.CommandButton1

    End Sub

    Private Sub CommandButton2_Exit()

    Set PrevCtl = UserForm1.CommandButton2

    End Sub

    Private Sub CommandButton5_Exit()

    Set PrevCtl = UserForm1.CommandButton5

    End Sub
    [/vba]

    If you are planning to do anything other than display the control that last had focus, you will need additional logic to switch between multipage tabs. For instance, if page 1 is displayed, you can't set the focus to a control on page 0 with first changing to that page.

    Also, you will need to set the PrevCtl during the Enter event of the default control that has focus when you first load the form or your button won't work properly. You could also just set the PrevCtl during the Enter event, but to me the logic isn't as intuitive.

    My .02

    James
    "All that's necessary for evil to triumph is for good men to do nothing."

  6. #6
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,710
    Location
    I don't think we need to use the exit Exit sub since the command button being used will not take the focus. So the previous value we set in the Enter sub will still be in effect.

  7. #7
    VBAX Tutor jamescol's Avatar
    Joined
    May 2004
    Location
    Charlotte, NC
    Posts
    251
    Location
    Jacob,
    You may be right. I was understanding the problem statement that he wants to click a button and diplay the previous control (not counting the button). So basically an N-1 deal.

    You interpretation is that he wants the button to display the control that had the focus just before the button.

    Not sure which scenario is the one he wants. Guess we got both covered

    Cheers,
    James
    "All that's necessary for evil to triumph is for good men to do nothing."

  8. #8
    VBAX Regular
    Joined
    Jul 2004
    Location
    Cambridge, England
    Posts
    23
    Location
    Actually, Daniel Klann's "ActiveControl.SelectedItem.ActiveControl.Name" works perfectly here (sorry, XL2K on XP Home, not used to asking questions).

    firefytr - if I crop it to "ActiveControl.SelectedItem.Name" it gives me the name of the MultiPage, which makes sense within the whole layer/object hierarchy thing (multipage owns each item on it).

    Daniel, I googled for "ActiveControl.SelectedItem.ActiveControl.Name" and got one solitary hit, so all I can say is if it works for the user then very well done.



    TVM,
    Andy

  9. #9
    VBAX Regular
    Joined
    May 2004
    Location
    Sydney, Australia
    Posts
    36
    Location
    Quote Originally Posted by firefytr
    Hey Dan, nice one. I have a question regarding the syntax of that one: why designate the object twice? I haven't worked with controls much, but that is just bizarre. While quite short, is there a way to bypass referencing the object multiple times for one call? Maybe you could set me straight on this one.
    Hi Zack,

    If you break it down it's not that bizarre:-
    • If the form contains a Multipage control the Msgbox.ActiveControl.Name would return the name of the Multipage control e.g. Multipage1.
    • The Multipage control has a SelectedItem property, which returns the currently selected tab or page. BTW, I found this by looking in the object browser at the available properties and checking help to see what each one did. That is one of the best way IMO to find answers to these sort of questions.
    • The page object acts as a container for controls and has some of the same properties and methods as your bog standard userform e.g. a Controls collection and an ActiveControl property (which is what we need).

    You could use more verbose code such as this:-

    [VBA]
    Private Sub CommandButton1_Click()
    Dim Mp As MSForms.MultiPage
    Dim Pg As MSForms.Page
    Dim Ct As MSForms.Control

    'This will give a reference to a multipage contro
    Set Mp = ActiveControl
    'This will give a reference to selected page on the
    'multipage control
    Set Pg = Mp.SelectedItem

    'Now get a reference to the active control on the
    'selected page
    Set Ct = Pg.ActiveControl

    MsgBox "ActiveControl = " & Mp.Name & vbCr & _
    "Page Number = " & Pg.Name & vbCr & _
    "Page.ActiveControl = " & Ct.Name
    End Sub
    [/VBA]

    Does that make sense?

    Dan
    Last edited by Daniel Klann; 08-11-2004 at 04:17 AM. Reason: HTML list tags didn't work

  10. #10
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,897
    Location
    Thanks for that Dan! I appreciate you taking the time to explain it to me. I'm always learning and always enjoy a lesson from one as astute as yourself!

    Take care!

Posting Permissions

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