+ Reply to Thread
Results 1 to 8 of 8

Combining object (textbox and combobox) change events

  1. #1
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Combining object (textbox and combobox) change events

    Hi,

    I have a userform with seven textboxes and seven related comboxes. So far the code works by preventing the combobox from being activated unless something is in the related textbox. Also it prevents the OK button from being activated if a textbox is populated but its related combobox is empty:

    Please Login or Register  to view this content.
    To be more efficient because the code is redundant, I have create two generic subs (one for textboxes and one for comboboxes) and I call each respective sub from the Change Event sub for the respective textbox and combobox.

    Is there a way to remove all these change event subs. Something maybe like:

    Please Login or Register  to view this content.
    I know that this is not correct, but there is a correct syntax or this not possible?

    abousetta
    Last edited by abousetta; 09-11-2011 at 10:46 PM.
    Please consider:

    Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
    Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.

  2. #2
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Combining object (textbox and combobox) change events

    Do not use 'Call' (it's 2011 !!)

    Please Login or Register  to view this content.
    It's always practical to give macro's that can be 'called' a discriminative name.
    I use a capital, followed by an underscore.
    Last edited by snb; 09-11-2011 at 04:41 PM.



  3. #3
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Combining object (textbox and combobox) change events

    The common way to make common event code for different controls is to use a custom class. Since each of your textboxes is paired with a combo box, this custom class is for that pair.

    Insert a Class module, change its name to clsPairedControls and put this code into it.
    Please Login or Register  to view this content.
    Then one has to create instances of that class and assign the appropriate controls to the class and store them in the userform.
    Put this code in the userform's code module.
    Please Login or Register  to view this content.
    I've assumed that the default (initial) Enabled for the comboboxes is False and the default Enabled for the command button is also False.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  4. #4
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Combining object (textbox and combobox) change events

    Thanks Mike. I am struggling to get your approach to work. I have stripped the form down to the bare essentials (attached). Its giving me an error at this line of code as object support this property or method:

    Please Login or Register  to view this content.
    abousetta
    Attached Files Attached Files

  5. #5
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Combining object (textbox and combobox) change events

    Thanks snb. I'm still self-teaching myself programming and so I may not be up to 'code'. I just learned to 'call' other subs and that's how I have been using it. I will keep this in mind for better coding practices.

    abousetta

  6. #6
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Combining object (textbox and combobox) change events

    Also a class approach, but maybe simpler, using an array.
    Attached Files Attached Files
    Last edited by snb; 09-11-2011 at 04:49 PM.

  7. #7
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Combining object (textbox and combobox) change events

    Ahh... the controls are all in a frame.
    The code was using ComboCtrl.Parent to refer to the Userform. Since they are in a frame, that returns the frame not the userform.
    So that needs to be changed to ComboCtrl.Parent.Parent
    Please Login or Register  to view this content.

  8. #8
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Combining object (textbox and combobox) change events

    Thanks snb and mikerickson. I have learned alot from this thread.

    abousetta

+ 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