Posted by: wasaa | June 22, 2008

Variables & Data Types

Variable- are placeholders used to store values in Main memory

The data types of variables determine how the bit representing these values are stored in the computer memory

Naming a Variable.

No more than 255 characters

They may include only letters, numbers, and underscore (_).The first character must be a

letter.You cannot use a reserved word (word needed by Visual Basic).

Must be unique within the same scope, which is the range from which the variable can be referenced — a procedure, a form, and so on.

Data Type Suffix

Boolean None

Integer %

Long (Integer) &

Single (Floating) !

Double (Floating) #

Currency @

Date None

Object None

String $

Variant None

Data type Description Range





1-byte binary data 0 to 255Integer

2-byte integer – 32,768 to 32,767Long

4-byte integer – 2,147,483,648 to 2,147,483,647 Single

Single 4-byte floating-point number


Negative values

– 3.402823E38 to – 1.401298E – 45

Positive values

1.401298E – 45 to 3.402823E38


8-byte floating-point number


Negative values

– 1.79769313486231E308 to– 4.94065645841247E – 324

Positive values

4.94065645841247E – 324 to 1.79769313486231E308


8-byte number with fixed decimal point


–922,337,203,685,477.5808 to 22,337,203,685,477.5807


String of characters Zero to approximately two billion characters Variant , Date/time, floating-point number, integer, string, or object. 16 bytes, plus 1 byte for each character if a string value.


2 bytes True or False





8-byte date/time value January 1, 100 to December 31, 9999  


4 bytes Any Object referenceVariable declaration

There are three ways for a variable to be declared




Implicit Declaration

Visual Basic automatically creates a variable with that name, which you can use as if you had explicitly declared it. While this is convenient, it can lead to subtle errors in your code if you misspell a variable name.

Explicit Declaration

There are many advantages to explicitly typing variables. we ensure all computations are properly done, mistyped variable names are easily spotted. Visual Basic will take care of ensuring consistency in upper and lower case letters used in variable names. Because of these advantages, it is a good programming practice that declares all variables as explicit. To avoid the problem of misnaming variables, declared explicitly as a variable. To explicitly declare variables. Place this statement in the Declarations. section of a class, form, or standard module:

Option Explicit

From the Tools menu, choose Options, click the Editor tab and check the Require Variable Declaration option. This automatically inserts the Option Explicit statement in any new modules, but not in modules already created;therefore, you must manually add Option Explicit to any existing modules within a project





Global level variables

Global level variables retain their value and are

available to all procedures within an application.

Module level variables are declared in the

declarations part of the general object of a module’s code window.


(It is advisable to keep all global variables in one module.)

Use the Global keyword or Public Keyword:Operators

1. Arithmetic. Operators

2. String-Concatenate Operators

3. Comparison Operators

4. Logical Operators

Arithmetic. Operators



^ Exponentiation

* / Multiplication and division

\ Integer division (truncates)

Mod Modulus

+ – Addition and subtraction

Parentheses around expressions can change precedence.

Operator precedence

If an expression is written 16+20*4-18/3 how would it be evaluated?

There is a set rule to be followed

All operations are carried out left to right.

All operators have associated hierarchies that determine the order of precedence in evaluating the expression.

Comparison Operators

  Operator Comparison> Greater than

< Less than

>= Greater than or equal to

<= Less than or equal to

= Equal to

<> Not equal to

The result of a comparison operation is a Boolean value (True or False).

String-Concatenate Operators

 To concatenate two strings, use the & symbol or the + symbol:



Not Logical not

And Logical and

Or Logical or


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s


%d bloggers like this: