How to add a TabControl to an MS Access Form and add pages to it dynamically using VBA

This procedure, AddATabControlToThisFormGeneric, adds a TabControl to an MS Access Form. Tyically developers adds TabControl in a form manually and then adds pages to the tabcontrol by hand. We think that’s a waste of time. So this procedure automates the mechanics, freeing up the developers time to do the design work. This code is part of MAARS. Feel free to copy it, use it in your own program, however, please give proper credit to the MAARS team.

Sub AddATabControlToThisFormGeneric(frmName As String, tabCDashboard As String, frmTitle As String)
Dim frm As Form, ctl As Control, ctlLabel As Control, ctlLabelHeader As Control, ctrlName As String, ctrlSource As String
Dim mdl As Module, fldCounter As Integer, colCounter As Integer, strProc As String
Dim shortButtonL As Integer, LongButtonL As Integer, startPoscontrols As Integer, buttonH As Integer, buttonT As Integer

'   Creation date: May 6 2021 Created by: BA
'   Comments: Part of MAARS
'   Purpose: This sub will add a tab control to a given form.
'   This procedure performs a check before adding the tab control. Same tab control is not added twice

startPoscontrols = 0.7 * 1440 'twips
shortButtonL = 400
LongButtonL = 11.5 * 1440 ' 11.5 inch
buttonH = 4.2 * 1440 ' 4.2 inch
buttonT = 50

DoCmd.OpenForm FormName:=frmName, View:=acDesign
Set frm = Forms(frmName)
Dim procHeader As String, SQLHeader As String, SQLData As String

colCounter = 1
leftPos = frm.WindowLeft + 120
toppos = frm.WindowTop + 100
Set mdl = frm.Module
With frm
'MsgBox .Width
    .Section(0).Height = 4.25 * 1440 ' Set the height of the detail section to 4.25 Inches
    .RecordSelectors = False
    
    If ControlExists(tabCDashboard, frmName) = False Then 'if the tabcontrol does not exist, then create it
    Set ctl = CreateControl(FormName:=frmName, ControlType:=acTabCtl, Section:=acDetail, _
    Left:=startPoscontrols, Top:=buttonT, Width:=LongButtonL, Height:=buttonH)
    ctl.Name = tabCDashboard
   
    Else
    
        Set ctl = frm(tabCDashboard)
        ctl.BorderColor = RGB(255, 0, 0)
        ctl.ForeColor = RGB(255, 255, 0)
        ctl.BackColor = RGB(0, 0, 255)
    End If
           
End With

frm.Caption = "Auto Created - " & frmName
frm.DefaultView = 0 ' Single form


    DoCmd.Save acForm, frmName
    DoCmd.Close acForm, frmName, acSaveYes
    DoCmd.Restore
    DoCmd.OpenForm frmName, acDesign

End Sub

This is a complimentary article written by the MAARS team for the MAARS user community. Code in this article drives the operation of MAARS (MS Access Application wRiting Software). MAARS is an intelligent automation program that speeds up MS Access Application Development by 10x, 20x or 100x times. To learn more about MAARS, click here.

Disclaimer:

Some information included in this article may have been sourced from other publicly available websites and blogs. In such cases, credit goes to those authors for the original ideas and thoughts, but we do take credit for putting valuable information together and improve the efficiency of other office developers.