none
How can we identify and change the active TabControl tab with VBA? RRS feed

  • Question

  • At some point during the user's interaction with a form, I would like to switch which tab is active on a TabControl on that form. It would also be helpful to identify the currently active tab of that control.

    Is this possible?

    Sunday, May 12, 2019 5:40 AM

Answers

  • You can use the Value property of the tab control for this purpose. It is the zero-based index of the active tab.

    So to identify the active tab:

        Dim ActiveTab As Long
        ActiveTab = Me.MyTabControl.Value

    If ActiveTab is 2, the 3rd tab is currently active. If you want to activate the 1st tab, use

        Me.MyTabControl.Value = 0


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Sunday, May 12, 2019 8:15 AM
  • A tab control has a Pages collection. To get information about the active tab page, use

    Me.MyTabControl.Pages(Me.MyTabControl.Value)

    A page has (among others) a Name property and a Caption property.

    If Caption has been set, that will be displayed in the tab, but if it is empty, the Name will be displayed. So if you want to get the displayed caption, you have to inspect both Caption and Name:

        Dim strText As String
        With Me.MyTabControl.Pages(Me.MyTabControl.Value)
            strText = .Caption
            If strText = "" Then
                strText = .Name
            End If
        End With


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Sunday, May 12, 2019 9:16 PM

All replies

  • You can use the Value property of the tab control for this purpose. It is the zero-based index of the active tab.

    So to identify the active tab:

        Dim ActiveTab As Long
        ActiveTab = Me.MyTabControl.Value

    If ActiveTab is 2, the 3rd tab is currently active. If you want to activate the 1st tab, use

        Me.MyTabControl.Value = 0


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Sunday, May 12, 2019 8:15 AM
  • Is there a TabPage collection that can be accessed? To get the caption value of the active tab?
    Sunday, May 12, 2019 8:59 PM
  • A tab control has a Pages collection. To get information about the active tab page, use

    Me.MyTabControl.Pages(Me.MyTabControl.Value)

    A page has (among others) a Name property and a Caption property.

    If Caption has been set, that will be displayed in the tab, but if it is empty, the Name will be displayed. So if you want to get the displayed caption, you have to inspect both Caption and Name:

        Dim strText As String
        With Me.MyTabControl.Pages(Me.MyTabControl.Value)
            strText = .Caption
            If strText = "" Then
                strText = .Name
            End If
        End With


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Sunday, May 12, 2019 9:16 PM