Using the tab control on a form

Applies to
Microsoft Office Access 2003
Microsoft Access 2000 and 2002


Most Valuable Professional logo

This article introduces the tab control and addresses some key issues that you might encounter when using it.

In this article

The tab control

Creating a tabbed form

Adding a bound control to a tab

Adding tabs to an existing form

Adding, removing and rearranging tabs

Adding a subform to a tab

Nesting a tab control within a tab

Setting the default tab page

Making a control visible on every tab page

Copying a tab page

The tab control

A tab control contains multiple pages on which you can place other controls, such as text boxes, labels or command buttons. You can add a tab control to a form to give the form a series of tabbed pages. The Employees form in the Northwind sample database that comes with Access is a good example of a tabbed form. The Employees form uses a tab control to divide employee information into two tabs (or pages) — the Company Info tab, displayed on the first tab page, and the Personal Info tab, displayed on the second tab page. Let's take a look at how we would create a form like this from scratch. Later, we'll look at how to convert an existing, non-tabbed form to a tabbed form.

Company Info tab page on the Employees form

Personal Info tab page on the Employees form

Top of Page Top of Page

 Tip    Try Office 2010 In Access 2010, you can create a "navigation form" that provides an intuitive tabbed interface for switching between forms and reports. Watch a video or try Office 2010!

Creating a tabbed form

The steps that follow modify the Northwind sample database. It can be useful to retain an unmodified example of Northwind.mdb, as it is used in many examples and articles. You may wish, therefore, to make a copy of Northwind.mdb and follow along, using the copy instead of the original.

Begin by creating a new, blank form in Design view, bound to the Employees table. There is more than one way to create a new form in Access; for the purposes of this example, we'll use the Insert menu on the main menu bar.

To create the form by using the Insert menu    

  1. In the Database window, select the Employees table.
  2. On the Insert menu, click Form.
  3. In the New Form dialog box (shown below), accept the default Design View from the list in the upper right corner of the dialog box. Because we selected the Employees table before clicking Form on the Insert menu, that table is selected as the default in the drop-down list of tables and queries. Accept this default (or select Employees from the list if you skipped step 1, above) and click OK.

New Form dialog box with Design View selected

In form Design view, if the property sheet is not already visible, display it by clicking Properties on the View menu. Similarly, if the Field List is not already visible, display it by selecting Field List from the View menu. If the Toolbox is not visible, display it by clicking Toolbox on the View menu. At this point, your screen should look something like this (this shows the Toolbox docked on the left):

Form in Design view with property sheet and Field List

Drag the bottom right corner of the form downward to make it a little larger and to give yourself more room to work in. Now select the Tab control in the Toolbox, and click and drag inside the form to draw a tab control, almost filling the detail section of the form, so that it looks similar to this:

Form containing a tab control

To change the caption displayed on the tab of each page, select the page by clicking on it, click the Format tab in the property sheet, and type the new caption in the Caption box.

Page properties for a tab control

Top of Page Top of Page

Adding a bound control to a tab

To create a bound control (text box, combo box, and so on) on a tab, click the tab, select a field from the Field List (to show the Field List, on the View menu, click Field List) and drop it onto the tab. When the dragged field is positioned over a tab page (such as Page 1), that page will be highlighted. If you drop a dragged field while a tab page is not highlighted, the control will be created on the form, but not on a page of the tab control. If this happens accidentally at this stage, delete the newly-created control from the form and try again. Shown below is our form containing several controls created by dragging fields from the field list onto the first tab page (Company Info) of the tab control.

Form in Design view with the Field List displayed

Top of Page Top of Page

Adding tabs to an existing form

You won't always build your tabbed forms from scratch, however. Often, something that began life as a simple form has had additional controls added over time, until it becomes crowded, busy, and difficult to use. At this point you may decide to convert the existing, non-tabbed form to a tabbed form. You can easily do this by placing a new tab control on the form and moving existing controls onto the various pages of the tab control. There is just one "gotcha" that you need to be aware of — drag-and-drop just won't work. If you drag an existing control on the form and drop it onto the tab control, it will not be placed on a page of the tab control — it will, instead, be placed on the form behind the tab control. To move a control onto a page of the tab control, you need to use cut-and-paste instead of drag-and-drop. Select the control you want to move, cut it to the clipboard, select the page of the tab control on which you want to place the control, and then paste the control. The image below shows a non-tabbed Employees form in the process of being converted to a tabbed form.

