+ Reply to Thread
Results 1 to 16 of 16

Class to control multiple option buttons affecting differenet textboxes

  1. #1
    Registered User
    Join Date
    06-10-2015
    Location
    Canada
    MS-Off Ver
    2007/2010
    Posts
    6

    Class to control multiple option buttons affecting differenet textboxes

    Hi there! I've created a userform with 26 option buttons, half of which are "yes" buttons and the other half "no". Of the "no" buttons, some run the following sub (below is a general sub; in reality, there are 10 subs, each with the corresponding numbers in place of # and @):

    Please Login or Register  to view this content.
    Where # is the option button's index (4, 6, 8, 10, 14, 16, 20, 22, 24, or 26) and @ is the corresponding textbox's index (2, 3, 9, 10, 17, 16, 23, 24, 30, or 31). The idea is, when you select one of these "no" option buttons, the corresponding textbox is displayed. (Conversely, when "yes" is selected, the textbox is hidden.)

    Currently, I have one sub for each option button. I want to make a class that will replace the 10 userform subs that are currently being used. I've played around with classes, but I'm having trouble telling the function in the class which textbox the option button affects (for example, OptionButton4 affects TextBox2). I was able to follow this example quite easily:

    http://www.thelandbeyondspreadsheets...el-vba/#showme

    ...but I don't know how the include the textbox in what I want to do.

    Any help is much appreciated!

  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: Class to control multiple option buttons affecting differenet textboxes

    Hello cpietka,

    Welcome to the Forum!

    I really need to see what code you have written so far. The best option is to post a copy of your workbook.

    How To Post Your Workbook
    1. At the bottom right of the Reply window, Click the button Go Advanced
    2. At the top of the Your Message Window, in the first row of icons, Click the Paperclip icon.
    3. Click the Add Files button at the top right in the dialog box. This displays the File Manager dialog.
    4. Click the Select Files button at the bottom middle of the dialog.
    5. In the new window Find the file you want to upload, select it, and Click Open.
    6. You will now be back in the File Manager dialog. Click the bottom Middle button Upload File.
    7. Wait until the file has completely uploaded before you exit the File Manager dialog.
    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
    Registered User
    Join Date
    06-10-2015
    Location
    Canada
    MS-Off Ver
    2007/2010
    Posts
    6

    Re: Class to control multiple option buttons affecting different textboxes

    Hello! Thank you for the quick response. A little bit of a heads up, I have little experience in programming, and have been learning VBA over the past couple of weeks, so the code isn’t particularly glamorous. Also, my apologies for the excessive amount of commenting; I’m making this form for someone with even less experience in VBA than myself who will likely have to make changes within the next year, and so has asked me to comment everything.

    There is more to the actual workbook that what I’ve attached but it’s just more userforms (I’m only having trouble with this one, as the others are pretty simple). There are 26 option button subs, and I’m trying to compress this somehow to make it easier to maintain. They don’t all do the same thing, but I’m thinking I could group them by what they do. Option buttons 4, 6, 8, 10, 14, 16, 20, 22, 24, and 26, for example, all do the same thing but with different textboxes. I’ve done a bit of Googling, and it looks like I can do this with a class module… I’ve taken a Java course, but I haven’t made a useful class in VBA yet. I attempted writing a class module, but the outcome was a bit embarrassing, so I haven’t included it. :P
    Attached Files Attached Files

  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: Class to control multiple option buttons affecting differenet textboxes

    Hello cpietka,

    Thanks for posting the workbook.I only count 24 Option Buttons on the UserForm. I think you are counting by the assigned name index. You have deleted a few from this workbook.

    Can you provide me with more details about these groups and which buttons should be in which group? Tell me which page they are on Disinfectant, Dechlorination, or Coagulant.

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

    Re: Class to control multiple option buttons affecting differenet textboxes

    I think that this will help you.

    In the attached, there is a class, clsLinkedControl.
    A clsLinkedControl object has properties:
    .BooleanControl - either a checkbox or an option button
    .HideWhenFalse - a collection of controls. The Visible property of each of these controls will be set to False, whenever .BooleanControl is False
    .HideWhenTrue - a collection of controls. Their .Visible will be false when .BooleanControl.Value = True
    .ShowWhenFalse - a collection whose .Visible will be True when .BooleanControl.Value = False
    .ShowWhenTrue - a collection whose .Visible will be True when .BooleanControl.Value = True

    There are methods .AddHideWhenFalse, .AddHideWhenTrue, .AddShowWhenFalse, .AddShowWhenTrue that add controls to these collections

    There is also a class to hold them, clsLinkedControls

    clsLinkedControls has an .Add function. The first (required) argument of .Add is the .BooleanControl of the clsLinkedControl that is being added.

    This code (in the Userform_Intialize) will show TextBox1 and TextBox2 when CheckBox1 is True and hide them when it is false. But TextBox3's visibility will be the inverse of CheckBox1. Textbox1 and TextBox2 are said to match CheckBox1, TextBox3 is said to invert CheckBox1
    Please Login or Register  to view this content.
    .Add will accept other (optional) arguments
    .Add(BooleanControl, (optional)list of matching controls, (optional boolean) inverse flag, (optional) list of inverting controls)

    The above code could be replaced by

    Please Login or Register  to view this content.
    All of your OptionButton and CheckBox event code has been replaced by this block in the Userform_Initilaize event
    Please Login or Register  to view this content.
    Attached Files Attached Files
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

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

    Re: Class to control multiple option buttons affecting differenet textboxes

    I've changed clsLinkedControl and clsLinkedControls to make it more versatile and more intuitive.

    clsLinkedControl.BooleanControl can now be any control, not just an OptionButton or CheckBox.
    BooleanControl is deemed to be True if its value equals the .TrueValue property of the clsLinkedControl object.
    Although currently written for only CheckBoxes, OptionButtons and ComboBoxes, adding a new kind of control is pretty transparent.
    There are two new properties, .ClearWhenTrue and .ClearWhenFalse. Both are collections of controls whose values will be set to vbNullString when the BooleanControl is True (or False). The methods .AddClearWhenFalse and .AddClearWhenTrue have also been added.
    In addition, there are methods .FollowBooleanControl and .InvertBooleanControl. Passing a user form's control as an argument for these methods will cause the control's visibility to either follow or invert the value of the BooleanControl.

    I've changed the confusing syntax for the clsLinkedControls .Add method.
    It now requires a msforms.Control object for the BooleanControl
    The second argument, TrueValue, is optional and defaults to True.

    This will make is so TextBox1 and TextBox2 are visible if, and only if, OptionButton1 is checked. TextBox3 will be visible iif OptionButton1 is unchecked.
    Please Login or Register  to view this content.
    This will make it so TextBox5 is visible iff ComboBox1.Value = "cat". Also, whenever ComboBox1.Value is changed to "cat", TextBox7 will be cleared
    Please Login or Register  to view this content.
    Here is the code for the new clsLinkedControl
    Please Login or Register  to view this content.
    and the new clsLinkedControls
    Please Login or Register  to view this content.
    Now I need to go to the OP's workbook and alter the Initialize event. And include the ComboBoxes.

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

    Re: Class to control multiple option buttons affecting differenet textboxes

    OK, I've incorporated this into the user form.

    Your using the Intialize event as a reset routine required that I move the class set-up into the Activate event.
    I also had issues with setting up clsLinkedControl objects for the ComboBoxes.

    Initially, I planned to use one clsLinkedControl object per combo box
    Please Login or Register  to view this content.
    But clearing the combo box would trigger the BooleanControl_Change event. So I had to use two clsLinkedControl objects, with different .TrueValue's and only use the ...WhenTrue collections. Preserving the "If ComboBox1.Value = vbNullString Then do nothing"
    Please Login or Register  to view this content.
    Here's the whole Active Event.
    Please Login or Register  to view this content.
    I think this version of the custom classes is more intuitive and easier to edit.
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    06-10-2015
    Location
    Canada
    MS-Off Ver
    2007/2010
    Posts
    6

    Re: Class to control multiple option buttons affecting differenet textboxes

    Hello mikerickson,

    Thank you very much for your help, I really appreciate it! This is still a bit more complicated than I had in mind, but I'm beginning to think that maybe what I had in mind isn't possible. I've found a few code snippets online written in VB that are closer to what I'm trying to achieve, such as:

    Please Login or Register  to view this content.
    Which I found here: http://www.vb-helper.com/howto_net_s...t_handler.html

    Basically, what I was hoping to find is a way of replacing my repetitive option button event handlers with a single event handler. However, I think I will stick with what you have provided. Thanks again!

  9. #9
    Registered User
    Join Date
    06-10-2015
    Location
    Canada
    MS-Off Ver
    2007/2010
    Posts
    6

    Re: Class to control multiple option buttons affecting differenet textboxes

    Hello Leith Ross,

    I still count 26 option buttons: 10 on the "Disinfectant" page; 6 on the "Dechlorination" page; and 10 on the "Coagulant" page.

    There are five different "types" of event handlers which handle the option buttons. For instance, options buttons 3, 5, 7, 9, 13, 15, 19, 21, 23, and 25 have event handlers like this one:

    Please Login or Register  to view this content.
    ...but where the textbox index differs for each option button.

    Similarly, option buttons 4, 6, 8, 10, 14, 16, 20, 22, 24, and 26 have event handlers like this one:

    Please Login or Register  to view this content.
    ...again, where the textbox index differs for each option button.

    I'm looking to replace the individual event handlers with a single event handler, to make it easier to maintain the code should, for instance, a new tank be added to "Dechlorination".

  10. #10
    Registered User
    Join Date
    06-10-2015
    Location
    Canada
    MS-Off Ver
    2007/2010
    Posts
    6

    Re: Class to control multiple option buttons affecting different textboxes

    Hi everyone,

    This might make my problem more clear. Attached is a simpler userform. It has 12 option buttons. Buttons 1 through 6 have a code similar to:

    Please Login or Register  to view this content.
    And buttons 7 through 12 have a code similar to:

    Please Login or Register  to view this content.
    Currently, each option button has its own even handler. Is there any way to compress this so that there is one for buttons 1 through 6, and one for buttons 7 through 12?

    Thank you for any input.
    Attached Files Attached Files

  11. #11
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Class to control multiple option buttons affecting differenet textboxes

    Only the way Rick has demonstrated. The code you found is VB.Net which is completely different to VBA, you can't do that in VBA

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

    Re: Class to control multiple option buttons affecting differenet textboxes

    I'm taking another look at your situation.
    Rather than focusing on code for every OptionButton, what I'm seeing is that you have frames.
    Some frames enclose one text box.
    Others enclose two textboxes and an option button pair that toggles one of the textboxes between user entered value and a default.
    Others enclose four textboxes and an option button pair.

    These three kinds of frames, and their controls, could be cast as custom Controls.

    Similarly you have two kinds of choosers. One kind is an option button pair, the other kind is a combobox. That's two more custom Controls.

    These custom Controls can be grouped together One controller decides which of the custom multi-textboxs to show. Those groupings can be made into other custom controls.

    I need some time to develop this, but I hope the result is more transparent than the global "control every option button" style I used above.

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

    Re: Class to control multiple option buttons affecting differenet textboxes

    I looked at your reduced user form in post #10.
    Could I suggest a slightly different interface rather than two option buttons.

    Two option buttons working together are functionally the same as a check box. Either one of the buttons is on or the other. Either the CheckBox is clicked or its not.
    Two option buttons = One check box.

    So I'd suggest using one CheckBox with one TextBox. (The .Caption of the CheckBox could be "Use previous from yesterday")

    I'd also suggest that you calculate all of these yesterday values in the initialize event and put them in the respective .Tag properties of the TextBox (or the CheckBox)

    That would reduce the code for each entry to

    Please Login or Register  to view this content.
    Note that, after the .Tag was set to yesterday's value in the Initialize event, you don't have to test anything when reading the "Previous" value. Its whatever is in TextBox1.Text

    The machinery of setting up a custom class to do this and assigning each check box to its text box is not worth the effort IMO.

    I'll be working on componentizing the OP user form so you can add tanks easily.

  14. #14
    Registered User
    Join Date
    06-10-2015
    Location
    Canada
    MS-Off Ver
    2007/2010
    Posts
    6

    Re: Class to control multiple option buttons affecting differenet textboxes

    Hi mikerickson,

    I'm liking you last suggestion, with the checkboxes in place of the option buttons. The idea really is to simplify the userform and it's code, so if classes aren't the way to do this, I'm definitely open to other possibilities.

    Thanks!

  15. #15
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Class to control multiple option buttons affecting differenet textboxes

    Hi cpietka,

    I apologize for being late to the party. I took a slightly different class approach (see the attached file), that leaves your design almost intact. I prefer your original approach with the OptionButtons, because I think the User may be less confused by OptionButtons. My OptionButton approach is easily changed to CheckBoxes.

    What I did (using your code from post #10):
    a. Created an OptionButton class named 'ClassOptionButton'.
    b. Call Sub GenerateUtilitiesUserFormOptionButtonControlArray() from UserForm_Initialize to assign OptionButtons to an 'OptionButtonGroup' whose events will be handled by the Class.
    c. Each time an 'OptionButton' in the 'OptionButtonGroup' is clicked, the Class Click Event Handler is triggered. I prefer to keep code out of the Class module, and Sub ProcessOptionButtongroupClickEvent() in ordinary Code module ModUtilitiesUserForm does the heavy lifting.
    d. Using CONDITIONAL COMPILATION, I removed the OptionButton1 thru OptionButton12 Click Event Code in the UserForm module, because those events are now handled by the Class module.
    e. I added a debugging label 'LabelStatus' to the UserForm to display a message each time an OptionButton is clicked.

    Class module ClassOptionButton code:
    Please Login or Register  to view this content.
    UserForm Code excerpts (changes in red):
    Please Login or Register  to view this content.
    Ordinary Module Code (e.g. Module ModUtilitiesUserForm):
    Please Login or Register  to view this content.
    Lewis

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

    Re: Class to control multiple option buttons affecting differenet textboxes

    Its been a while, but I have been wrestling with the Creeping Feature Creature on this one.

    In the attached, a clsDefaultButton object is a TextBox and a Controller.
    The Controller is either a CheckBox, an OptionButton or a ToggleButton.
    When the Controller is checked, the TextBox is filled with the .DefaultValue, when not, the TextBox will accept user entry from the keyboard.

    A clsDefaultBox is instansized with code like
    Please Login or Register  to view this content.
    When CheckBox1 is clicked (so that Controller.Value = DefaultBox1.UseDefaultWhen) the .DefaultValue (today's date) is put in TextBox1.

    clsDefaultBox objects have a Change event.
    DefaultBox1.Reset will reset the Controller and TextBox to the .ResetControllerValue and the .ResetTextBoxValue

    These are the properties of a clsDefaultBox
    Please Login or Register  to view this content.
    In the attached, Userform1 there is a multi-page.
    On page 1, there is a CheckBox style clsDefaultBox, DefaultBox1, and a Reset button, which resets DefaultBox1
    The DefaultBox1_Change event puts the DefaultBox1.Value into a Label.

    There is also a Monitor that allows you to change the properties of that clsDefaultBox (similar to the Properties window in the VBEditor)

    Page2 has an OptionButton style clsDefaultBox and Page3 has a ToggleButton style.

    Userform2 goes with Sheet2 and is an implementation from post #6(?)

    As I've said, I've been struggling with the Creeping Feature Creature.
    Features that one might add:
    -Validation, the clsDefaultBox might restrict user entry to numeric or integer entry from the keyboard.

    -.UnDefaultTextValue - as written when the Controller is changed from UseDefaultWhen, the default value remains in the TextBox (the user can edit it). One could code a .UnDefaultTextValue property that specified some other string to fill the TextBox in that situation.

    - a ToggleButton style clsDefaultBox could be made to toggle the ToggleButton's .Caption between the clsDefault object's .Caption and .Caption2 properties.
    Attached Files Attached Files

+ 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] Add form control to identify the option buttons to each group
    By Rocky2013 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-11-2015, 08:58 AM
  2. can you have SEPERATE form control option buttons on one sheet??
    By Muzza86 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 05-20-2014, 09:18 AM
  3. Using control toolbox's option buttons to switch between vlookup tables
    By da_sprite in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-15-2009, 05:45 AM
  4. Option Buttons and Drop Down List: Form Control
    By starbwoy in forum Excel General
    Replies: 4
    Last Post: 08-27-2008, 01:09 PM
  5. Replies: 2
    Last Post: 04-22-2005, 09:08 PM
  6. No control for option buttons, invisible macro??
    By Steve in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-13-2005, 10:06 AM
  7. [SOLVED] Selective control of Option Buttons
    By gtcyberaz in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-25-2005, 02:06 PM

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