BLOGS BY TOPIC
BLOGS BY AUTHOR
BLOGS BY YEAR
The two previous parts of this mini-blog have shown how to draw forms and how to write code to handle form events. This final part shows how to add some of the more exotic controls to user forms, like combo boxes, list boxes, multipage controls, spinners and option buttons.
- Advanced Controls - Our Example
- Multipage Controls
- Combo boxes (and list boxes)
- Multiple Column Combo Boxes and Listboxes
- Multi-select ListBoxes
- SpinButtons (Spinners)
- Check Boxes
- Option Buttons (Radio Buttons) (this blog)
- The Calendar Control
Posted by Andy Brown on 28 February 2012
You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.
Option Buttons (Radio Buttons)
An option button (sometimes called a radio button) allows you to choose between mutually exclusive possibilities:
You can have no milk, skimmed or normal milk, but you must choose one and one only possibility.
I'd avoid option buttons like the plague: a combo box is easier to create, and makes it much easier to add additional options that you've forgotten (semi-skimmed milk, anyone?). I've only included this page because some clients or managers will ask for option buttons!
How Option Buttons Work
In a group of option buttons, each one can be True or False, but at any one time only one button can be True:
Here the OptNone option button has a value of False initially. As soon as you select None as your milk choice - and thereby set the value of OptNone to True - the value of the other option buttons in the frame will automatically be set to False.
Adding Option Buttons using a Frame
There are two ways to add option buttons: either as part of a frame, or as part of an option group. To add option buttons to a frame, first add the frame:
|Click on the Frame tool ...||... to add a frame|
You can then click on the option button tool to add buttons to the frame one by one:
|Click on the option button ...||... then click inside the frame|
It's a good idea to rename your option buttons once you've added them: I've called the ones above OptNone, OptSkimmed and OptNormal.
Adding Option Buttons in a Group
Option buttons don't have to belong to a group. Instead, you can add the option buttons to a form, then set their GroupName property:
Select the option buttons to change their GroupName property (or you can set it for each button individually).
Type in a value for the GroupName property (here we've set it to grpMilkChoices). Because no other option buttons use the same group name, the buttons will automatically now cancel each other when clicked.
There's no great advantage that I can think of to using a frame or using grouped buttons - maybe it's down to personal preference!
Coding Option Buttons
The code to manage option buttons can be a bit messy. Here's our example code:
'transfer milk option into worksheet
'variable to hold name of milk choice
Dim MilkChoice As String
'determine which button was chosen
If Me.optNone Then
MilkChoice = "None"
ElseIf Me.optSkimmed Then
MilkChoice = "Skimmed"
ElseIf Me.optNormal Then
MilkChoice = "Normal"
MilkChoice = "N/A2"
'transfer this into worksheet
ActiveCell.Offset(0, 4).Value = MilkChoice
Adding another option button is not only going to mess up the form, but also require more lines of code. Like I say - combo boxes are easier!