+ Reply to Thread
Results 1 to 6 of 6

Which combobox has the focus?

  1. #1
    Daniel Bonallack
    Guest

    Which combobox has the focus?

    I have a label on Userform A - if you click it, it opens up Userform B

    Is there a way of capturing (when I click the label) which combobox had the
    focus in UserForm A?

    In other words, cmbIndustry in UserForm A has the cursor flashing in it.
    Then I click Label A. I would like my variable sHadFocus to be equal to
    "cmbIndustry".

    Is that possible?

    Thanks in advance
    Daniel




  2. #2
    Peter T
    Guest

    Re: Which combobox has the focus?

    Hi Daniel,

    Try something like this

    ' In Userform1

    Dim mCtrInFocus As Control

    Public Property Set propHadFocus(ctr As Control)
    Set mCtrInFocus = ctr
    End Property

    Public Property Get propHadFocus() As Control
    Set propHadFocus = mCtrInFocus
    End Property

    Private Sub ComboBox1_Enter()
    Set propHadFocus = ComboBox1
    End Sub

    ' in Userform2 with Userform1 loaded

    Private Sub CommandButton1_Click()
    Dim ctl As Control
    On Error Resume Next
    Set ctl = UserForm1.propHadFocus
    If Not ctl Is Nothing Then
    MsgBox ctl.Name, , TypeName(ctl)
    Else
    MsgBox "can't get control"
    End If
    End Sub

    Regards,
    Peter T



    "Daniel Bonallack" <DanielBonallack@discussions.microsoft.com> wrote in
    message news:BC2F4B62-8551-45F0-9CC3-B46AAF4AF1C1@microsoft.com...
    > I have a label on Userform A - if you click it, it opens up Userform B
    >
    > Is there a way of capturing (when I click the label) which combobox had

    the
    > focus in UserForm A?
    >
    > In other words, cmbIndustry in UserForm A has the cursor flashing in it.
    > Then I click Label A. I would like my variable sHadFocus to be equal to
    > "cmbIndustry".
    >
    > Is that possible?
    >
    > Thanks in advance
    > Daniel
    >
    >
    >




  3. #3
    Dave Peterson
    Guest

    Re: Which combobox has the focus?

    Are you doing this to be able to return to the same control when you reshow the
    first userform?

    In my simple testing, that wasn't necessary. The activecontrol didn't change.

    But you could get the activecontrol (or the activecontrol name) with something
    like:

    Option Explicit
    Private Sub Label1_Click()

    Dim ActCtrl As Control

    Set ActCtrl = Me.ActiveControl

    Me.Hide
    UserForm2.Show
    ActCtrl.SetFocus
    Me.Show

    End Sub

    Or you could use its name:

    Option Explicit
    Private Sub Label1_Click()

    Dim ActCtrlName As String

    ActCtrlName = Me.ActiveControl.Name

    Me.Hide
    UserForm2.Show
    Me.Controls(ActCtrlName).SetFocus
    Me.Show

    End Sub


    Daniel Bonallack wrote:
    >
    > I have a label on Userform A - if you click it, it opens up Userform B
    >
    > Is there a way of capturing (when I click the label) which combobox had the
    > focus in UserForm A?
    >
    > In other words, cmbIndustry in UserForm A has the cursor flashing in it.
    > Then I click Label A. I would like my variable sHadFocus to be equal to
    > "cmbIndustry".
    >
    > Is that possible?
    >
    > Thanks in advance
    > Daniel


    --

    Dave Peterson

  4. #4
    Daniel Bonallack
    Guest

    Re: Which combobox has the focus?

    Hi Dave

    No, it's to be able to populate a combobox on the new form, depending on
    which combobox had the focus in the first form.

    So I'll try the code you've provided - thanks for that.

    Daniel

    "Dave Peterson" wrote:

    > Are you doing this to be able to return to the same control when you reshow the
    > first userform?
    >
    > In my simple testing, that wasn't necessary. The activecontrol didn't change.
    >
    > But you could get the activecontrol (or the activecontrol name) with something
    > like:
    >
    > Option Explicit
    > Private Sub Label1_Click()
    >
    > Dim ActCtrl As Control
    >
    > Set ActCtrl = Me.ActiveControl
    >
    > Me.Hide
    > UserForm2.Show
    > ActCtrl.SetFocus
    > Me.Show
    >
    > End Sub
    >
    > Or you could use its name:
    >
    > Option Explicit
    > Private Sub Label1_Click()
    >
    > Dim ActCtrlName As String
    >
    > ActCtrlName = Me.ActiveControl.Name
    >
    > Me.Hide
    > UserForm2.Show
    > Me.Controls(ActCtrlName).SetFocus
    > Me.Show
    >
    > End Sub
    >
    >
    > Daniel Bonallack wrote:
    > >
    > > I have a label on Userform A - if you click it, it opens up Userform B
    > >
    > > Is there a way of capturing (when I click the label) which combobox had the
    > > focus in UserForm A?
    > >
    > > In other words, cmbIndustry in UserForm A has the cursor flashing in it.
    > > Then I click Label A. I would like my variable sHadFocus to be equal to
    > > "cmbIndustry".
    > >
    > > Is that possible?
    > >
    > > Thanks in advance
    > > Daniel

    >
    > --
    >
    > Dave Peterson
    >


  5. #5
    Daniel Bonallack
    Guest

    Re: Which combobox has the focus?

    Hi Peter

    Thanks very much, I'll give this a go.

    regards
    Daniel

    "Peter T" wrote:

    > Hi Daniel,
    >
    > Try something like this
    >
    > ' In Userform1
    >
    > Dim mCtrInFocus As Control
    >
    > Public Property Set propHadFocus(ctr As Control)
    > Set mCtrInFocus = ctr
    > End Property
    >
    > Public Property Get propHadFocus() As Control
    > Set propHadFocus = mCtrInFocus
    > End Property
    >
    > Private Sub ComboBox1_Enter()
    > Set propHadFocus = ComboBox1
    > End Sub
    >
    > ' in Userform2 with Userform1 loaded
    >
    > Private Sub CommandButton1_Click()
    > Dim ctl As Control
    > On Error Resume Next
    > Set ctl = UserForm1.propHadFocus
    > If Not ctl Is Nothing Then
    > MsgBox ctl.Name, , TypeName(ctl)
    > Else
    > MsgBox "can't get control"
    > End If
    > End Sub
    >
    > Regards,
    > Peter T
    >
    >
    >
    > "Daniel Bonallack" <DanielBonallack@discussions.microsoft.com> wrote in
    > message news:BC2F4B62-8551-45F0-9CC3-B46AAF4AF1C1@microsoft.com...
    > > I have a label on Userform A - if you click it, it opens up Userform B
    > >
    > > Is there a way of capturing (when I click the label) which combobox had

    > the
    > > focus in UserForm A?
    > >
    > > In other words, cmbIndustry in UserForm A has the cursor flashing in it.
    > > Then I click Label A. I would like my variable sHadFocus to be equal to
    > > "cmbIndustry".
    > >
    > > Is that possible?
    > >
    > > Thanks in advance
    > > Daniel
    > >
    > >
    > >

    >
    >
    >


  6. #6
    Daniel Bonallack
    Guest

    Re: Which combobox has the focus?

    Thanks very much - your solution worked perfectly

    "Dave Peterson" wrote:

    > Are you doing this to be able to return to the same control when you reshow the
    > first userform?
    >
    > In my simple testing, that wasn't necessary. The activecontrol didn't change.
    >
    > But you could get the activecontrol (or the activecontrol name) with something
    > like:
    >
    > Option Explicit
    > Private Sub Label1_Click()
    >
    > Dim ActCtrl As Control
    >
    > Set ActCtrl = Me.ActiveControl
    >
    > Me.Hide
    > UserForm2.Show
    > ActCtrl.SetFocus
    > Me.Show
    >
    > End Sub
    >
    > Or you could use its name:
    >
    > Option Explicit
    > Private Sub Label1_Click()
    >
    > Dim ActCtrlName As String
    >
    > ActCtrlName = Me.ActiveControl.Name
    >
    > Me.Hide
    > UserForm2.Show
    > Me.Controls(ActCtrlName).SetFocus
    > Me.Show
    >
    > End Sub
    >
    >
    > Daniel Bonallack wrote:
    > >
    > > I have a label on Userform A - if you click it, it opens up Userform B
    > >
    > > Is there a way of capturing (when I click the label) which combobox had the
    > > focus in UserForm A?
    > >
    > > In other words, cmbIndustry in UserForm A has the cursor flashing in it.
    > > Then I click Label A. I would like my variable sHadFocus to be equal to
    > > "cmbIndustry".
    > >
    > > Is that possible?
    > >
    > > Thanks in advance
    > > Daniel

    >
    > --
    >
    > Dave Peterson
    >


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1