Reference 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 Code
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 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
|