VB6 Tutorial 06: Variables and data types

Posted On 2018-03-22 by VB6Boy
Keywords: visual,basic,vb6,visualbasic,tutorial,basic,beginner,Event,Variables,Data,Types
Tags: VB6 Tutorials Windows
Views: 71

Looking for the more of the VB6 Tutorial? Here is the tutorial index.

This lesson is designed to get you familiar with data types and their usage. Very important stuff for memory management in larger programs.



Variable is used to store value temporarily. The value of the variable may vary during the program execution.


Constant is a fixed value that does not change during the program execution. You can define your own constant to use it in your program.

Naming Rules of variables

  • A variable name must begin with an alphabet.
  • It cannot be more than 255 characters.
  • The variable name must not contain any special character like %,&,!,#,@ or $.
  • And finally, it has to be unique within the same scope.

A good practice to get in to is naming your variables with a descriptive prefix. When looking through your code, you'll know at a glance what type of data a particular variable should have in it.

As an example, I use three-letter descriptive prefixes such as these:

Data Type Prefix Example
Byte byt
bytTotalBytes = 1024
Boolean bln
blnIsEnabled = vbTrue
Integer int
intNumberOfEntries = 1000
Long Integer lng
lngBigNumber = 147483748
Single sng
sngPi = 3.14
Double dbl
dblPi = 3.14159265
Date dat
datToday = "03/25/2018"

Data Types

Visual Basic is rich in its data types. Data types are used to declare the variables. At the time of declaration, memory is allocated for the variables. Different data types are used to store different types of values.

Data type memory storage values

Data Type Storage Data Type Storage
Byte1 byte String (variable-length)Length + 10 bytes
Boolean2 bytesString (Fixed-Length)Length of string
Integer2 bytesCurrency8 bytes
Long4 bytesDecimal12 bytes
Single4 bytesObject4 bytes
Double8 bytesVariant (numeric)16 bytes
Date8 bytesVariant (text)length +22 bytes

Data types and their value range

Data TypeValue Range
Byte 0 to 255
Integer-32,768 to 32,767
Long-2,147,483,648 to 2,147,483,647
Single-3.402823*10^3 to -1.401298*10^45 for negative values
1.401298*10^-45 to 3.402823*10^38 for positive values
Double -1.79*10^308 to -4.94*10^-324 for negative values
4.94*10^-324 to 1.79*10^308 for positive values
DateJanuary 1, 100 to December 31, 9999
String (Variable length)0 to approximately 2,097,152 characters
String (Fixed length)1 to 65,535 characters
Currency-922,337,203,685,477.5808 to 922,337,203,685,477.5807
Decimal+,-79,228,162,514,264,337,593,543,950,335 if no decimal is used
+,-7.9228162514264337593543950335 (28 decimal places)
ObjectAny object
Variant (numeric)Any value as large as Double
Variant (text)Same as variable length string

Variable Declaration

Depending on where the variables are declared and how they are declared, there are many ways to declare a variable in visual basic. When you declare a variable, memory space for the variable is reserved. This is called memory allocation. Different amount of memory space is reserved for different data types.

You can declare a variable with the 'Dim' keyword.


Dim variable As [Type]


Private Sub cmdSum_Click()
    Dim m As Integer
    Dim n As Integer
    Dim sum As Integer
    m = 10 'm is a variable, 10 is a constant
    n = 30
    sum = m + n
    Print "The sum is " & sum
End Sub

Output : The sum is 40

You can declare many variables in one line as follows and assign multiple variables in one line using ':' operator.

Private Sub cmdSum_Click()
    Dim m As Integer, n as Integer, sum as Integer 
    m = 10 : n = 30 
    sum = m + n
    Print "The sum is " & sum
End Sub

Output : The sum is 40


Declaring multiple variables on a single line is not generally a good practice. Declaration of variables in this manner leads to readability issues.

Implicit declaration vs The Variant Data Type

If you use a variable without declaring its type, it is called a variant variable.


Dim num


Dim num As Variant 

Or, if you choose not to declare a variable then also it is of the Variant data type. So you can use a variable in Visual Basic without declaring it.

The variant data type can store numeric, date/time or string values. This is called implicit declaration. That means, you are declaring the variable implicitly.

But it is not recommended to use implicit declaration and a good programmer will declare the variables explicitly, because it can lead to errors that may not be detected at run time.

Using the Option Explicit statement

The statement 'Option Explicit' is written in general declaration section that reports the user of any undeclared variable showing an error message. That means, if you forget to declare any variable, an error message will be shown reporting it.

Creating your own constants

You can create your own constant to use it in your program. The value of the constant remains unchanged throughout the program.


Private Sub cmdCalculate_Click()
    Const pi = 3.1415926 'or Const pi As Double = 3.1415926
    Dim area As Double, r As Double
    r = 2
    area = pi * r * r
    Print area
End Sub

Output : 12.5663704

Looking for the more of the VB6 Tutorial? Here is the tutorial index.

About the Author

VB6Boy has posted a total of 71 articles.

Comments On This Post

No comments on this post yet!

Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.

Or you can drop a note to the administrators if you're not sure where you should post.

Your IP address is:

Before you can post, you need to prove you are human. If you log in, this test goes away.

Code Links