Using the data types [Part 2] - Visual Basic 6

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.


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.

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. 

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.

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

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

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