You are on page 1of 32

Variables,Constants and Data types

Variables temporarily stores values during


the execution of an application
Variables have a name and data type
Declare a variable with a Dim statement
Dim VariableName [As DataType ]
Variable declared with the Dim statement
within a procedure exists only as long as the
procedure is executing
Variables,Constants and Data types

When the procedure is completed, the value


of the variable disappears
The value of the variable in a procedure is
Local to that procedure
A variable name
must begin with a letter
must not exceed 255 characters
Variables,Constants and Data types
Data types are
Numeric data types
Integer 32767 -32768
Long 2,147,xxx,xxx
Single 3.4*10**38
Double 1.8*10**308
Currency 999999999999999.9999
Example
Dim Amnt as Double, (Prev,Temp) as Integer
Dim NameAndLName As String
Variables,Constants and Data types

The String Data type


String type data type can be declared as Max
length of a string variable can 32767

Dim Name As string * 24 Fixed length


Dim Adress As string Variable size
Variables,Constants and Data types

The Byte Data Type


If the variable contains binary data, Declare
it as an array of the Byte data type.

Dim Temp(1 to 100) As Byte;


Variables,Constants and Data types

The Boolean Data type


If you have variable that will contain
true/false, or on/off information, you can
declare it to be of type Boolean

Dim Flag As Boolean


Variables,Constants and Data types

The Variant Data type

A variant is capable of storing all system-


defined types of data. You dont have to
convert between these types of data.

Dim SomeValue Variant by default


Variables,Constants and Data types
The Object Data type
Object variables are stored as 32-bit
addresses. A variable declared as Object is
one that can be assigned(using the set
Statement) to refer to any actual object.
Dim ObjDb As Object
Set ObjDb =OpenDatabase(c:\vb5\Biblio.mdb)
Variables,Constants and Data types
Converting Data Types
Visual Basic Provides several conversion functions
you can use to convert values into a specified data type
Conversion Converts an Conversion Converts an
Function expression to Function Expression to
Cbool Boolean Cint Integer
Cbyte Byte CLng Long
Ccur Currency CSng Single
Cdate Date CStr String
CDbl Double Cvar Variant
Variables,Constants and Data types

There are other ways to declare variables


Declaring a variable in the declaration section of a
Form, Standard module, or Class module, rather
than within a procedure, makes the variable
available to all the procedures in the module
Declaring a variable using Public keyword makes it
available throughout your application
Declaring a variable using the Static keyword
preserves its value even when a procedure ends.
Variables,Constants and Data types
To avoid the problem of misnaming
variables place
Option explicit
in the declaration section of a class, form, or
standard module
OR
From
Tools(Options(Editor(RequireVariableDeclarati
on))
Array Declaration

Array allow you to refer to a series of


variables by the same name and to use a
number(an index) to tell them apart
All the elements in an array have the same
data type
Fixed-size arrays
Dynamic arrays(Can change at run-time)
Array Declaration
Declaring Fixed size arraysUsing Dim Statement
Dim Counter(20) As Integer 21 elements
Dim Sums(100 To 120) As Double
Dim Multi(3, 1 To 10, -40 To 50) As Long
LBound and Ubound functions returns upper and
loower limits of a given dimension
For I = LBound(Multi,3) To UBound(Multi,3)
-40 50
Array Declaration

There are 3 ways to declara fixed size arrays


To create Public arrays, use Public statement
in the declaration section of a module
To create a Module-level array, use the private
statement in the declaration section of a
module
To create a local array, use the Private
statement in a procedure
Array Declaration

Dynamic Arrays
Using either Public, Dim, Static
First declare the array declaration
Dim TempArr()
Then using Redim statement
Redim TempArr(X+1)
Control Structures

Decision Structures
Visual basic Procedures can test conditions
and then, depending on the results of that
test, perform different operations.

If .. Then
If .. Then .. Else
Select Case
Control Structures

If .. Then Structure

If condition Then Statement

If condition Then
Statements
endif
Control Structures
If .. Then .. Else Structure
If condition Then
[Statementblock-1]
[ElseIf condition2 Then
[statement-2]]
[Else
[Statemnt-n]]
Endif
Control Structures
Select Case statement
Select Case testexpression
[Case expressionlist1
[statemntblock-1]]
[Case expressionlist2
[statement-2]]
..
Case else
[statementblock-n]]
end select
Control Structures

Loop Structure
Loop structure allow you to execute one or
more lines of code repetitively

Do .. Loop
For .. Next
For Each.. Next
Control Structures

Do .. Loop
Test First Condition

Do [While / Until] Condition


Stataments
Loop
Control Structures
Test Last Condition

Do
Statements
Loop [While/Until] condition
Control Structures

For.. Next condition

For Counter = start To end [Step increment]


statements
Next[counter]
Control Structures

For EachNext
It repeats a group of statements for each element in a
collection of objects or in an array instead of
repeating the statement a specified number of
times
For Each elemen In Group
statements
Next [element]
Scoping Variables

Depending on how it is declared, a variable


is scoped as either a procedure-level or
module-level
Exiting From Loops

Exiting from loops


Ubnormal exiting from loops

Exit For
Exit Do
Exit Sub
String Functions

String Functions
Len(a) :Returns the length of a string
Mid(a1,Start,length) :returns a specified
number of characters(length), from a string a1,
starting position of Start.
Instr(Start,a1,a2) :Returns the first occurrences
of one string(a2), within another string(a1),
starting(Start) position of a2
String Functions

Left(a1,a2) :Returns a specified(a2) number


of characters from the left of string(a1)
Right(a1,a2) :Returns a specified(a2)
number of characters from the Right of
string(a1)
Str(a1) : Returns a string representation of a
number a1
Examples
Select Case Asc(userInput)
Case 65 To 90
Msg = You entered the uppercase letter
Msg=Msg&Chr(Asc(UserInput))&.
Case 97 To 122
Msg =You entered the lower case letter
Msg=Msg&Chr(Asc(UserInput))&.
Case 1 , 3 , 5
Msg=You entered odd number
Case else
Msg=You did not enter a letter or a number.
End Select
Example

You can use multiple expressions or ranges


in each Case clause
Case 1 To 4, 7 To 9, 11, 13
Case is > MaxNumber
Case everything,nuts To soup, TestIteem
Gosub..Return Statement
Branch to, and return from
A subroutine within a procedure
Sub Form_Click()
Dim Num
Num = InputBox(Type A Number)
Gosub Routine
Goto NextPart
Routine:
Num = Num / 2
Return
NextPart :
MsgBox Half Of Your Number is & Num
End Sub
Examples
For I = 1 To 10
For j = 1 to 10


Next j
Next I
Nl = Chr(13) & Chr(10)
For Rep = 5 To 1 Step - 1
For I = Asc(A) To Asc(Z)
Msg = Msg & Chr(I)
Next i
Msg = Msg & Nl
Next Rep
MsgBox Msg

You might also like