Lesson 5: Managing VB Data

Master VB6 data management to create efficient applications with proper data handling and storage techniques

Key Takeaway

Understanding VB6 data types and proper variable management is crucial for creating efficient, error-free applications. Mastering data types helps optimize memory usage and prevent runtime errors.

Welcome to Lesson 5 of our Visual Basic 6 Tutorial! In this lesson, you'll learn how to effectively manage data in VB6 applications. We'll cover data types, variables, constants, and scope with practical examples you can implement.

5.1 Understanding Visual Basic 6 Data Types

Data types in VB6 are essential for managing information effectively in applications. They are broadly categorized into numeric and non-numeric data types. This classification helps developers write efficient and accurate code.

5.1.1 Numeric Data Types in VB6

Numeric data types support mathematical operations. These include:

1 Integer Types

Byte: Unsigned 8-bit number (0 to 255)
Integer: 16-bit signed integer (-32,768 to 32,767)
Long: 32-bit signed integer (-2.1B to 2.1B)

2 Floating-Point Types

Single: 32-bit floating-point
Double: 64-bit floating-point
Currency: 64-bit fixed-point
Decimal: 96-bit high-precision

Important Note

Always choose the smallest data type that can accommodate your values to conserve memory. Use Currency for financial calculations to prevent rounding errors.

5.1.2 Non-Numeric Data Types in VB6

These types do not support mathematical operations. Common examples include:

String

Text values, can be fixed or variable length

Date

Stores date and time values

Boolean

True/False values

Variant

Flexible but less efficient

5.1.3 Using Suffixes for Literals in VB6

Suffixes specify the data type of a literal value, allowing more accurate computations:

Suffix Data Type Example
& Long 12345&
! Single 3.14!
# Double 3.1415926535#
@ Currency 19.99@

Also, enclose string literals in quotes and date literals within # symbols.

Literals.vb
Private Sub Form_Load()
    Dim memberName As String
    Dim TelNumber As String
    Dim LastDay As Date
    Dim ExpTime As Date
    
    memberName = "Turban, John"
    TelNumber = "1800-900-888-777"
    LastDay = #12/31/2000#
    ExpTime = #12:00 AM#
End Sub

5.2 Working with Variables in VB6

Variables are placeholders for storing data in memory. You must name and declare them correctly for effective program design.

5.2.1 Naming Rules for Variables

Follow these rules when naming variables in VB6:

No Spaces

Variable names cannot contain spaces

Start with Letter

Must begin with a letter

No Special Characters

Avoid !, @, &, $, etc.

Length Limit

Must be less than 255 characters

Examples:

Valid Names Invalid Names
My_Car My.Car
ThisYear 1NewBoy
Long_Name_Can_beUSE He&HisFather

5.2.2 Declaring Variables Explicitly

Use Dim for local variables, and combine multiple declarations using commas:

VariableDeclaration.vb
Private Sub Command1_Click()
    ' Declare multiple variables on one line
    Dim name As String, age As Integer, score As Double
    
    ' Fixed-length string declaration
    Dim yourName As String * 10
End Sub

5.2.3 Variable Scope in VB6

Variable scope determines where in your program a variable can be accessed:

1 Local Scope

Declared with Dim within a procedure
Only accessible within that procedure

2 Module Scope

Declared with Private in declarations section
Accessible to all procedures in the module

3 Global Scope

Declared with Public in declarations section
Accessible throughout the application

4 Static Variables

Declared with Static
Retains value between procedure calls

5.3 Working with Constants

Constants hold values that do not change during program execution. Declared using the Const keyword.

Constants.vb
' Declare a constant for Pi
Const Pi As Single = 3.142

' Declare a string constant
Const AppName As String = "VB6 Data Manager"

Practical Example: Calculate Area of a Circle

This program calculates the area of a circle using a constant and variables. The user can resize the circle shape and calculate its area.

CircleArea.vb
' Declare variables and constant
Dim h, r, a, rad, area As Single
Const Pi As Single = 3.142

Private Sub CmdArea_Click()
    r = h / 2
    rad = r * 0.001763889  ' Convert to meters
    a = Pi * rad ^ 2       ' Area = πr²
    area = Round(a, 2)     ' Round to 2 decimal places
    
    ' Show result in a message box
    MsgBox ("The Area of the circle is " & area)
End Sub

Private Sub CmdResize_Click()
    ' Get height from user
    h = InputBox("Enter the value of height")
    
    ' Resize the circle shape
    MyShape.Height = h
End Sub

Circle Area Simulation:

Enter circle height:
Circle Area Example Output
Figure 5.1: Circle Area Application Interface

Lesson Summary

In this lesson, you've mastered essential VB6 data management techniques:

Data Types

Learned numeric and non-numeric data types and their appropriate uses

Variable Declaration

Mastered declaring variables with proper naming conventions

Scope Management

Understood variable scope and how to control it with Dim, Private, Public

Constants

Implemented constants for fixed values that don't change during execution

You've now built a solid foundation in VB6 data management and are ready to explore variables in more depth in the next lesson.

Next Lesson

Continue your VB6 journey with Lesson 6: Variables.

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

Writing Code in VB6

Learn how to write effective VB6 program code

Learn More

Visual Basic 2022 Tutorial

Modern VB.NET programming guide with latest features

Explore VB2022