VB2022 VB2019 VB6 VB Sample Code Excel VBA About Us

Lesson 5: Managing Visual Basic 6 (VB6) Data


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:

5.1.2 Non-Numeric Data Types in VB6

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

5.1.3 Using Suffixes for Literals in VB6

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

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

Examples:

memberName = "Turban, John"
TelNumber = "1800-900-888-777"
LastDay = #12/31/2000#
ExpTime = #12:00 AM#
    

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

Examples:

Valid NamesInvalid Names
My_CarMy.Car
ThisYear1NewBoy
Long_Name_Can_beUSEHe&HisFather

5.2.2 Declaring Variables Explicitly

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

Dim name As String, age As Integer, score As Double
    

Fixed-length strings use:

Dim yourName As String * 10
    

5.2.3 Variable Scope in VB6

5.3 Working with Constants

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

Const Pi As Single = 3.142
    

Example: Calculate Area of a Circle

Uses a constant and several variables to compute the circle’s area based on user input.

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
  a = Pi * rad ^ 2
  area = Round(a, 2)
  MsgBox ("The Area of the circle is " & area)
End Sub

Private Sub CmdResize_Click()
  h = InputBox("Enter the value of height")
  MyShape.Height = h
End Sub
    
VB6 Circle Example Output
Figure: Example of Circle Area Output

Lesson Summary: Managing VB Data

  • Visual Basic Data Types: Split into Numeric and Non-Numeric categories.
  • Numeric Types: Byte, Integer, Long, Single, Double, Currency, Decimal.
  • Non-Numeric Types: String, Boolean, Date, Object, Variant.
  • Literals: Use suffixes like # for Double, @ for Currency, etc.
  • Variables: Declared using Dim, follow naming rules strictly.
  • Scope: Determine with Dim, Private, Public, Static.
  • Constants: Use Const to store unchangeable values.
  • Practical Example: VB code for calculating the area of a circle.

Related Resources




Copyright©2008 Dr.Liew Voon Kiong. All rights reserved |Contact|Privacy Policy

Last update:06/02/2025 08:08:11