Option Buttons

jstevens
GoldLounger
Posts: 2618
Joined: 26 Jan 2010, 16:31
Location: Southern California

Option Buttons

Post by jstevens »

I have a form with 12 option buttons with code behind each one. OB 7 unhides checkbox1.

When another option button is selected checkbox1 should be hidden. Is there a better way other than adding code to hide the checkbox to each option button?
Regards,
John

User avatar
HansV
Administrator
Posts: 78241
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Option Buttons

Post by HansV »

You could create a single procedure in the userform module that hides the check box, and call that from each of the option buttons except #7.
Best wishes,
Hans

jstevens
GoldLounger
Posts: 2618
Joined: 26 Jan 2010, 16:31
Location: Southern California

Re: Option Buttons

Post by jstevens »

Hans,

Your suggestion makes sense but is that not the same as adding this bit of code behind each option button: UserForm1.CheckBox1.Visible = False?
Regards,
John

User avatar
HansV
Administrator
Posts: 78241
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Option Buttons

Post by HansV »

More or less, yes, except that if you wanted to change the name of the check box, you'd have to change it only in the single procedure instead of in the 11 On Click event procedures of the option buttons.

An alternative would be to create a custom option button in a class module, but that would be overkill, since it would require extra code in the Userform_Initialize event procedure.
Best wishes,
Hans

jstevens
GoldLounger
Posts: 2618
Joined: 26 Jan 2010, 16:31
Location: Southern California

Re: Option Buttons

Post by jstevens »

Hans,

Thank you for the explanation.
Regards,
John

User avatar
SpeakEasy
4StarLounger
Posts: 536
Joined: 27 Jun 2021, 10:46

Re: Option Buttons

Post by SpeakEasy »

It is a shame that VBA (or more specifically MS Forms), unlike classic VB6, does not support control arrays, which make this kind of thing easy.

User avatar
HansV
Administrator
Posts: 78241
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Option Buttons

Post by HansV »

Yes, control arrays were a great concept.
Best wishes,
Hans