Access VBA Macro Programming

(Joao Candeias) #1
Option Buttons
Option buttons are sometimes known as radio buttons because as you click one in a group
of option buttons, the others are deselected and lose their black buttons. These are more
complicated to set up and code than the simple check box. Their advantage is that the user
can only select one option.
First, you must put an Option Group control onto your form. To do this, put the form into
Design View mode and drag the Option Group icon from the Controls Group of the ribbon.
This icon is found on the top row, third icon from the left. Size it to what you need for the
number of options and provide a title for it in the corresponding label box, which is added
automatically.
It is very important that you add the Options Group control first before you add the Option
Button controls. This is because you cannot add an Option button that was added prior to the
Options Group into an Option Group. Seems rather strange, but that is the way it is.
You can now add in the Option buttons. To do this, use the Option Button icon in the
Controls Group of the ribbon, which is on the bottom row, third icon from the left.
If you view your form in Form View mode, you will see that as you click each button, the
others go out. Thus, it is only possible to have one button in the group set.
To use VBA to interpret which option button is live, you can use the following code:

MsgBox MyOptionGroup.Value

This will return 1 if the first button is set, 2 if the second button is set, and so on.
You can set a default button by setting the Default Value of the Option Group control to
the number of the button. If you want the second button in the group to be the default, set the
Default Value property to 2.

Tab Control
The Tab Control allows you to use tabbed pages on a form and is very useful if you are running
out of space on a form. I have certainly written applications where there has been so much
information displayed on a form that the tabbed sheets were the only answer.
The Tab Control icon is on the top row of the Controls Group in the ribbon, fifth icon from
the left in Full Screen mode. It is a case of populating it with controls as if it was a form.
However, one problem is that you cannot use drag-and-drop to do this. If you do, in the
Design window it looks as if your control has ended up on the Tab Control page, but when
you look at the form in Form View mode, you will not see it because it is sitting behind the
Tab Control.
To put controls onto the Tab Control, you must first drag them onto an empty part of your
form. You then use cut and paste to move them from the form and to the Tab Control.
You can select the tabs in Design mode by clicking them. To insert and delete pages, right-click
the Tab Control and select the appropriate option from the pop-up menu. To change the name
of the tab, right-click the selected tab and choose Properties. You can now give the Caption
property its new name. The controls on the tabbed control can be used in VBA just as if they
were on the form itself.

110 Microsoft Access 2010 VBA Macro Programming

Free download pdf