557 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owls only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Advanced controls in userforms with VBA macros
Part eight of a nine-part series of blogs
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.
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!
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.
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.
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!
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!
|Parts of this blog|
25 Aytoun Street