How to use tabs in visual basic

Updated April 17, 2017

Visual Basic tabs serve four different functions. Tabs provide navigation within forms; serve as space markers for printing text through Visual Basic; act as delimiters between text fields for databases; and display tabbed dialogue boxes in applications. Two controls, "TabStrip" and "SSTab," are part of the Visual Basic array of control objects. Choosing between the TabStrip and SSTab control is a decision the programmer must make depending on the individual properties of each. The TabStrip is more complicated to develop but has more features, while the SSTab control offers faster development.

Open a new project by clicking on the "Standard EXE" template when Visual Basic loads. Double-click on the "TextBox" control in the "Toolbox" in the left panel of the screen, represented by a small square containing the lower-case letters "ab" on the top right of the Toolbox. Repeat this action three more times so you end with four boxes on the form.

Drag the boxes apart since each box added covers up the preceding one. Click on the top box and drag it out of the way. Repeat to separate all four boxes. Click on the first box to display its properties in a column at the right of the screen. Scroll down until you see the "TabIndex" property. This property should be a 1 since the tabs default to the order added.

Keep the "TabIndex" numbers in order if you add or delete boxes, labels, and other objects or change the order of appearance on the form.

Add a "CommandButton" to the form from Section 1 by double-clicking on the icon in the "Toolbox," shown as small rectangle in the second row of controls.

Add the Visual Basic language code to text or numbers that should appear in columns by using the term "vbTab" or "TAB(n)" with "n" representing the exact number of spaces, for more precise spacing. Click on "View/Code" in the top menu of the form created in Section 1. Enter code lines as they appear below:

Private Sub Command1_click()

Dim column 1, column 2, column 3 As String

column 1 = "First Name"

column 2 = "Last Name"

column 3 = "Date of Birth"

Debug.Print column 1 & vbTab & column 2 & vbTab & column 3

End Sub

Click on the "Command 1 button on the form to see the results.

Change the code in Step 2 as follows to demonstrate using Tabs to delimit fields written to a text file, which then can be imported into a database.

Private Sub Command1_click()

Dim column 1, column 2, column 3, tabfile As String

tabfile = "c:\testfile.txt"

column 1 = "First Name"

column 2 = "Last Name"

column 3 = "Date of Birth"

Open tabfile For Output As #1

Print #1, column 1; Tab(20); column 2; Tab(40); column 3

Close #1

Open tabfile For Input As #1

tabfile = Input(LOF(1), 1)

Debug.Print tabfile

Close #1

End Sub

(See References 3)

Save the project created in Section 1, Step 1 with a name of your choosing. Create a new project choosing the "Standard EXE" template. Add two supplementary controls by clicking on "Project," "Components" in the top level Visual Basic menu. Scroll down the list of components to find "Microsoft Tabbed Dialog Control 6.0" and "Microsoft Windows Common Controls 6.0 (SP6)." Click on the "Checkbox" next to each to insert a check mark and "OK" to add them to the "Toolbox."

Locate the "TabStrip" control, a square box with an apostrophe at the top and the "SSTab" control, an icon resembling pince-nez glasses in the "Toolbox." If you have not added any other controls, both should be in the sixth row. Highlighting them will bring up the name. Double-click on each to place them on the form. Drag them apart, placing the "TabStrip" above the "SSTab." Add a "TextBox" by double-clicking this "Toolbox" icon. Drag it below the SSTab.

Right-click on the "TabStrip" control to display its properties. Four tabs appear at the top. Click on the second one, "Tabs. " Each of the tabs on the form itself has an "Index" and a "Caption" property plus others. Enter these captions for each indexed tab, then click "OK."

Index 1: "Login"

Index 2: "New User"

Index 3: Send User Name"

Index 4: Send Password

Double-click on the "TabStrip" control to open the code window and enter these lines of code:

Private Sub TabStrip1_BeforeClick(Cancel As Integer)

Select Case TabStrip1.SelectedItem.Index

Case 4

MsgBox "Insert login and pwd code here."

Case Else

MsgBox "You can create other actions associated with the tabs in this fashion."

End Select

End Sub

Press "F5" to run this part of the application. Try clicking on each of the tabs several times, back and forth. (See References 4)

Follow the process in Step 3, now using the SSTab control. The "TabCaptions" are on the "General" tab. Use the left and right arrows next to "Current Tab" to rename the captions from the default as follows:

Tab 0: "Add new form"

Tab 1: "Display new form"

Tab 2: "Calculator"

Display the code window again and add these lines:

Private Sub SStab1_click(SelectedTab As Integer)

Static Active As Boolean

If Active Then Exit Sub

Active = True

Select Case SelectedTab

Case 0

Text 1 = "Add desired next step to coding."

Case 1

Text 1 = "Add a new page to display in coding"

Case 2

Text 1 = "Add to display a calculator."

End Select

Active = False

End Sub

Press "F5" to run the application and click on the various tabs. (See References 5)


The actual contents of the text displayed in the tabbing sections are examples. By writing more extensive code, you enable to user to display information, run other calculations or programs or whatever actions you want taken by following the tab links. The properties for these controls contain multiple options for display. Explore these to tweak the controls.


The code lines for the SSTab control relating to "Active" cases must be present to prevent recursive steps because of the way the "Click" event fires when selecting one of the tabs. Without this code, the process could keep running.

Things You'll Need

  • Visual Basic 6.0
Cite this Article A tool to create a citation to reference this article Cite this Article

About the Author

Robert Karr has been a writer, indexer, reference librarian, computer programmer and Web designer. He has a Master’s Degree in Library Science. Karr has 30 years experience in reference and research and has been writing professionally for 25 years, focusing on the library, medical and computer areas.