Page 284 - Excel Progamming Weekend Crash Course
P. 284
n540629 ch20.qxd 9/2/03 9:35 AM Page 259
Session 20 — Controls for User Forms 259
You can have two or more groups of OptionButton controls on a form by plac-
ing each group within its own Frame control or by assigning different
Tip GroupName properties.
The following program demonstrates the use of OptionButton controls. Follow these steps
to create the user form.
1. In the VBA Editor, select Insert ➪ UserForm to add a new user form to the project.
2. Change the form’s Name property to OptionButtonDemo, and its Caption property
to Option Button Demo.
3. Add a Frame control to the form, and change its Caption property to Choose a
Color.
4. Add an OptionButton in the Frame control. Change its Caption property to Blue
and its Name property to optBlue.
5. Add two more OptionButton controls on the frame, setting the Name and Caption
properties to optRed and red for the first one and optGreen and Green for the sec-
ond one.
6. Change the Value property of one of the OptionButton controls to True (it does
not matter which one).
7. Add a CommandButton to the form but not on the frame. Change its Caption
property to OK.
8. Double-click the CommandButton control to edit its Click event procedure. Enter
the single line of code Me.Hide in the procedure.
9. Save the project.
The completed form should look similar to Figure 20-2. The next step is to write the VBA
procedure that displays this user form and retrieves the user’s data.
Figure 20-2 The complete Option Button Demo form
1. Open a code module in your project.
2. Use the Insert ➪ Procedure command to add a procedure named
TestOptionButtonDemo.
3. Add the code from Listing 20-5 to the procedure.