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 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.
Strategic Overview
Microsoft Access within an Organization's Database Strategy
How many simultaneous Microsoft Access users?
Blaming Microsoft Access instead of the Developer
Microsoft Access Version Feature Differences
Microsoft Access Versions, Service Packs and Updates
Microsoft Office 365 Access Update Version Releases
Top 14 Features Added with MS Access 2007
Taking Over Legacy MS Access Databases
Winner of Every Best Access Add-in Award
Set AutoNumber Starting Number Other than 1
Avoid Unnecessary or Duplicate Indexes
Copy Command Button and Keep Picture
Module VBA to Forms and Controls
Subform Reference to Control Rather than Field
Suppress Page Headers and Footers on the First Page of Your Report
Annual Monthly Crosstab Columns
Add Buttons to the Quick Access Toolbar
Collapse the Office Ribbon for more space
Avoid Exits in the Body of a Procedure
Send Emails with DoCmd.SendObject
Error Handling and Debugging Techniques
Error Number and Description Reference
Remote Desktop Connection Setup
Terminal Services and RemoteApp Deployment
Missing Package & Deployment Wizard
Remove 'Save to SharePoint Site' Prompt from an Access Database
Class Not Registered Run-time Error -2147221164
Microsoft Access to SQL Server Upsizing Center
When and How to Upsize Access to SQL Server
SQL Server Express Versions and Downloads
Deploying MS Access Linked to SQL Azure
SQL Server Azure Usage and DTU Limits