Tutorial 2, Part 2 - Create and Implement the Support_WebTabControl Class
In this part, we'll add the Support module and functions that are used to instance our new Composite Control class. Finally, we'll add the form-level code.
Step 1 - Create the Support Module
- In the VBA Editor window, right click in the Project Explorer panel in the upper left and select
- Rename the module Support_WebTabControl
- Save changes.
Step 2 - Add the Support Module Functions
This step will familiarize you with the process of creating a creation function for a composite control and the essential steps that must be followed. To learn more about the Composite Control Support module creation function,
review this article.
Public Function CreateWebTabControl(Tab_Control As TabControl) As cControl_WebTabControl
Dim WebTC As cControl_WebTabControl
Dim wb As cControl_WebButton
Dim clrSelect As Long
Dim clrHighlight As Long
clrSelect = RGB(0, 0, 255) 'dark blue for selected buttons
clrHighlight = RGB(192, 192, 255) 'light blue for highlighted buttons
'create the advanced tab control
Set WebTC = CompositeControls.CustomControls.GetOrCreate(CreateGenericReference(Tab_Control, ccTabControl), ccControl_WebTabControl)
'Create the event handler for the parent form and attach the new web tab control to it
.AttachToEventHandler CreateEventHandler(CreateGenericReference(.Control.Parent, ccForm), False)
'assign the webbutton collection to the advanced tab control
Set .PageTabs = GetWebButtons(Tab_Control, clrSelect, clrHighlight)
'set up the web buttons to act as an option group
'assign select / highlight colors for the web buttons
'and link the tab control pages to the respective buttons
For Each wb In .PageTabs
wb.ParentEventHandler.Events("OnSelect").AddCallback WebTC, "ShowPage", ccFullSignature
Private Function GetWebButtons(Tab_Control As TabControl, SelectColor As Long, HighlightColor As Long) As Collection
Dim pag As Page
Dim ctl As Control
Dim ctl2 As Control
Dim wb As cControl_WebButton
Dim WebButtons As Collection
Set WebButtons = New Collection
'iterate all pages
For Each pag In Tab_Control.Pages
'iterate all controls in each page
For Each ctl In Tab_Control.Parent.Controls
'check all command buttons
If TypeOf ctl Is Access.CommandButton Then
'if the command button tag matches the page's pageindex, then it's the page's web button
If ctl.Tag = pag.PageIndex Then
'iterate controls looking for the associated label
For Each ctl2 In Tab_Control.Parent.Controls
'if the control is a label
If TypeOf ctl2 Is Access.Label Then
'and the top / left positions match, it's the associated label
If (ctl2.Top = ctl.Top) And (ctl2.Left = ctl.Left) Then
Set wb = CreateWebButton(ctl, ctl2, , HighlightColor, SelectColor)
Set GetWebButtons = WebButtons
Note the following is taking place in the
- Default creation of objects.
- The WebButton objects which act as tabs for the control are returned in a collection from the function
- The collection is assigned to the WebTabControl's
- The collection is established as an option group (that is, only one button may be selected at a time).
- The WebTabControl's ShowPage() callback is assigned to each cControl_WebButton's
The GetWebButtons() function operates on the following assumptions:
- The CommandButton and Label controls that comprise each cControl_WebButton are located (Top and Left) in the same position and are sized (Width and
Height) the same. If this is not true, the function will not create the corresponding cControl_WebButton object.
- The CommandButton for the corresponding Page control has the Page's PageIndex value stored in it's Tag property.
Step 3 - Add the Form-Level Code
In the Startup Form, add the Form_Open() event and add the following line of code to it:
Note that the # symbol indicates the number in the TabControl Name and is not meant to be typed literally.
Finally, save the project, run the Autoexec macro, and play!