VB6 Tutorial 55: Using Data Types

By | 2018-05-28

Visual Basic 6 is rich in data types. So you should know the usage of all the data types to make a powerful application.

Integer values

Among the data types for storing integer values, Integer is the most used data type for its efficiency. But sometimes you may use the Long data type instead of Integer especially when you want to reduce the overflow error as it can hold a very large value, from -2,147,483,648 to 2,147,483,647 where an Integer can only store a value from -32,768 to 32767.

For small integer values, use the Byte data type, the value range is from 0 to 255 only in this case.

The Boolean Data Type

The Boolean variables stores only 0 and 1 that stand for False and True respectively. To use a Boolean variable in your code, firstly declare the variable as Boolean and then assign a True/False value.

Example

Private Sub cmdCheck_Click()
    Dim TextOff As Boolean    'Declaring the variable

    'Assigning value
    If Text1.Visible = False Then
        TextOff = True
    End If

    'Using the Boolean variable
    If TextOff = True Then
        MsgBox "TextBox is hidden"
    Else
        MsgBox "TextBox is shown"
    End If

End Sub

Can you guess what the output of this script is?

Floating point values

Single and Double are the mostly used data types. For small decimal values, use the Single data type and Double for large values. Decimal is a floating point data type. The Decimal data type is higher in precision and lower in range compared to Double. In case of Decimal data type, you can’t explicitly declare it (As Decimal). To use it, convert a value to Decimal data type with the CDec function and assign it to a variant.

Example

The Decimal Data Type Dim num As Variant expr = 22.45 * 5.987 num = CDec(expr)

The String Data Type

String data types can be variable length or fixed-length. When you know the length of your string value, you may use the fixed-length data type. But remember that variable length strings are faster than the fixed-length strings.

Example

Dim s As String             'variable length string
Dim s1 As String * 10       'fixed-length string

The fixed-length string in the above example takes 10*2=20 bytes memory space.

The Currency Data Type

The value range of a Currency data type from -922,337,203,685,477.5808 to 922,337,203,685,477.5807. Currency values always include 4 decimal digits. This data type is used for storing monetary values.

The Date Data Type

The Date data types are useful in the sense that they can be used to perform math operations on date values. You can truncate, add or subtract the date values. You first need to assign the date value to a variable for that.

Example

Private Sub cmdShow_Click()
    Var = Now
    Var = Var + 14    '14 days from today
    Print Var
End Sub

The Object Data Type

Visual Basic is fully an object-based programming language. So Object data type plays a very important role in the language. The Object data type is used to store a reference object. There are several objects in visual basic like forms, textbox, database and so on.

You first need to declare the object variable and then assign an object reference to the object variable using the Set keyword.

After assigning, you can use the object variable to access the properties and methods of the original object.

Example

'Declaration
Dim frm As Form     
Dim txt As TextBox

'Assign object reference
Set frm = Form1      
Set txt = Text1

'Using object variable to access the original object
frm.BackColor = vbGreen   
txt.Text = "hello world"

Clearing a group of TextBox controls

Create a control array of text boxes with the name “Text1” and then write the following code.

Example

Private Sub cmdClear_Click()
    Dim field As TextBox   'Declaring as Object

    For Each field In Text1  'Text1 is a control array
        field.Text = ""
    Next
End Sub

This is just a brief discussion on Object data type. You’ll learn more later in the tutorial.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.