FMS Home FMS Software Development Team Blog FMS Facebook Page FMS Twitter
Jump: Search:

Microsoft Access Developer Center

Table Design

Query Design

Form Design

Form Tips and Mistakes

Form Navigation Caption

Using a RecordsetClone

Synchronize Two Subforms

Late Bind Tab Subforms

Subform Reference to Control Rather than Field

Tab Page Reference

Shortcut Keys


Combo Box Top Tips

Properties and Validation

Select First Item

Cascading Combo Boxes

Zip, City, State AutoFill

Report Design

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

Add the NoData Event

Annual Monthly Crosstab Columns

Design Environment

Adding Buttons to the Quick Access Toolbar

Collapsing the Office Ribbon for more space

VBA Programming

Using Nz() to Handle Nulls

Avoiding Exits in the Body of a Procedure

Debugging Keys

Setting Module Options

Math Rounding Issues

Source Code Library

Microsoft Access Module VBA Library

Royalty Free VBA 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 Source Safe

Deployment

Runtime Downloads

Simulating Runtime

Prevent Close Box

Disable Design Changes

Broken References

Missing Package & Deployment Wizard

Terminal Services and RemoteApp Deployment

Avoid Program Files

Microsoft Access Front-End Deployment

System Admin

Disaster Recovery Plan

Compact Database

Compact on Close

Database Corruption

Inconsistent Compile Error

Decompile Database

Bad DLL Calling Convention

Converting ACCDB to MDB

Cloud and Azure

Cloud Implications

MS Access and SQL Azure

Deploying MS Access Linked to SQL Azure

Visual Studio LightSwitch

LightSwitch Introduction

Comparison Matrix

Additional Resources

Microsoft Access Help

MS Access Developer Programming

More Microsoft Access Tips

Technical Papers

Microsoft Access Tools

Connect with Us

Email NewsletterEmail Newsletter

FMS Development Team BlogDeveloper Team Blog

Facebook PageFacebook

Twitter with FMSTwitter

FMS Support Site Support Forum

 

 

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

by Luke Chung, President of FMS, Inc.

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

The Problem

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.

The Solution

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.

Sample Database and VBA Code

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.

Additional Resources

Return to the tips page

Feedback

Contact Us  l   Web questions: Webmaster   l   Copyright FMS, Inc., Vienna, Virginia
Celebrating our 27th Year of Software Excellence