+ Reply to Thread
Results 1 to 5 of 5

Class Module & GotFocus with ComboBoxes

Hybrid View

  1. #1
    Registered User
    Join Date
    12-01-2007
    Posts
    42

    Class Module & GotFocus with ComboBoxes

    Hello,

    I would like to create a class module to simplify my procedure. These are ComboBoxes that depend on the GotFocus event.
    I started, somehow, to deal with the problem, but it didn't work.
    How to do it ?
    Attached Files Attached Files

  2. #2
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Class Module & GotFocus with ComboBoxes

    Hi there,

    I haven't read your code in detail, but it seems that you're trying to use the GotFocus event in a Class module which contains a ComboBox object declared WithEvents.

    Unfortunately (as far as I am aware) the GotFocus event is not exposed in such a situation. When you place the cursor inside your "CBL_Click" routine you can see the list of available events in the dropdown list at the top of the VBA Editor window (GotFocus is not included).

    I had a similar requirement some years ago and I found that an excellent solution had been developed by Jaafar Tribak; it can be found using the following link:



    Hope this helps you - it certainly helped me!

    Regards,

    Greg M

  3. #3
    Registered User
    Join Date
    12-01-2007
    Posts
    42

    Re: Class Module & GotFocus with ComboBoxes

    Hello Greg M,

    I looked a bit on other forums, actually it does not seem to be possible.
    I will try to go through Click instead of GotFocus. But it is not won ...
    Last edited by Magic_Doctor; 09-28-2021 at 11:22 PM.

  4. #4
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Class Module & GotFocus with ComboBoxes

    Hi again,


    I looked a bit on other forums, actually it does not seem to be possible.


    But I think that it very definitely IS possible!

    Take a look at the attached workbook and see if it gets you moving in the right direction. Select/tab through the various controls on the UserForm and see how the ComboBoxes respond when they receive or lose the focus.

    The VBA CodeModule for the UserForm contains the following code:

    
    
    Option Explicit
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Public WithEvents mclsWatchFormControls As C21_WatchFormControls
    
    
    '=========================================================================================
    '=========================================================================================
    '           USERFORM ROUTINES
    '=========================================================================================
    '=========================================================================================
    
    
    Private Sub UserForm_Initialize()
    
        Me.btnClose.SetFocus
    
    End Sub
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Private Sub UserForm_Activate()
    
    '   NOTE: The Class Method invoked by this routine accesses the currently-active Control
    '   on the UserForm, so the UserForm must already have been created (otherwise there can
    '   be no active Control on it) - the following code must therefore be called from the
    '   UserForm_Activate (i.e. this) routine rather than from the UserForm_Initialize routine
    
    '   Instantiate the Class which will watch for UserForm controls getting/losing the Focus
        If mclsWatchFormControls Is Nothing Then
    
            Set mclsWatchFormControls = New C21_WatchFormControls
                mclsWatchFormControls.StartWatching Form:=Me
    
        End If
    
    End Sub
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    
        If CloseMode <> vbFormCode Then
    
            Call btnClose_Click
            Cancel = True
    
        End If
    
    End Sub
    
    
    '=========================================================================================
    '=========================================================================================
    '           USERFORM CONTROL ROUTINES
    '=========================================================================================
    '=========================================================================================
    
    
    Private Sub btnClose_Click()
    
        Call StopWatchingFormControls
    
        Me.Hide
    
    End Sub
    
    
    '=========================================================================================
    '=========================================================================================
    '           PRIVATE ROUTINES
    '=========================================================================================
    '=========================================================================================
    
    
    Private Sub StopWatchingFormControls()
    
        If Not mclsWatchFormControls Is Nothing Then
    
            mclsWatchFormControls.StopWatching
            Set mclsWatchFormControls = Nothing
    
        End If
    
    End Sub
    
    
    '=========================================================================================
    '=========================================================================================
    '           CLASS EVENT ROUTINES
    '=========================================================================================
    '=========================================================================================
    
    
    Private Sub mclsWatchFormControls_ControlGotFocus(ctl As MSForms.control)
    
        If TypeOf ctl Is MSForms.ComboBox Then
    
            MsgBox "The ComboBox """ & ctl.Name & """ has got the focus"
    
        End If
    
    End Sub
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Private Sub mclsWatchFormControls_ControlLostFocus(ctl As MSForms.control, _
                                                       bCancel As Boolean)
    
        If TypeOf ctl Is MSForms.ComboBox Then
    
            MsgBox "The ComboBox """ & ctl.Name & """ had lost the focus"
    
        End If
    
    End Sub
    And a Class Module called "C21_WatchFormControls" contains the following code:

    
    
    
    Option Explicit
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Const msMODULE_NAME             As String = "C21_WatchFormControls"
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Public Event ControlLostFocus(ctl As MSForms.control, bCancel As Boolean)
    Public Event ControlGotFocus(ctl As MSForms.control)
    
    '~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
    
    Private mctlPreviouslyActive    As MSForms.control
    Private mbFormLoaded            As Boolean
    Private mbCancel                As Boolean
    
    
    '=========================================================================================
    '=========================================================================================
    '           CLASS METHODS
    '=========================================================================================
    '=========================================================================================
    
    
    Public Sub StartWatching(Form As UserForm)
    
        mbFormLoaded = True
    
        Set mctlPreviouslyActive = Form.ActiveControl
        RaiseEvent ControlGotFocus(Form.ActiveControl)
    
        Do While mbFormLoaded = True
    
            If Not mctlPreviouslyActive Is Nothing Then
    
                If Not mctlPreviouslyActive Is Form.ActiveControl Then
    
                    RaiseEvent ControlLostFocus(mctlPreviouslyActive, mbCancel)
    
                    If mbCancel Then
    
                          mctlPreviouslyActive.SetFocus
    
                    Else: RaiseEvent ControlGotFocus(Form.ActiveControl)
                          Form.ActiveControl.SetFocus
    
                    End If
    
                End If
    
            End If
    
            Set mctlPreviouslyActive = Form.ActiveControl
    
            mbCancel = False
            DoEvents
    
        Loop
    
    End Sub
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Public Sub StopWatching()
    
        Set mctlPreviouslyActive = Nothing
        mbFormLoaded = False
    
    End Sub

    As I mentioned in my original post, I'm happy to acknowledge that the idea behind the above code came from Jaafar Tribak.

    By the way, I just love the lists of "ingredients" in your ComboBox dropdown lists.

    Hope this helps - please let me know how you get on.

    Regards,

    Greg M
    Attached Files Attached Files

  5. #5
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2406 (Windows 11 23H2 64-bit)
    Posts
    81,605

    Re: Class Module & GotFocus with ComboBoxes

    Administrative Note:

    We would very much like to help you with your query, however it has been brought to our attention that the same query has been posted on one or more other forums and you have not provided the required cross-post link(s) here.

    Please see Forum Rule #3 about cross-posting and adjust accordingly. Read this to understand why we (and other sites like us) consider this to be important.

    (Note: this requirement is not optional. No help to be offered until the link is provided.)
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Class Module Events : How to refering to individual class members
    By kev_ in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-29-2020, 06:27 AM
  2. Class Module to design a class based on an autoshape (arrow)
    By BluesEnd in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-18-2017, 07:23 AM
  3. Place code in userform, module or class module
    By nigelog in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-16-2017, 07:04 AM
  4. [SOLVED] ComboBox class .AddItem filled in class module
    By Jacques Grobler in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 07-03-2012, 05:48 AM
  5. Populating comboboxes and ranges through a class module
    By Mats Samson in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 12-08-2011, 01:14 PM
  6. Replies: 1
    Last Post: 08-30-2011, 02:23 AM
  7. comboboxes, userforms and class modules
    By natanz in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-29-2005, 11:10 AM

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