VB Tutor VB.NET 2022 Tutorial VB2019 Tutorial VB6 Tutorial VB Sample Code About Us
Visual Basic Sample Code

Visual Basic Sample Code

Comprehensive collection of VB6 and VB.NET examples for developers


Visual Basic 6, a third-generation event-driven programming language launched by Microsoft in 1991, offers boundless possibilities for application development. Whether you aspire to create educational apps, financial tools, games, multimedia experiences, animations, or robust database applications, VB 6 empowers you to reach for the stars in your programming endeavors.

Explore a wealth of sample codes meticulously crafted by the vbtutor.net team; they serve as valuable references for your assignments and projects.

Why Learn Visual Basic?

Rapid Development

Build applications quickly with VB's intuitive drag-and-drop interface

Beginner Friendly

Perfect language for those starting their programming journey

Real-World Applications

Create practical business and productivity tools

Proven Legacy

Millions of VB applications still power businesses worldwide

Visual Basic Sample Code Book

VISUAL BASIC PROGRAMMING WITH CODE EXAMPLES

A Practical Guide authored by Dr. Liew Voon Kiong for Beginners and Intermediate Developers to Master Visual Basic programming—From VB6 to VB.NET

Unlock the power of Visual Basic with this comprehensive, hands-on guide that bridges the gap between classic VB6 and modern VB.NET programming. Whether you're maintaining legacy systems or developing cutting-edge Windows applications, this book gives you the knowledge, tools, and inspiration to succeed

Visual Basic Programming with Code Examples offers a unique dual-format approach, showcasing sample codes in both Visual Basic 6 (VB6) and VB.NET. This side-by-side presentation helps you understand the evolution of Visual Basic and empowers you to work confidently across both environments.

    🔍 What You’ll Learn:

  • ✅ Core Concepts Made Easy: Explore data types, control structures, file handling, procedures, user interface design, and more.
  • ✅ Hands-On Application Building: Design real-world applications, including financial calculators, educational tools, games, multimedia apps, and database systems.
  • ✅48 Practical Code Examples: Study and customize fully explained programs that illustrate key programming techniques.
  • ✅ Dual-Code Format: Learn to translate and adapt code between VB6 and VB.NET seamlessly.

📈 Why Choose This Book?

  • 💡 Beginner-Friendly Explanations: Clear, concise writing with high-quality diagrams and illustrations.
  • 🖥️ Side-by-Side Code Samples: See the differences and similarities between VB6 and VB.NET in one place.
  • 🎯 Designed for Learners and Professionals: Whether you're starting out or enhancing your existing skills, this book has something for you.
  • ✨ Practical Focus: Build knowledge through experience, not just theory.
  • 👉 Ideal for students, developers, hobbyists, and IT professionals.

Whether you're a beginner or looking to refine your programming skills, this book simplifies learning through hands-on examples. Ideal for students, educators, and hobbyists.

Buy Now on Amazon Kindle Edition

Featured Sample Code Examples

BMI Calculator

A simple Body Mass Index calculator with visual indicators.

Private Sub CalculateBMI()
    Dim height As Double
    Dim weight As Double
    Dim bmi As Double
    
    ' Get user input
    height = Val(txtHeight.Text) / 100 ' Convert cm to meters
    weight = Val(txtWeight.Text)
    
    ' Calculate BMI
    If height > 0 Then
        bmi = weight / (height * height)
        lblBMI.Caption = Format(bmi, "0.00")
        
        ' Display category
        Select Case bmi
            Case Is < 18.5
                lblCategory.Caption = "Underweight"
                lblCategory.ForeColor = vbBlue
            Case 18.5 To 24.9
                lblCategory.Caption = "Normal weight"
                lblCategory.ForeColor = vbGreen
            Case 25 To 29.9
                lblCategory.Caption = "Overweight"
                lblCategory.ForeColor = vbOrange
            Case Else
                lblCategory.Caption = "Obesity"
                lblCategory.ForeColor = vbRed
        End Select
    Else
        MsgBox "Please enter valid height", vbExclamation
    End If
End Sub

This example demonstrates form controls, calculations, and conditional logic.

Simple Database Viewer

Connect to an Access database and display records.

Private Sub LoadData()
    Dim conn As ADODB.Connection
    Dim rs As ADODB.Recordset
    Dim sql As String
    
    ' Initialize connection
    Set conn = New ADODB.Connection
    conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\data\mydb.mdb;"
    conn.Open
    
    ' Create SQL query
    sql = "SELECT * FROM Customers"
    
    ' Execute query
    Set rs = New ADODB.Recordset
    rs.Open sql, conn, adOpenStatic, adLockReadOnly
    
    ' Display data in listbox
    lstCustomers.Clear
    If Not rs.EOF Then
        rs.MoveFirst
        Do While Not rs.EOF
            lstCustomers.AddItem rs!CustomerID & " - " & rs!CompanyName
            rs.MoveNext
        Loop
    Else
        MsgBox "No records found", vbInformation
    End If
    
    ' Clean up
    rs.Close
    conn.Close
    Set rs = Nothing
    Set conn = Nothing
End Sub

This example shows how to connect to a database and retrieve records using ADO.

Visual Basic Programming Summary

Visual Basic remains a valuable skill despite newer languages because:

Tip: Modern VB.NET continues the Visual Basic legacy with full .NET framework support while maintaining much of VB6's syntax and approachability.

Practice Exercises

Test your Visual Basic skills with these practical exercises:

Exercise 1: Temperature Converter

Create a program that converts between Celsius and Fahrenheit. The program should:

  • Have two text boxes for input (Celsius and Fahrenheit)
  • Update the other temperature automatically when the user types in one field
  • Include a button to swap conversion direction
  • Display a color-coded indicator for extreme temperatures

Formulas:
Fahrenheit = (Celsius × 9/5) + 32
Celsius = (Fahrenheit - 32) × 5/9

Exercise 2: Contact Manager

Create a simple contact management system with the following features:

  • Form to input name, phone, email, and address
  • List to display all contacts
  • Search functionality to find contacts by name
  • Ability to save contacts to a text file
  • Load contacts from a text file on startup

Advanced: Add editing and deletion capabilities for existing contacts.

About Us

Dr. Liew Voon Kiong, the founder and webmaster of VBTutor.net, holds a Bachelor's Degree in Mathematics, a Master's Degree in Management, and a Doctorate in Business Administration (DBA) from the University of South Australia.

With a lifelong passion for programming and education, Dr. Liew has been actively involved in the software development and academic community for decades. In 1996, he launched the renowned Visual Basic Tutorial website, which has since become a top-ranked resource for Visual Basic learners around the world, attracting millions of visitors.

In addition to his online tutorials, Dr. Liew has authored multiple best-selling books on Visual Basic, including the popular title Visual Basic 6 Made Easy, published by Creativespace.com, an Amazon publishing platform. His work continues to empower students, hobbyists, and professionals to learn programming in an approachable and practical way.