Refer to tab control objects in VBA

Use a tab control to present several pages of information about a single form. A tab control is useful when your form contains information that can be sorted into two or more categories.

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 

Refer to the Pages collection

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 

Refer to and change the current page

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 available only in VBA 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 by 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.

Refer to controls on a tab control page

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 is unique with respect to all other controls on the same form. Refer to controls on a tab control page by using the same syntax for controls on a form without a tab control.

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.

Sub ListTabControlControls() 
 
   Dim tabCtl As TabControl 
   Dim ctlCurrent As Control 
 
On Error GoTo ErrorHandler 
 
   ' 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 ctlCurrent In tabCtl 
      Debug.Print ctlCurrent.Name 
   Next ctlCurrent 
 
   Set tabCtl = Nothing 
   Set ctlCurrent = Nothing 
 
   Exit Sub 
 
ErrorHandler: 
   MsgBox "Error #: " & Err.Number & vbCrLf & vbCrLf & Err.Description 
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.

Sub ListPageControls() 
 
   Dim tabCtl As TabControl 
   Dim pagCurrent As Page 
   Dim ctlCurrent As Control 
   Dim intPageNum As Integer 
 
On Error GoTo ErrorHandler 
 
   ' 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 pagCurrent In tabCtl.Pages 
      intPageNum = intPageNum + 1 
      Debug.Print "Page " & intPageNum & " Controls:" 
      For Each ctlCurrent In pagCurrent.Controls 
         Debug.Print ctlCurrent.Name 
      Next ctlCurrent 
      Debug.Print 
   Next pagCurrent 
 
   Set tabCtl = Nothing 
   Set ctlCurrent = Nothing 
   Set pagCurrent = Nothing 
 
   Exit Sub 
 
ErrorHandler: 
   MsgBox "Error #: " & Err.Number & vbCrLf & vbCrLf & Err.Description 
End Sub

Support and feedback

Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.