Intro and Basic Syntax

Introduction

Visual Basic is a highly popular language in the commercial world because it allows for the rapid development of Windows based programs. VB is particularly strong at creating front ends for databases. This can be done in amazing time through the use of wizards. This page does not cover all aspects of VB, it does not show how to do the basics like layout a form, neither does it cover all the built in functions, as there is already plenty of help provided for these, and a lot of it is self-evident.
A more limited version of Visual Basic is also included in several other Microsoft Applications such as MS Access. Most of the information here applies to that version.

Data Types:

1. Numeric
ByteStore integer values in the range of 0 - 255
IntegerStore integer values in the range of (-32,768) - (+ 32,767)
LongStore integer values in the range of (- 2,147,483,468) - (+ 2,147,483,468)
SingleStore floating point value in the range of (-3.4x10-38) - (+ 3.4x1038)
DoubleStore large floating value which exceeding the single data type value
Currencystore monetary values. It supports 4 digits to the right of decimal point and 15 digits to the left

2. String
Use to store alphanumeric values. A variable length string can store approximately 4 billion characters

3. Date
Use to store date and time values. A variable declared as date type can store both date and time values and it can store date values 01/01/0100 up to 12/31/9999

4. Boolean
Boolean data types hold either a true or false value. These are not stored as numeric values and cannot be used as such. Values are internally stored as -1 (True) and 0 (False) and any non-zero value is considered as true.


Operators in Visual Basic:

Arithmetical Operators

Operators
Description
Example
Result
+
Add5+510
-
Substract10-55
/
Divide25/55
\
Integer Division20\36
*
Multiply5*420
^
Exponent (power of)3^327
Mod
Remainder of division20 Mod 62
&
String concatenation"An"&" "&"Apple""An Apple"

Relational Operators

Operators
Description
Example
Result
>
Greater than10>8True
<
Less than10<8False
>=
Greater than or equal to20>=10True
<=
Less than or equal to10<=20True
<>
Not Equal to5<>4True
=
Equal to5=7False

Logical Operators
Operators
Description
OR
Operation will be true if either of the operands is true
AND
Operation will be true only if both the operands are true

Array Functions:
Arrays in Visual Basic are very useful things to store a collection of variables or store records. Visual Basic allows any variable type to become an array.

Array([item], [nthitem])
Turns a list of arguments into an array

Example:
Dim a() as String
a() = Array("5","10","15","20")
MsgBox("My Number Is: " & a(3))
' A message box will popup with the text "My Number Is: 20"

IsArray([expr])
Returns a Boolean value that indicates if a specified variable is an array.

Example:
' Create a 3 element array
Dim a(3) as String
a(0) = "Blue"
a(1) = "Green"
a(2) = "Yellow"
IsArray(a)
' True
' Declare a single variable
Dim b as String
b = "Orange"
IsArray(b)
' False

Join(array, [delimiter])
Joins the array elements using the specified delimiter character(s)

Example:
' Declare variables.
Dim days() as String
Dim a as String
days() = Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat")
' The default delimiter for Join() is a space " ".
a = Join(days())
' a="Sun Mon Tue Wed Thu Fri Sat"
a = Join(days(), ",")
' a="Sun,Mon,Tue,Wed,Thu,Fri,Sat"
a = Join(days(), "##")
' a="Sun##Mon##Tue##Wed##Thu##Fri##Sat"

Split(string, [delimiter], [count], [compare])
Split a string into an array that contains the specified number of substrings. When count is ommited, splits into as many substrings as possible.

Example:
' Declare variables.
Dim days() as String
Dim a as String
a="Sun,Mon,Tue,Wed,Thu,Fri,Sat"
days() = Split(a, ",")
MsgBox(days(3))
' Wed

LBound(array)
UBound(array)
Returns the smallest subscript (or largest for UBound) for the specified array

These functions are very useful when you don't know the size of an array.

Example:
Dim days() as String
Dim a as String
days() = Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat")
a = LBound(days())
' a=0
a = UBound(days())
' a=6 

Mathematical Operators:

Example1:

Dim a As Double, b As Double, c As Double
    a = 12: b = 5
    c = a \ b
msgbox(c)    
Print c
End Sub


Output:
2

Example2:

Dim a as Integer
Dim b as Integer
Dim c as Integer
a=1
b= 2
c = a + b
msgbox(c)
End Sub

Output:
3

IF and IF Else Condition:

Example1:

Dim num1 As Integer
num1 = 30
If num1 > 0 Then
msgbox("The number is positive")
End If

Output:
The number is positive.

Example2:
To print the largest number.
Dim num1 As Integer, num2 As Integer
num1 = 30
num2 = 50
If num1 > num2 Then
msgbox( num1)
Else
msgbox( num2)
End If

Output:
50

Nested IF Else:

Example:
a = Val(InputBox("Enter a no."))
If a > 0 Then
Print "Positive"
ElseIf a < 0 Then
Print "Negative"
Else
Print "Zero"
End If


Input and Output Operation:

Example: (TextBox)

Private Sub Command1_Click()
Dim a As Integer
a = Val(Text1.Text)
a = a + 1
Text2.Text = a
End Sub

Example: (Input Box)

Private Sub cmdTakeInput_Click()
Dim n As Integer
n = InputBox("Enter the value of n : ")
Print n
End Sub

Switch Statement:

Example:
Dim num As Integer
num = Val(Text1.Text)
Select Case num
Case 0
Print "You have entered zero"
Case 1
Print "You have entered one"
Case 2
Print "You have entered two"
Case Else
Print "The number you entered is greater than 2"
End Select

Do While Loop:

Example:
To print from 0 to 9.
Dim num As Integer
num = 0
Do While num < 10
Print num
num = num + 1
Loop

Do Loop While:

Example:
To print from 0 to 10.
Dim num As Integer
num = 0
Do
Print num
num = num + 1
Loop While num <= 10

Do Loop Until:

Example:
'x is incremented until x becomes greater than 10
Dim x As Integer
x = 0
Do
x = x + 1
Loop Until x > 10
MsgBox x

For Next Loop:

Example:
Dim i As Integer
For i = 0 To 10
Print i
Next i


Output:
To print 0 to 10.

Example2:
Dim i As Integer
For i = 0 To 6 Step 2
Print i
Next i

Every time, the value of i is incremented by 2.

Output:
0
2
4

No comments:

Post a Comment