Lesson 2: Creating Visual Basic 6 Applications

Learn how to build your first VB6 application with step-by-step instructions and practical examples

Key Takeaway

Building a VB6 application involves two main steps: designing the interface and writing the event-driven code. The VB6 IDE provides all the tools needed to create Windows applications efficiently.

Welcome to Lesson 2 of our Visual Basic 6 Tutorial! In this lesson, you'll learn how to create your first VB6 application from scratch. We'll cover everything from launching the IDE to writing functional code for interactive applications.

2.1 Creating Your First Application

1 Launching the VB6 IDE

Start the Microsoft Visual Basic 6 compiler. In the New Project dialog, select Standard EXE to enter the Visual Basic 6 Integrated Development Environment (IDE). A default form named Form1 will appear.

2 Accessing the Code Window

Double-click on Form1 to open its source code window, as shown below. This is where you'll write your VB6 code.

Visual Basic 6 source code window
Figure 2.1: The VB6 Source Code Window

Understanding the Object and Procedure Boxes

At the top of the code window, you'll find two drop-down lists:

Object Box

Displays the objects on your form (e.g., Form1, Command1, Label1)

Procedure Box

Shows events associated with the selected object (e.g., Load, Click, DblClick)

List of visual basic 6 objects
Figure 2.2: List of Objects
List of visual basic 6 procedures
Figure 2.3: List of Procedures

Writing Your First Code

You don't need to worry about the Private Sub and End Sub statements; just enter your code between them. For example:

Welcome.vb
Private Sub Form_Load()
    Form1.show
    Print "Welcome to Visual Basic tutorial"
End Sub

Output:

Running this code (press F5) will display the message in the form:

Output of welcome message
Figure 2.4: Output of Example 2.1.1

Performing Arithmetic Operations

You can perform arithmetic calculations using the Print statement:

Arithmetic.vb
Private Sub Form_Activate()
    Print 20 + 10
    Print 20 - 10
    Print 20 * 10
    Print 20 / 10
End Sub

Output:

Press F5 or click the run button to see the results arranged vertically:

Output of arithmetic operations
Figure 2.5: Output of Example 2.1.2

Concatenating Strings

Use the + or & operator to join strings:

Using + Operator

Concatenates strings but can cause errors with mixed types

Using & Operator

Safer method that converts non-strings to strings

Concatenation.vb
Private Sub Form_Load()
    Dim A, B, C, D, E As String
    A = "Tom"
    B = "likes"
    C = "to"
    D = "eat"
    E = "burger"
    
    ' Using + operator
    Print A + B + C + D + E
    
    ' Using & operator
    Print A & B & C & D & E
End Sub

Output:

The output of both concatenation methods:

Output of string concatenation
Figure 2.6: Output of String Concatenation

2.2 Steps in Building a Visual Basic Application

1 Design the Interface

  • Add controls (e.g., buttons, labels) to the form
  • Set their properties (e.g., Caption, BackColor, Font)

2 Write Event Procedures

  • Double-click on a control to open its code window
  • Write code that defines behavior during events

Example: Changing Colors Randomly

Create a form with two command buttons and a label:

Command1

Changes the background color

Command2

Changes the foreground color

Label1

Displays the current foreground color

We'll use the Rnd(), Int(), and RGB() functions to generate random colors.

ColorChanger.vb
Private Sub Command1_Click()
    ' Change form background color randomly
    Randomize
    Dim R As Integer = Int(Rnd() * 256)
    Dim G As Integer = Int(Rnd() * 256)
    Dim B As Integer = Int(Rnd() * 256)
    Form1.BackColor = RGB(R, G, B)
End Sub

Private Sub Command2_Click()
    ' Change label foreground color randomly
    Randomize
    Dim R As Integer = Int(Rnd() * 256)
    Dim G As Integer = Int(Rnd() * 256)
    Dim B As Integer = Int(Rnd() * 256)
    Label1.ForeColor = RGB(R, G, B)
    Label1.Caption = "Foreground Color Changed"
End Sub

Output:

Running this program allows users to change colors randomly:

Color changer application
Figure 2.7: Color Changer Application

Lesson Summary

In this lesson, you've learned how to create your first VB6 applications:

IDE Navigation

Launched the VB6 IDE and accessed the code window

Basic Programming

Displayed messages, performed arithmetic, and concatenated strings

Application Structure

Understood the two-step process of building VB6 applications

Interactive Features

Created a color-changing application with random color generation

You've now built functional VB6 applications and are ready to explore more advanced topics in the next lesson.

Next Lesson

Ready to learn how to work with VB6 controls? Continue to Lesson 3: Working with Controls.

Related Resources

Full VB6 Tutorial Index

Complete list of all VB6 lessons with descriptions

Explore Tutorials

Visual Basic Examples

Practical VB6 code samples for real-world applications

View Examples

Using Controls in VB6

Learn how to work with buttons, textboxes and other UI elements

Learn More

Visual Basic 2022 Tutorial

Modern VB.NET programming guide with latest features

Explore VB2022