Nazrul
Nazrul

Reputation: 5

How to programatically add and rename tabs in tabstrip of a vba user form?

I've been trying to write a program which will add a new tab on in tabstrip of vba. I need to do this in run-time. I have an "add tab" button to add it. And at the same time it will rename the tab as a series e.g. "Add Tab">> "Nozzle 2", "Add Tab">> "Nozzle 3" and so one. Any help? Following is the code I tried but it is showing type mismatch error, why?- I don't understand. I tried with similar code for multipage and it worked. But why isn't it working for Tabstrip??

Private Sub CommandButton1_Click()
Dim newPage As Tabs
Dim PagesCnt As Long

With Me.TabStrip1
   PagesCnt = .Count
   Set newPage = .Tabs.Add("Nozzle" & (PagesCnt + 1), "Nozzle " & (PagesCnt + 1), PagesCnt)
   UserForm1.TabStrip1.Value = PagesCnt
End With

End Sub

Upvotes: 0

Views: 4794

Answers (2)

DisplayName
DisplayName

Reputation: 13386

Tabs is a collection of tabs, so you can't set it as a single tab

the proper type for a tab control is MSForms.Tab

    Dim newPage As MSForms.Tab

but you don't need it altogether:

Private Sub CommandButton1_Click()
    Dim PagesCnt As Long

    With Me.TabStrip1
       PagesCnt = .Count
       .Tabs.Add "Nozzle" & (PagesCnt + 1), "Nozzle " & (PagesCnt + 1), PagesCnt
       .Value = PagesCnt
    End With    
End Sub

and you can get rid of PagesCnt variable, too:

Private Sub CommandButton1_Click()
    With Me.TabStrip1
      .Tabs.Add "Nozzle" & (.Count + 1), "Nozzle " & (.Count + 1), .Count
       .Value = .Count - 1
    End With
End Sub

Upvotes: 2

ashleedawg
ashleedawg

Reputation: 21639

The example below uses the Style property to specify the appearance of the tabs in MultiPage and TabStrip. This example also demonstrates using a Label. The user chooses a style by selecting an OptionButton.

More information at the source.

Sub OptionButton1_Click()
    Set MultiPage1 = Item.GetInspector.ModifiedFormPages("P.2") _
          .Controls("MultiPage1")
    Set TabStrip1 = Item.GetInspector.ModifiedFormPages("P.2") _
          .Controls("TabStrip1")
    MultiPage1.Style = 0 '0=fmTabStyleTabs
    TabStrip1.Style = 0 '0=fmTabStyleTabs
End Sub

Sub OptionButton2_Click()
    'Note that the page borders are invisible
    Set MultiPage1 = Item.GetInspector.ModifiedFormPages("P.2") _
          .Controls("MultiPage1")
    Set TabStrip1 = Item.GetInspector.ModifiedFormPages("P.2")_
          .Controls("TabStrip1")
    MultiPage1.Style = 1 '1=fmTabStyleButtons
    TabStrip1.Style = 1 '1=fmTabStyleButtons
End Sub

Sub OptionButton3_Click()
    'Note that the page borders are invisible and
    'the page body begins where the tabs normally appear.
    Set MultiPage1 = Item.GetInspector.ModifiedFormPages("P.2")_
         .Controls("MultiPage1")
    Set TabStrip1 = Item.GetInspector.ModifiedFormPages("P.2")_
         .Controls("TabStrip1")
    MultiPage1.Style = 2 '2=fmTabStyleNone
    TabStrip1.Style = 2 '2=fmTabStyleNone
End Sub

Sub Item_Open()
    Set Label1 = Item.GetInspector.ModifiedFormPages("P.2")_
         .Controls("Label1")
    Set OptionButton1 = Item.GetInspector.ModifiedFormPages("P.2")_
         .Controls("OptionButton1")
    Set OptionButton2 = Item.GetInspector.ModifiedFormPages("P.2")_
         .Controls("OptionButton2")
    Set OptionButton3 = Item.GetInspector.ModifiedFormPages("P.2")_
         .Controls("OptionButton3")
    Set MultiPage1 = Item.GetInspector.ModifiedFormPages("P.2")_
         .Controls("MultiPage1")
    Set TabStrip1 = Item.GetInspector.ModifiedFormPages("P.2")_
         .Controls("TabStrip1")

    Label1.Caption = "Page/Tab Style"
    OptionButton1.Caption = "Tabs"
    OptionButton1.Value = True
    MultiPage1.Style = 0 '0=fmTabStyleTabs
    TabStrip1.Style = 0 '0=fmTabStyleTabs

    OptionButton2.Caption = "Buttons"
    OptionButton3.Caption = "No Tabs or Buttons"
End Sub

(Source)

Upvotes: 0

Related Questions