+ Reply to Thread
Results 1 to 8 of 8

OLEObject Class Module

  1. #1
    Forum Contributor yay_excel's Avatar
    Join Date
    08-12-2011
    Location
    Canada
    MS-Off Ver
    2013
    Posts
    190

    Question OLEObject Class Module

    Hello. I would like to create GotFocus and LostFocus events in a class module to apply to all ComboBoxes on a certain worksheet. If I use MSForms.ComboBox I do not have access to the GotFocus and LostFocus events. If I use OLEObject I have access to only the GotFocus and LostFocus events. The ComboBoxes have already been added to another class (that may be incorrect terminology) using this code:
    Please Login or Register  to view this content.
    Is it possible for me to use the same type of setup for the OLEObject class as well? I am having trouble after much experimentation.

    Basically I need all the ComboBoxes on the worksheet to share the same KeyDown, GotFocus, and LostFocus code. What is the best way to achieve that?

    Thank you.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: OLEObject Class Module

    Hello yay_excel,

    What you are referring to here is known as Sub-Classing. You can Sub_Class a Sub-Classed object. It can get confusing pretty fast. This wouldn't be an issue if VBA were an Object Oriented Programming (OOP) language. Instead, it is an Object Based Programming language. The big distinction being VBA does not support true inheritance.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Contributor yay_excel's Avatar
    Join Date
    08-12-2011
    Location
    Canada
    MS-Off Ver
    2013
    Posts
    190

    Re: OLEObject Class Module

    Rewriting in Java is not feasible for me. :P

    Are you able to point me in the right direction?

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: OLEObject Class Module

    Hello yay_excel,

    I wasn't suggesting rewriting the code in another language. It was simply a statement about why it is more difficult to accomplish this in VBA than in other OOP languages.

    Actually, I am not sure you can attach a OLEObject class to a predefined OLE object like a ComboBox using VBA. Do you really need to handle these events?

  5. #5
    Forum Contributor yay_excel's Avatar
    Join Date
    08-12-2011
    Location
    Canada
    MS-Off Ver
    2013
    Posts
    190

    Re: OLEObject Class Module

    I have a list of comboboxes which are used to select an item from a database. From that selection, there are multiple VLOOKUP formulas which pull additional information about that item from the database. If I do not handle those events, all of those formulas calculate every time the user enters any character into the comboboxes. I do not want to calculate until the user has exited the combobox so as to speed it up. In the GotFocus and LostFocus events I want to change the calculation mode to manual and automatic respectively. Formerly I had separate event code for each combobox but I am currently refactoring and was hoping I could implement a class module to accomplish this.

    ...Maybe the best thing would be to redo the entire sheet as a userform?

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: OLEObject Class Module

    Hello yay_excel,

    The ComboBox has two events that can help: AfterUpdate and Exit. AfterUpdate occurs after the user has made an entry. This is good event to use for validation of the entry. The Exit event is similar to LostFocus. This occurs when the user has click on another object and before that object takes the focus.

    Seems to me, the AfterUpdate event would work for what you want to do.

  7. #7
    Forum Contributor yay_excel's Avatar
    Join Date
    08-12-2011
    Location
    Canada
    MS-Off Ver
    2013
    Posts
    190

    Re: OLEObject Class Module

    I don't have those events in my class module... I have:

    BeforeDragOver
    BeforeDropOrPaste
    Change
    Click
    DblClick
    DropButtonClick
    Error
    KeyDown
    KeyPress
    KeyUp
    MouseDown
    MouseMove
    MouseUp

    What have I done wrong?

  8. #8
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: OLEObject Class Module

    Hello yay_excel,

    It isn't you, I was thinking about a MSForms ComboBox. The standard ComboBox doesn't have those events.

    It looks like we have to create a generic OLE object class that your ComboBox class can access. I'm just not sure if it will be triggered by the GotFocus and LostFocus events.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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