Microsoft Access FormsReference Tab Pages by its PageIndex Rather than Tab Value on Microsoft Access Forms

Provided by Luke Chung, FMS President

Sample database: TabExample.zip (16K)

Tabs are a powerful and easy to place and use on Microsoft Access forms. A tab control contains pages (tabs) with each page identified by its PageIndex property starting with 0. The value determines the order of the tabs.

From VBA, it's common to identify the current page from the value of the tab control (in this example, named tabExample) starting with 0 for the first page, 1 for the second, etc.:

Select Case Me.tabExample.Value
  Case 0
    ' Do something for the first tab
  Case 1
    ' Do something for the second tab
  etc...
End Select

While using the tab's value works, it's not the best over time. If the tabs are reordered (for instance page 0 and 1 are swapped), the values no longer reference the original pages and your code will crash or not work properly. Similarly, if you add/insert or delete tabs, the values no longer reference the pages as expected.

To ensure you always reference the correct page, it's best to name the individual pages, and references the page name and its PageIndex property:

Select Case Me.tabExample.Value
  Case Me.pagExample.PageIndex
    ' Do something for this tab
  Case Me.pagSecondTab.PageIndex
    ' Do something for this tab
  etc...
End Select

With this approach, as long as you don't rename the pages, no matter which order the page is on the tab control, it's properly referenced, and the code you have specifically for that page always works.

The associated sample database shows a trivial example of the problem and solution. Here's the form from the database:

Example of Tabs on a Microsoft Access form
Example of a multi-page tab control adapted from Total Access Emailer

When you click on the tab pages, the tab control's Change event fires and a message box shows the caption of the selected tab.

By changing the Page Reference Option, the message box that is presented is based on using the tab control's value or the PageIndex. The first fails if the tab pages are reordered. The second option always works. You can see this when you press the [Reorder Tabs] button which randomly makes one of the tab pages the first one.

Here's the VBA code for the two ways to reference the pages:

Private Sub tabEmailSettings_Change()
  Dim strCaption As String
  
  If ogOption.Value = 1 Then
    ' Example of using the tab control's value which fails if the tab order is changed
    Select Case Me.tabEmailSettings.Value
      Case 0
        strCaption = Me.pagBasics.Caption
      Case 1
        strCaption = Me.pagTextHeader.Caption
      Case 2
        strCaption = Me.pagTextBody.Caption
      Case 3
        strCaption = Me.pagTextFooter.Caption
      Case 4
        strCaption = Me.pagHTML.Caption
      Case 5
        strCaption = Me.pagAttachment.Caption
      Case 6
        strCaption = Me.pagAuditing.Caption
      Case 7
        strCaption = Me.pagStatistics.Caption
    End Select
  Else
    ' Example of using each tab's page index which works even if the tab order is changed
    Select Case Me.tabEmailSettings.Value
      Case Me.pagBasics.PageIndex
        strCaption = Me.pagBasics.Caption
      Case Me.pagTextHeader.PageIndex
        strCaption = Me.pagTextHeader.Caption
      Case Me.pagTextBody.PageIndex
        strCaption = Me.pagTextBody.Caption
      Case Me.pagTextFooter.PageIndex
        strCaption = Me.pagTextFooter.Caption
      Case Me.pagHTML.PageIndex
        strCaption = Me.pagHTML.Caption
      Case Me.pagAttachment.PageIndex
        strCaption = Me.pagAttachment.Caption
      Case Me.pagAuditing.PageIndex
        strCaption = Me.pagAuditing.Caption
      Case Me.pagStatistics.PageIndex
        strCaption = Me.pagStatistics.Caption
    End Select
  End If
  
  MsgBox "Page selected: " & strCaption & vbCrLf & "Page value: " & Me.tabEmailSettings.Value
End Sub

Note that this example is trivial because you can get the caption of the current page by using this code:

strCaption = Me.tabEmailSettings.Pages(Me.tabEmailSettings.Value).Caption

A more realistic use would be page specific code such as setting up the controls on the page when it is selected the first time but that would complicate the focus of this example.

Check out this resource for Late Binding Subforms on Form Tabs. That example references the page name rather than the index which is another way to do this.

Table Design

Query Design

Form Design

Form Tips and Mistakes

Copy Command Button and Keep Picture

Module VBA to Forms and Controls

Form Navigation Caption

Resync Record in a Subform

Synchronize Two Subforms

Multiple OpenArgs Values

Late Bind Tab Subforms

Subform Reference to Control Rather than Field

Tab Page Reference

Shortcut Keys


Combo Box Top 6 Tips

Properties and Validation

Select First Item

Cascading Combo Boxes

Zip, City, State AutoFill

Report Design

Suppress Page Headers and Footers on the First Page of Your Report

Add the NoData Event

Annual Monthly Crosstab Columns

Design Environment

Add Buttons to the Quick Access Toolbar

Collapse the Office Ribbon for more space

VBA Programming

Basics: Forms and Controls

Run VBA Code from a Macro

Use Nz() to Handle Nulls

Avoid Exits in the Body of a Procedure

Shortcut Debugging Keys

Set Module Options

Math Rounding Issues

Rename a File or Folder

Avoid DoEvents in Loops

Age Calculations

Weekday Math

Send Emails with DoCmd.SendObject

Source Code Library

Microsoft Access Modules Library

Microsoft Access Modules

VBA Error Handling

Error Handling and Debugging Techniques

Error Number and Description Reference

Basic Error Handling

Pinpointing the Error Line

Performance Tips

Linked Database

Subdatasheet Name

Visual SourceSafe

Deployment

Runtime Downloads

Simulate Runtime

Prevent Close Box

Disable Design Changes

Broken References

Remote Desktop Connection Setup

Terminal Services and RemoteApp Deployment

Reboot Remote Desktop

Missing Package & Deployment Wizard

Avoid Program Files Folder

Unavailable Mapped Drives

Microsoft Access Front-End Deployment

System Admin

Disaster Recovery Plan

Compact Database

Compact on Close

Database Corruption

Remove 'Save to SharePoint Site' Prompt from an Access Database

Class Not Registered Run-time Error -2147221164

Inconsistent Compile Error

Decompile Database

Bad DLL Calling Convention

Error 3045: Could Not Use

Converting ACCDB to MDB

SQL Server Upsizing

Microsoft Access to SQL Server Upsizing Center

Microsoft Access to SQL Server Upsizing Center

When and How to Upsize Access to SQL Server

SQL Server Express Versions and Downloads

Cloud and Azure

Cloud Implications

MS Access and SQL Azure

Deploying MS Access Linked to SQL Azure

SQL Server Azure Usage and DTU Limits

Visual Studio LightSwitch

LightSwitch Introduction

Comparison Matrix

Additional Resources

Connect with Us

 

Free Product Catalog from FMS