Employees1 form in Design view

Top of Page Top of Page

Adding, removing and rearranging tabs

When you add a new tab control to a form, the new tab control, by default, is created with two tab pages. You can add additional pages by using either the Insert menu on the main menu bar, or the shortcut menu of the tab control. To add a tab page by using the Insert menu, on the Insert menu, click Tab Control Page. To add a page using the shortcut menu of the tab control, right-click the tab control and click Insert Page from the shortcut menu.

To remove a tab page, select the page and press DELETE on your keyboard, or select the page and click Delete Page on the shortcut menu. The tab page, along with any controls on that page, will be deleted without prompting for confirmation, so ensure that you really want to delete the tab page and that you have selected the page that you want to delete.

To rearrange the order in which pages are displayed, right-click the tab control and click Page Order on the shortcut menu. In the Page Order dialog box (see below), select the page you want to move in the Page Order list and use the Move Up or Move Down buttons to move the page up or down in the list.

Page Order dialog box

Top of Page Top of Page

Adding a subform to a tab

You can use an existing form to quickly create a subform on a tab page. As an example, we'll use the existing Customer Orders Subform1 to create a subform on a new tab page on the Employees form.

  1. Begin by adding a new tab page to the tab control on the Employees form, as described earlier.
  2. Change the Caption property of the new page to Orders.
  3. Keep the Employees form open in Design view, but resize and move the window, if necessary, so that you can see the Database window.
  4. Select Forms in the Database window, find Customer Orders Subform1, and drop it onto the new tab page. (Drag-and-drop works as usual in this scenario, just not when trying to move existing controls from the form onto a tab page).
  5. Delete the label attached to the newly created subform, and resize the subform to almost fill the tab page.

With the subform control selected, look at the Data tab in the property sheet, and notice that Access is smart enough to have automatically set the Link Child Fields and Link Master Fields to EmployeeID — this is what causes the subform to list only orders taken by the current employee. The form and the property sheet should look this in Design view:

Form and subform with subform properties displayed

If you switch to Form view at this stage, you'll see an error message that says "Microsoft Access can't find the field 'Customer Orders Subform2' referred to in your expression." The field cannot be found because the Customer Orders Subform1 form was designed to be used as a subform of the Customer Orders form. The Customer Orders form includes a second subform control named Customer Orders Subform2, and the code behind Customer Orders Subform1 refers to that subform control, but that second subform control doesn't exist in our modified Employees form.

We could just delete or comment out the offending line of code. If we did that, however, the form would no longer work correctly when used with the Customer Orders form, as we originally intended. Our challenge, then, is to avoid this error message when using the subform with the Employees form, while still having it work correctly when used with the Customer Orders form.

Close the Employees form, choosing Yes when prompted to save changes, and open the Customer Orders Subform1 form in Design view. Select Code from the View menu to view the code behind this form. The line that interests us at the moment is the one that reads "Me.Parent![Customer Orders Subform2].Requery". In this context, "Me.Parent" will return a reference to the form that contains the Customer Orders Subform2 as a subform. When that form is the Customer Orders form, the code will execute without generating an error, because the Customer Orders form contains a subform control called Customer Orders Subform2. But when the main form is our modified Employees form, this line of code will cause an error, because the Employees form doesn't contain any such control. To avoid the error, what we need to do is examine the Name property of the Parent object to determine which form it is pointing to:

If Me.Parent.Name = "Customer Orders" Then
  Me.Parent![Customer Orders Subform2].Requery
End If

Here is the complete Form_Current event procedure, with the new lines indicated by comments:

Sub Form_Current()
' This code created by Form Wizard.
  Dim strParentDocName As String

  On Error Resume Next
  strParentDocName = Me.Parent.Name

  If Err <> 0 Then
    GoTo Form_Current_Exit
  Else
    On Error GoTo Form_Current_Err
    If Me.Parent.Name = "Customer Orders" Then  '<-- new line
      Me.Parent![Customer Orders Subform2].Requery
    End If  '<-- new line
  End If

