Visual Basic Notes
Visual Basic Notes
Roll Number 4
Name Himanshu
Phone 9213222920
Student
Roll Number Name Address Phone
1 Raj c-65, hari nagar 25126633
2 Amit a-45, Ramesh ngr. 25165263
3 Rahul c-96, Janak puri 25545298
• DataTypes in VB
Variant variables
• Literals
String literals:
“raj”, “a-45, hari nagar”, “845.73”, “8-15-2005”
Boolean literals:
True, false
Date / Time literals:
#8-12-2005#, #1:45 AM #
Single / Double literals:
12.5#, -13.80 , +0.73, 85.2!
byte / integer / long literals:
+45, -33, 0, 48, 128, 85%
% Integer
$ string
@ currency
& long
# double
! single
• Comments
The word REM or symbol (‘) tells the visual basic that everything on that line
following REM or (‘) is not the code, it is a comment and should be ignored by the
compiler. REM can appear only in the beginning of the line but apostrophe (‘) can
appear after a statement also.
Example:
Dim x% ‘declares x as integer variable
REM example of a comment.
• Operators
1. Arithmetic Operators
2. String or Concatenation Operators
3. Comparison Operators
4. Logical Operators
• Arithmetic operators
+, -, *, /, \ , mod, ^
2+5 …. 7
“daljeet” + ”singh” …. “daljeetsingh”
“91” + “6” …. “916”
5/2 …. 2.5
11/3 …. 3.66
5\2 …. 2
11\ 3 …. 3
5 mod 2 …. 1
11 mod 3 …. 2
2 ^ 3 = 23 …. 8
3 ^ 3 = 33 …. 27
• Concatenation operators(&)
“Daljeet“ & “singh“ …. “Daljeetsingh”
• Comparison operators
= 2=3 …. false
<> 9< >7 …. true
> 8>5 …. true
>= 5>=5 …. true
< 91<15 …. false
<= 9<=8 …. false
• Logical Operators
Truth Table
A B A AND B A OR B A XOR B NOT A NOT B
False False False False False True True
False True False True True True False
True False False True True False True
True True True True False False False
Events
• Event refers to the occurrence of an activity.
1. Click:- When the user clicks the primary mouse button on an object.
2. Change:- When the user modifies text in combo box or text box.
3. Gotfocus:- When an object receives focus.
4. Lostfocus:-When an object loses focus.
5. Keypress:- The user presses and releases a keyboard key while an object has
focus.
6. Keyup:- When the user releases a keyboard key when an object has focus.
7. Dragdrop:- The use drags an object to another location.
8. Dragover:- The user drags an object over another control.
9. Form_load: - When the form loads for execution in memory.
1. At design time
2. At run time.
Example:
Label1.alignment = 0
Or
Text1.alignment = 1
Example:
Label1.appearance =1
Or
Command1.appearance = 1
Or
Option1.appearance = 0
Example:
Label1.height = 100
Or
Command1.height = 200
Hscroll1.min = 1
Vscroll1.max = 100
Vscroll1.min = 1
• Setting the fillstyle and fillcolor property
Example:
Shape1.fillstyle = 0
Some Examples:
Example1:- Loading a picture in the picture box at run time.
Picture Box
Load
Image control(image1)
Load
Delete Listindex
Clear Display
Hscroll1
Sum
C:\
Abc.dll
Program
Rmn.dll
Files
Pqr.exe
……….
………
……….
………
……….
………
Reset
Timer1 cmdreset
Percentage
90 80
70 60
Click
lblgrade cmdgrade
• Manipulating forms
Load form1
Unload form1
Unload me
Form1.show
Form2.show
Form1.hide
Me.hide
Control Structures in VB
• Control Flow:
1. Sequence
2. Selection
3. Iteration
The if statement:
Syntax:
if (condition) then
statements
end if
Example:
If (a>10) then
Print ”Raj”
End if
Result
cmdresult
Example:
Text1
Enter a number
Print day
cmdprintday
• Nested ifs
Example:
If ( x > 7) then
If( y <= 8) then
Print ”Ravi”
Else
Print ”Amit”
End if
Else
If (P <> 71) then
Print ”vishvas”
Else
Print ”Raj”
End if
End if
• Compound if:
Example:
If ( x > 7 AND y <> 75) then
Print ”Vicky”
Else
Print ”Raj”
End if
Example:
If( x >= 91 OR p <> 83) then
Print ”Rishabh”
Else
Print ”Amrita”
End if
If Select case
1. if x = 1 case1
2. if x > 7 case is > 7
3. if x < 7 case is < 7
4. if x >= 7 AND x <= 20 case 7 to 20
Example 1:
Text1
Enter a number
Print day
cmdprintday
Example 2:
Text1
Result
cmdresult
Example 3:
txtmonth
No. of days
cmdnod
• Looping Structures:
1. For…Next
2. Do loop
(a) Do while…loop
(b) Do…loop while
(c ) Do until…loop
(d) Do…loop until
3. while…wend
1. For…next
Example:
For i = 1 to 10 step 1
Print “Raj”
Next i
Example:
For i = 7 to 22 step 2
Print “abc”
Next i
Example:
For x = 17 to 1 step -3
Print “raj”
Next x
2.(a) Do while…loop
Example:
x=1
Do while (x < = 10)
Print “xyz”
x=x+1
loop
Example:
x = 10
Do while (x > = 1)
Print “xyz”
x=x-1
loop
Example:
x=1
Do while (x < = 20)
Print “raj”
x=x+2
loop
2.(c) Do until…loop
While Until
x < 10 x >= 10
x <= 10 x > 10
x>1 x <= 1
x >= 1 x<1
x < >1 x=1
x=1 x <> 1
Example:
x=1
do until x > 10
print “Raj”
x=x+1
loop
Example:
x = 10
do until x < 1
print “Raj”
x=x-1
loop
Example:
x=1
do
print “Raj”
x=x+1
loop until > 10
Example:
x = 10
do
print “Raj”
x=x-1
loop until x < 1
3. While wend
Example:
x=1
while x <= 10
print “raj”
x=x+1
wend
Example:
x = 10
while x >= 1
print “raj”
x=x-1
wend
• Nested loops
Example:
for I = 1 to 3 step 1
for j = 1 to 5 step 1
print "raj"
next j
next i
Example:
i=1
do while i<=3
j=1
do while j<=5
print "raj"
j=j+1
loop
i=i+1
loop
Types of Procedures
1. Sub Procedures
2. Function Procedures
3. Property Procedures
Print message
cmdprintmessage
Private sub cmdprintmessage_click( )
print "Raj"
print "Daljeet"
disp or call disp
print “ravi”
print "kailash"
call disp
print "vishal"
End sub
Private sub disp( )
print "Harish"
print "Manish"
print "Vicky"
End sub
cmdsum
Private sub cmdsum_click( )
dim x as integer, y as integer
x = val (text1.text)
y = val(text2.text)
call findsum (x, y) or findsum x, y
findsum x …. Error
findsum x, 5, y …. Error
End sub
Private sub findsum (n as integer, m as integer)
text3.text = n + m
End sub
2. Function Procedures: A function is a procedure that performs a specific
task and returns a value.
Types of Functions
1. User Defined Functions
2. Built in Functions or Library Functions
Example:
Result is Text3
Sum
cmdsum
Private sub cmdsum_click( )
dim x as integer, y as integer, z as integer
x = val(text1.text)
y = val(text 2.text)
z = findsum(x, y)
text3.text = z
End sub
Private function findsum (n as integer, m as integer) as integer
findsum = n + m
End function
• Passing Parameters to Procedures
1. Call by Value (pass by value) ……………….Byval
2. Call by Reference (pass by reference) ..……Byref
Example:
Print
cmdprint
Result is Text3
sum
cmdsum
check
cmdcheck
• Library Functions
• String Functions:
1. Lcase and Ucase Functions
Print Ucase(“hello”) - HELLO
Print Lcase (“HELLO”) - hello
Text1.text = Ucase(text1.text)
Text1.text = Lcase(text1.text)
2. Len Function
dim x as integer
x = len(“raj”)
print x - 3
x = len(text1.text)
print x
print len(“Rajeev”) - 6
5. Mid function
dim x as string, y as string
x = “raj kumar arora”
y = mid (x, 5, 5)
print y - kumar
print (x,11, 5) - arora
6. Instr function
dim searchstring as string, searchchar as string, mypos
searchstring = “XXpXXpXXPXXP”
searchchar = “P”
mypos = instr (4, searchstring, searchchar, 1) - 6
mypos = instr(1, searchstring, searchchar, 0) - 9
mypos = instr(searchstring, searchchar) - 9
mypos = instr(searchstring, ”w”) - (0) not found
7. Space Function
dim p as string
p = “raj” & space(10) & ”puri”
print p - raj puri
8. String Function
dim x as string
x = string(10, ”a”)
print x - “aaaaaaaaaa”
x = string (5, ”ABC”)
print x - “AAAAA”
9. Str Function
dim x as string, y as string
x = str(1205) - “1205”
y = str(1305) - “1305”
print 12 + 15 - 27
print str(12) + str(15) - 1215
print “12” + “15” - 1215
10.Asc Function
dim x as integer
x = asc(“A”)
print x - 65
print asc(“Abc”) - 65
x = asc(“a”)
print x - 97
ASCII Values
American Standard Codes For Information Interchange.
“A” to “Z” - 65 to 90
“a” to ”z” - 97 to 122
“0” to “9” - 48 to 57
11.Chr Function
dim x as string
x = chr(65)
print x - A
print chr(66) - B
x = chr(98)
print x - b
12.Strreverse Function
Dim x as string
x = “Neeraj“
Print strreverse(x) - jareeN
Print strreverse (“neeraj”) - jareen
Text1.text = strreverse(text1.text)
• Numeric Functions
1. Int & Fix Functions
print int(14.1) …. 14
print int(14.6) …. 14
print int(-14.1) …. –15
print int(-14.6) …. –15
print cint(14.1) …. 14
print cint(14.6) …. 15
print cint(-14.1) …. –14
print cint(-14.6) …. –15
print fix(14.1) …. 14
print fix(14.6) …. 14
print fix(-14.1) …. –14
print fix(-14.6) …. –14
2. Sgn Function
if number is Sgn function returns
greater than 0 1
equal to 0 0
less than 0 -1
print sgn(-5) …. -1
print sgn(0) …. 0
print sgn(10) …. 1
x = sgn (-5)
print x …. -1
3. Val( ) Function
print 2+5 …. 7
print “2” + ”5” …. 25
print val(“2”) + val(“5”) …. 7
print val(“18th Road”) …. 18
4. Rnd Function
Text1.text = rnd(1)
Or print rnd(3)
To produce a random number within the given range.
int ((upperbound – lowerbound + 1) * rnd + lowerbound)
Example:
(To produce a random number between 1 and 6)
dim x as integer
x = int ((6 – 1 + 1) * rnd + 1)
Or x = int (( 6 * rnd) + 1)
Date Date$
- Variant String
- Uses ”/” Uses “-“
- Do not places 0 Places 0
- Do not appends 19 or 20 Appends 19 or 20
before year before year
Time Time$
- Variant String
- 12 hour 24 hour
4. Datepart Function
Syntax:
Datepart (interval, validdate)
Interval
yyyy year
q quarter
m month
y day of the year
d day
w weekday
ww week
h hour
n minute
s second
Example:
dim x as date
x = #1/17/2006#
Print datepart (“m”, x) - 1
Print datepart(“yyyy”, now) - 2006
5. Day, Month and Year Functions
Print day(date) - 17
Print month (date) - 1
Print year(date) - 2006
dim x as date
x = #1/19/2006#
print year(x) - 2006
print day(x) - 19
7. Timer( ) Function
Print timer
Or Text1.text = timer( )
Datediff Function
Syntax:
Datediff(interval, date1, date2)
Example:
x = #1/17/2006#
y = #2/25/2004#
n = datediff (“yyyy”, x, y)
print n …. 2
• Miscellaneous Functions
1. Isdate( ) Function
dim x as integer, y as string
x = 10
y = “12/17/2005”
print isdate(x) .… false
print isdate(y) .… true
2. Isnumeric( ) Function
print isnumeric(x) …. true
print isnumeric(y) …. false
3. Isempty( ) Function
4. Isnull( ) Function
5. Vartype( ) Function
Example:
dim x as integer, y as string
Print vartype (x) …. 2
Print vartype (y) …. 8
6. Inputbox( ) Function
Syntax:
Inputbox (prompt [,title] [,default] [,xpos] [,ypos]…)
Example:
p = inputbox(“Enter the number“, “My Project”, 10, 100, 200)
7. Msgbox( ) Function
Syntax:
Msgbox (prompt [,buttons] [,title])
Example:
Msgbox (“A Trial Message“)
x = Msgbox (“Do you wish to continue:“, 4 + 32, “My Project”)