Using VBA to manipulate controls is easy. Access lets you control the various properties needed, even at runtime.
Figure 10.31 shows a form that's used for a main switchboard. This form uses an option group with toggle buttons to emulate a tabbed menu.
Note
This example can be done by using the native Access Tab control described earlier in the section “Working with the Native Access Tab Control.” I use the option group here to show manipulating controls on-the-fly and creating dynamic interfaces.
Three different types of Access objects make up this Option Group menu form: a table named Menus, a form named ManipulatingControlsExample, and the VBA code behind the form. Each category used on this menu has a number of tools assigned to it. This is done by using the Tag property of each command button, which will be discussed in a moment. First, look at the Menus table, which is used to keep track of the number of tools for each category. The current contents of this table are as follows:
MenuID | Description | NumberOfTools |
---|---|---|
1 | Order Entry | 9 |
2 | Sales | 4 |
3 | Purchasing | 4 |
The MenuID field is used for locating the corresponding tab value during program execution. The NumberOfTools field is used to control an array. A description is there for your own purposes, such as documentation.
Figure 10.32 shows the ManipulatingControlsExample form in Design view. Notice that most of the controls are stored on page two of the form, after the Page Break control, and are moved around when the form is loaded.
Also notice in Figure 10.32 that the Tag property of the first command button contains 1;1. The first 1 represents which toggle button this command button corresponds with; the second 1 refers to the order on the menu. The second command button on the first tab choice will have a 1;2 for its Tag property, and so on.
The next items to examine are the various code routines used. All the code is behind the ManipulatingControlsExample form.
Before looking at the individual routines, look at the declarations section, which contains various dimension statements:
Option Compare Database 'Use database order for string comparisons Option Explicit Option Base 1 Dim snpMenus As Recordset Dim aryCurrTools() As String Dim dblTopStorage As Double
The Option Base 1 line sets to 1 the base for arrays used for this form. The following table lists the variables and describes their purposes. Note that all these variables are used throughout the form.
Variable | Purpose |
---|---|
snpMenus | Reference to the Menus table |
aryCurrTools() | Array that contains the tools used on the currently selected tab |
dblTopStorage | Integer that contains the top location to store unused controls |
The first subroutine to look at is attached to the form's OnOpen event. This code establishes the reference to the Menus table with the RecordSet type variable, snpMenus, which is shown in the following code:
Private Sub Form_Open(Cancel As Integer) Set snpMenus = CurrentDb.OpenRecordset("Menus", dbOpenSnapShot) End Sub
The Form_Load routine in Listing 10.11 establishes the first view of controls for the menu.
This routine performs the following steps:
1. |
The Top position is established for storing controls out of the way: dblTopStorage = Me!cmdTool1.Top | ||
2. |
The aryCurrTools array is redimensioned to contain the current list of controls plus the Close button: ReDim aryCurrTools(snpMenus!NumberOfTools + 1) As String | ||
3. |
While the routine loops through the command buttons on the form, if the Tag property for the current control matches 1 (the first choice), the control name is stored in the array. It's stored in the position retrieved from the order portion of the Tag property: For i = 1 To Me.Tag Set ctlCurrTool = Me("cmdTool" & LTrim(Str(i))) '-- If the tool is used on the first menu pull it in '-- to the active tool array If GetToolMenu(ctlCurrTool.Tag) = 1 Then aryCurrTools(GetToolOrder(ctlCurrTool.Tag)) = _ ctlCurrTool.Name End If Next Listing 10.12 shows the code for the GetToolMenu() function; Listing 10.13 shows the code for the GetToolOrder() function. Listing 10.12. Chap10.mdb: Returning the Menu-Level Portion of the Tag Property
Listing 10.13. Chap10.mdb: Getting the Current Order of Current Command Buttons from the Tag Property
| ||
4. | |||
5. |
Listing 10.14 shows the final routine, SetToolForm, which runs through the array of controls for the current option group choice. Each control is positioned beside the previous control. Listing 10.14. Chap10.mdb: Arranging Command Buttons Programmatically |
These routines are run when the form is first opened. Some of these routines are also used when a new toggle button is chosen. The main routine, shown in Listing 10.15, is attached to the After Update event of the option group control named optTabs.
Because this routine is like the Form_Load routine in Listing 10.11, you can follow what's happening. One difference between this routine and the Form_Load routine is that this routine calls the ClearCurrTools routine to move the previous command buttons off the page, shown in Listing 10.16.
Private Sub ClearCurrTools() '-- This routine clears the previous tab choice by '-- storing them on the second page Dim intIndex As Integer For intIndex = 1 To UBound(aryCurrTools) Me(aryCurrTools(intIndex)).Top = dblTopStorage Next End Sub |
The other difference is that the optTabs value is used rather than the hard-coded value of 1 in the Form_Load routine.
If you open the ManipulatingControlsExample form and click the three toggle buttons, you'll see how the controls change with the option group. You can create many other form types by using methods like this one.
18.218.168.16