Visual Basic 6.0 Guide
Variables - Working With Data

What Is A Variable?

When your program runs, the data that it uses is stored in main memory (RAM). The programmer makes up a name (a variable) to identify where this information is stored. The value we give to a variable can vary depending on how we write our program.

Data Types

When we use variables, we need to think about the type of data that we want to store. In Visual Basic, we will need to use the following data types.

Data TypeWhat Is StoredPossible ValuesStorage
IntegerWhole Numbers-32768 to +327672
LongWhole Numbers-2biilion to + 2billion (approx)4
SingleDecimal NumbersVery small to Very Large4
CurrencyMonetary values (Up to 4dp)Very large range of numbers8
StringOne or more charactersAny characters1 per char
DateDates and timeDates and times8
BooleanTrue or falseTrue, False2

You will notice that a different data types require different amounts of storage space. This is because Visual Basic stores each data type in a different way.

Declaring A Variable

To declare a variable you must tell Visual Basic what you want to call it and what type of data you want to store. You use the keywords Dim and As to declare a variable.

For example.

Dim intNumber As Integer
Dim curPayementAmount As Currency
Dim dtPayemnt As Date

Try to use meaningful names for your variables. The variable declarations above use a prefix of 2 or 3 letters to show the data type. You cannot use spaces in the names of variables.

Using Variables In Your Code

1. Start a new project and get to the code window by clicking on View Code on the menu.

2. You should see the word General in the dropdown box in the top left of the code window.

3. Type the following variable declarations.

Dim intFirstNumber As Integer
Dim intSecondNumber As Integer
Dim intTotal as Integer

4. Return to the form and double click on the Form to get tot the form's Load event code. Type in the following lines.

Private Sub Form_Load()
intFirstNumber = InputBox("Enter your first number")
intSecondNumber = InputBox("Enter your second number")
intTotal = intFirstNumnber + intSecondNumber
Print "The total is ", intTotal
End Sub

5. Save your work and test your program. Remember that there is a limit on the size of the Integer data type. Try typing in a six digit number into the one of the boxes. You should get an error like this.

error image

How would you change the program if you needed to add larger numbers together?

Variable Scope

The scope of a variable indicates which parts of the program can use it. There are 2 types of scope, Global and Local.

Global variables are declared at the top of the form in the General section. Global variables can be used in any part of the form.

Local variables are declared inside a procedure like the form's load event. Local variables can only be used inside the procedure where they have been declared.

1. Open a new project and place a command button named cmdOK on the form. Click on View Code and type the following in the General section of the form. This will be a global variable.

Dim strSurname As String

2. Type the following in the form's Load Event.

Private Sub Form_Load()
strSurname = "Green"
Form1.Print "The global variable stores "; strSurname
End Sub

3. Double click the command button to bring up its event procedure. Type in the following.

Private Sub cmdOK_Click()
Dim strSurname As String
strSurname = "Brown"
Form1.Print "The local variable stores "; strSurname
End Sub

4. Run the program. The variable strSurname means something different to the program when the OK button is clicked. When the form is loaded, the global variable is used.