You can use a tab control to present several pages of information on a single form. A tab control is useful when your form contains information that can be sorted into two or more categories. For example, the Employees form in the Northwind sample application uses a tab control to display two pages of information about employees.
The tab control provides a user interface similar to Windows tabbed dialog boxes. Users switch between pages by clicking the corresponding tab on the top of the page.
You add a tab control to a form in much the same way as you add other controls, except that each page on a tab control contains other controls. A tab control can’t contain other tab controls, but it’s possible to have two or more separate tab controls on a form.
Note When you add a tab control to a form, Microsoft Access assigns default values to the Name property for each page: Page1 and Page2. Each page in a tab control is treated as a separate control on the form, and every control must have a unique value for its Name property. For this reason, if you add a second tab control to a form, Microsoft Access won’t assign Page1 as the default value for the Name property of the first page in the second tab control, or allow you to change the value to Page1. For the same reason, no other control on the same form can have its Name property set to Page1. However, you can use the same text on more than one page’s tab by setting the page’s Caption property.
Û To add a tab control to a form
Microsoft Access adds a tab control with two pages. The Page1 tab is on top.
Note You can also copy controls from another part of a form or from another page and paste them onto a tab control page. However, you can’t drag controls from another part of a form or from another page.
To | Do this |
Change the name of a tab | Right-click the tab whose name you want to change, click Properties on the shortcut menu, and then specify a new name in the Caption property. If you don’t specify a name in the Caption property, Microsoft Access uses the text in the Name property. |
Add or delete pages, or change the page order of tabs |
Right-click the tab, and then click Insert Page, Delete Page, or Page Order on the shortcut menu. You can also insert a page by copying and pasting an existing page. This copies the entire page, including the controls on it. You can also delete a page by clicking the page, and then pressing DELETE. |
Change the tab order of controls on a page | Right-click the page and click Tab Order on the shortcut menu. |
Change the font name, font size, or font style (weight, italic, and/or underline) of all pages |
Right-click the border of the tab control, click Properties on the shortcut menu, and then set the appropriate properties. The property settings you select apply to the fonts on all pages of the tab control. Note The font color is determined by the Color setting for 3D Objects specified on the Appearance tab of the Display Properties dialog box, which is available from Windows Control Panel. |
Note Microsoft Access won’t crop controls when you size the tab control. You may need to move controls before you make the tab control smaller.
You can further customize how a tab control and its pages look and work by setting their properties. Tab control properties affect the way the tab control as a whole looks and works, and in many cases apply to all the pages within the control. For example, you can set the TabFixedHeight and TabFixedWidth properties to set the size of all tabs on a tab control. You can set most tab control properties in the tab control property sheet; however, some properties can only be set or referenced by using Visual Basic. To display the tab control property sheet, right-click the border of the tab control and click Properties on the shortcut menu.
See Also For information on tab control properties that are only available by using Visual Basic, see “Referring to Tab Control Objects in Visual Basic” later in this section.
The following table lists the most commonly used tab control properties. For information on other properties, press F1 when the insertion point is in the property box.
Tab control property | Description |
MultiRow | Specifies whether a tab control can have more than one row of tabs. If the MultiRow property is set to No, Microsoft Access truncates the tabs if they exceed the width of the tab control and adds a scroll bar. The default setting is No. |
BackStyle | Specifies whether the pages in the tab control are transparent. When set to Normal, the color of pages is determined by the 3D Objects color specified on the Appearance tab of the Display Properties dialog box, which is available from Windows Control Panel. When set to Transparent, the color of pages is determined by the BackColor property of the detail section and the Picture property of the form (if any) showing through them. The tabs in a tab control are always solid and use the 3D Objects color set in the Windows Control Panel. The default setting is Normal. |
Style |
Specifies what to display at the top of the tab control. You can display tabs, command buttons (in the same positions as tabs), or nothing. The default setting is Tabs. You may want to display nothing if you want to use command buttons on the form outside the tab control to determine which page has the focus. To do this, set the tab control’s Style property to None. Then add an event procedure to the button’s OnClick event that sets the tab control’s Value property to the page you want to display. See Also For information on how to do this, see “Referring to and Changing the Current Page” later in this section. |
TabFixedHeight | Specifies the height of tabs in inches. When set to 0, each tab is tall enough to fit its contents. The minimum height is .05 inches. The default setting is 0. |
TabFixedWidth | Specifies the width of tabs in inches. When set to 0, each tab is wide enough to fit its contents and, if there is more than one row of tabs, the width of each tab is increased so that each row of tabs spans the width of the tab control. If the setting is greater than 0, all tabs have an identical width as specified by this property. The minimum width is 0.5 inches. The default setting is 0. |
In addition to the properties that apply to the tab control as a whole, there are also properties that apply to individual pages. Tab control page properties affect the way a page looks and works. All tab control page properties can be set in the page property sheet. To display the page property sheet, right-click the tab, and then click Properties on the shortcut menu.
The following table lists the most commonly used tab control page properties. For information on other properties, press F1 when the insertion point is in the property box.
Tab control page property | Description |
Name | Specifies the name of the page. Use this name when referring to a tab control page in Visual Basic. The default name is Page1 for the first page, Page2 for the second page, and so on. |
Caption | Specifies the display text that appears on a tab. If you don’t specify a name in the Caption property, Microsoft Access uses the text in the Name property. |
Picture | Use to add a graphic to a tab. The graphic is displayed to the left of the tab name specified in the Caption property. If you want to display only a picture and no name, enter a space in the Caption property. |
If you’re going to use a tab control in a custom dialog box, you may want to set additional properties so that your form looks and works like a Windows dialog box.
See Also For more information on making a form look and work like a dialog box, see “Using Pop-up Forms and Dialog Boxes” earlier in this chapter.
In most ways, a tab control works like other controls on a form and can be referred to as a member of a form’s Controls collection. For example, to refer to a tab control named TabControl1 on a form named Form1, you can use the following expression:
Form1.Controls!TabControl1
However, because the Controls collection is the default collection of the Form object, you don’t have to explicitly refer to the Controls collection. That is, you can omit the reference to the Controls collection from the expression, like this:
Form1!TabControl1
A tab control contains one or more pages. Each page in a tab control is referenced as a member of the tab control’s Pages collection. Each page in the Pages collection can be referred to by either its PageIndex property setting (which reflects the page’s position in the collection starting with 0), or by the page’s Name property setting. There is no default collection for the TabControl object, so when referring to items in the Pages collection by their index value, or to properties of the Pages collection, you must explicitly refer to the Pages collection.
For example, to change the value of the Caption property for the first page of a tab control named TabControl1 by referring to its index value in the Pages collection, you can use the following statement:
TabControl1.Pages(0).Caption = "First Page"
Because each page is a member of the form’s Controls collection, you can refer to a page solely by its Name property without referring to the tab control’s name or its Pages collection. For example, to change the value of the Caption property of a page with its Name property set to Page1, use the following statement:
Page1.Caption = "First Page"
Note If a user or code changes a page’s PageIndex property, the reference to the page’s index and the page’s position in the page order change. In this case, if you want to maintain an absolute reference to a page, refer to the page’s Name property.
The Pages collection has one property, Count, that returns the number of pages in a tab control. Note that this property is not a property of the tab control itself, but of its Pages collection, so you must explicitly refer to the collection. For example, to determine the number of pages in TabControl1, use the following statement:
TabControl1.Pages.Count
A tab control’s default property is Value, which returns an integer that identifies the current page: 0 for the first page, 1 for the second page, and so on. The Value property is only available in Visual Basic code or in expressions. By reading the Value property at run time, you can determine which page is currently on top. For example, the following statement returns the value for the current page of TabControl1:
TabControl1.Value
Note Because the Value property is the default property for a tab control, you don’t have to refer to it explicitly. For this reason, you could omit .Value
from the preceding example.
Setting a tab control’s Value property at run time changes the focus to the specified page, making it the current page. For example, the following statement moves the focus to the third page of TabControl1:
TabControl1 = 2
This is useful if you set a tab control’s Style property to None (which displays no tabs) and want to use command buttons on the form to determine which page has the focus. To use a command button to display a page, add an event procedure to the button’s OnClick event that sets the tab control’s Value property to the integer that identifies the appropriate page.
By using the Value property with the Pages collection, you can set properties at run time for the page that is on top. For example, you can hide the current page and all of its controls by setting the page’s Visible property to False. The following statement hides the current page of TabControl1:
TabControl1.Pages(TabControl1).Visible = False
Each page in a tab control also has a PageIndex property that specifies the position of a page within the Pages collection using the same numbering sequence as the tab control’s Value property: 0 for the first page, 1 for the second page, and so on. Setting the value of a page’s PageIndex property changes the order in which pages appear in the tab control. For example, if you wanted to make a page named Page1 the second page, you’d use the following statement:
Page1.PageIndex = 1
The PageIndex property is more typically set at design time in a page’s property sheet. You can also set the page order by right-clicking the border of a tab control and then clicking Page Order on the shortcut menu.
The controls you place on a tab control page are part of the same collection as all controls on the form. For this reason, each control on a tab control page must have a name that’s unique with respect to all other controls on the same form. You can refer to controls on a tab control page by using the same syntax used for controls on a form without a tab control. For example, a fully qualified reference to the HomePhone text box on the Personal Info tab of the Employees form in the Northwind sample application would read as follows.
Forms!Employees!HomePhone
Because each control on a form has its own Controls collection, you can also refer to the controls on a tab control as members of its Controls collection. For example, the following code enumerates (lists) all the controls on the tab control of the Employees form in the Northwind sample application. Because the EmployeeName text box in the header section of the form is not a member of this collection, it isn’t listed.
Sub ListTabControlControls()
' Declare object variables.
Dim tabCtl As TabControl, ctl As Control
' Return reference to tab control on Employees form.
Set tabCtl = Forms!Employees!TabCtl0
' List all controls on the tab control in the Debug window.
For Each ctl In tabCtl
Debug.Print ctl.Name
Next ctl
End Sub
Additionally, each page on a tab control has its own Controls collection. By using a page’s Controls collection, you can refer to controls on each page. The following code enumerates the controls for each page of the tab control on the Employees form in the Northwind sample application.
Sub ListPageControls()
' Declare object variables.
Dim tabCtl As TabControl, pg As Page, ctl As Control, intPageNum As Integer
' Return reference to tab control on Employees form.
Set tabCtl = Forms!Employees!TabCtl0
' List all controls for each page on the tab control in the Debug window.
For Each pg In tabCtl.Pages
intPageNum = intPageNum + 1
Debug.Print "Page " & intPageNum & " Controls:"
For Each ctl In pg.Controls
Debug.Print ctl.Name
Next ctl
Debug.Print
Next pg
End Sub
See Also For more information on referring to objects, see “Referring to Objects and Their Values” in Chapter 1, “Creating an Application.” For more information on working with objects and collections in Visual Basic, see Chapter 5, “Working with Objects and Collections.”