I've uploaded an example. You may be able to copy and paste the option buttons directly from my form onto yours (I'm not sure).
Also, here's a section from my notebook which explains how to use option buttons (you shouldn't need it as I already wrote the code for you).
------------------ start of option Group control --------------------
start of group box
start of radiobutton
WARNING: If two option groups are close together, they might not work right. Keep them far apart. Also, if you want to change the backcolor, you may need to change the special effect to Raised in order for the new color to show up.
- In a VBA UserForm (as opposed to an Access Form), option group works different than in Access - works like a VB.net groupbox. Yes !!!
- one problem with an Access optin group is that when you drag it, the controls don't drag with it. You might be better off using a subform, if all you need is a draggable group box to encircle the controls.
Option Group is a group box into which you can drop radio buttons or checkboxes.
An option button is just a radiobutton or a checkbox.
If you DRAG radiobuttons into an OptionGroup, the OptoinGroup will behave like a mere rectangle (where all the radiobuttons can be checkmarked at once). But if you DROP radiobuttons into an OPtionGroup (using Control-V to paste them), it will behave like a true OptionGroup. To insure that the RBs fall into the right place, make sure the target OptionGroup is selected and then use Control-V to paste. WARNING: Although you can paste the radio buttons this way, you cannot paste textboxes and labels this way - these you have to drag.
(By the way, a toggle button is simply a radiobutton. Instead of having a bullet, it changes color when you select it).
Suppose you want multiple checkboxes selectable at once. Don't use an option group. Put them in a rectangle.
Thus a rectangle is a group box that allows for multiple radiobuttons or checkboxes to be selected at once.
- by the way, an optionGroup is named Frame1 in the designer.
Handle the After_Update event of the Option Group. If you like, you can caption the optionGroupBox1.Value property, which will be set to the radiobutton.Text value.
alternvatively, loop through all the radiobuttons (For each control in ...)
Dim ctl As Control
For Each ctl In grpOption.Controls
If ctl.ControlType = acOptionButton Then...
Next ctl
You cannot checkmark the radiobutton (called an Option Button0) directly. If there are 3 radio buttons, setting the group box to a vaue of 3 will checmkark the third one. You can do this during Form1_Open
optionGroup1 = 3
Or you can set the option groups "Default VAlue" in the designer, to 3. Note, also, that the third button doesn't necessarily have to associate with 3. You can associate it insstead with 2, if you like, by setting its option value to 2. Then you could set the second button's option value to 3.
How to change the color of a label. Also it demonstrats the use of an option group. For each radiobutton, set the OptionValue to a different number (in this case 1 to 4) so that when the user selects a radiobutton, the OptionGroup will value to the selected number, so that you can track his selectin.
Private Sub optNatureAndExtentGroup_AfterUpdate()
If Me.optNatureandExtentGroup = 1 Then
Me.lblHeadNeck.BackColor = vbYellow
ElseIf Me.optNatureandExtentGroup = 2 Then
Me.lblRightShoulder.BackColor = vbYellow
ElseIf Me.optNatureandExtentGroup = 3 Then
Me.lblLeftShoulder.BackColor = vbYellow
ElseIf Me.optNatureandExtentGroup = 4 Then
Me.lblChest.BackColor = vbYellow
End If
VBA has no TypeOf or GetType, instead use this:
If ctl.ControlType = acOptionButton Then...
Oddly, ctrl.ControlType doesn't show in Intellisense.
As far as to change the color, if the BackStyle is set to Transparent (i.e. zero), the color will be ignored because the parent color wins. If you change the color using code, be sure to add an extra line of code specifying Normal instead of Transparent
optionGroupCmbs.BackColor = vbBlack
optionGroupCmbs.BackStyle = 1 'Means 'Normal'
------------- end of option Group ---------------