Form_Current_Exit:
  Exit Sub

Form_Current_Err:
  MsgBox Err.Description
  Resume Form_Current_Exit

End Sub

Close the Customer Orders Subform1 form, choosing Yes when prompted to save changes, and then reopen the modified Employees form to confirm that the error message is no longer displayed. If you open the Customer Orders form, you can also confirm that the Customer Orders Subform1 form still works correctly there — it updates the second subform control to display order details for the selected order.

Top of Page Top of Page

Nesting a tab control within a tab

The one form control that can not be placed directly on a tab page is another tab control. But if you really need a tab control within a tab control, you can achieve this effect by using a subform. Create a new form containing the "inner" tab control, and use that form as a subform on a page of the "outer" tab control. To achieve this effect, the Scroll Bars property of the new form has been set to Neither, and the Record Selectors, Navigation Buttons, and Dividing Lines properties have been set to No. The Border Style property of the subform control used to contain this new form within the Employees form has been set to Transparent.

Tab control with a second tab control nested as a subform

Sometimes, you may want to conditionally show or hide certain tab pages, or even disable them. (When a tab page is disabled, you can still select the page but all of the controls on the page are made unavailable). You can do this by setting the Visible or Enabled property of the page to True or False. In addition, you can set these properties programmatically. For example, the following code will hide the tab page named pgeThree and disable the page named pgeTwo if the OpenArgs property of the form contains the text "Hide It".

Private Sub Form_Open(Cancel As Integer)

  If Me.OpenArgs = "Hide It" Then
    Me!pgeThree.Visible = False
    Me!pgeTwo.Enabled = False
  End If

End Sub

Top of Page Top of Page

Setting the default tab page

You may also want to change the default page (the default page is the tab page that is displayed when the form is first opened). You can do this by setting the Value property of the tab control to the numeric index in the Pages collection of the page that you want to display. The Pages collection of the tab control is zero-based, meaning that the first page is page 0, the second is page 1, and so on. So, to show the first page, set the Value property of the tab control to 0. The following example again uses the OpenArgs property of the form to programmatically determine which page should be displayed when the form first opens:

Private Sub Form_Open(Cancel As Integer)

  Select Case Me.OpenArgs
  Case "One"
    Me!TabCt10.Value = 0
  Case "Two"
    Me!TabCt10.Value = 1
  Case "Three"
    Me!TabCt10.Value = 2
  Case Else
    MsgBox "Unrecognized argument - should be 'One', 'Two', or 'Three'"
  End Select

End Sub

Top of Page Top of Page

Making a control visible on every tab page

Sometimes, you may have controls that you want to remain visible on the form, regardless of which tab page is selected — for example, OK and Cancel command buttons. One way to do this is to place the controls on the form, but outside of the tab control. Alternatively, you can make a control available to all pages, while still appearing to be contained within the tab control. To do this, create the control outside of the tab control (for example, in the Detail section), and then drag and drop the control inside the boundaries of the tab control. The control will appear to be contained in the tab control, but will remain visible and available to all tab pages, regardless of which one is selected.

Top of Page Top of Page

Copying a tab page

If you want to use an existing tab page as the basis for a new tab page, you can copy and paste the existing tab page. The only trick to this is that it can be a little difficult to discern which object is selected — the page tab or the tab control that contains it. To select and copy a tab page, you right-click it and then click Copy from the shortcut menu.

To paste the copied tab page into the same tab control, you select the tab control itself by clicking the grey area immediately to the right of the rightmost tab page. Alternatively, you can select any of the controls contained within the tab control, any of the tab pages, or another control (such as a label or text box) contained within the tab control. Paste the copied page by using either the Insert command or the shortcut key combination CTRL+V. If you selected either the tab control itself, or any control contained within the tab control, the copied page will be pasted back as a new page to the right of the rightmost existing tab page. If, on the other hand, you select the form or any control on the form that is not contained within the tab control, Access will automatically create a new tab control containing the new tab page.

Top of Page Top of Page

 
 
Applies to:
Access 2003