Вы находитесь на странице: 1из 44

Front-End And Back-End

Roll Number 4

Name Himanshu

Address WZ-45, Hari Nagar

Phone 9213222920

Save Delete Search

Form / Interface / Front-End

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

Table / Relation / Back-End

• DataTypes in VB

Datatype Storage size Range


1. Byte 1 byte 0-255
2. Boolean 2 bytes True or False
3. Integer 2 bytes -32768 to +32767
4. Long 4 bytes -2147483648 to
+2147483647
5. Single 4 bytes
6. Double 8 bytes
7. Currency 8 bytes
8. Decimal 14 bytes
9. Date 8 bytes Jan 1, 100 to
Dec 31, 9999
10. Object 4 bytes Any object reference
11. String (variable length) 10 bytes + string length 0 to
approx. 2 billion
12. String (fixed length) length of string 1 to approx. 65,400
13. Variant (numeric) 16 bytes
14. Variant (text) 22 bytes + string length

• Declaring & initializing Variables

Dim Num as integer


Num = 2557
Or Num = -235
Or Num = 32 + 7 - 3
Dim Age as byte
Age = 73
Or Age = 12
Dim Name as string …. variable length string
Name = “raj”
Dim Name as string * 15 …. fixed length string
Name = “raj”
Dim Address as string
Address = “A-45, janak puri“
Dim price as single
Price = 75.80
Or Price = 75.25 - 1.85
Dim x as Boolean
x = true
Or x = false
Or x= 5<2 …. false
dim doj as date
doj = #8-15-2005#
dim x as date
x = #12:45 PM#
dim p as variant
Or dim p …. (by default variant)
p = “raj”
Or p = 45
Or p = #8-15-2005#
Or p = true
Or p=45.7
Dim a, b, c as integer

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%

• Type declaration characters

% Integer
$ string
@ currency
& long
# double
! single

Dim x% declares x as integer variable


Dim p$ declares p as string variable
Dim n# declares n as double variable

• 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.

• Variable Default Values

Datatype Default Values


Integer 0
Long 0
Single 0
Double 0
String “ “ blanks/null
Boolean false
Variant empty
Date 0
Currency 0

• Implicit & Explicit Variables Declaration

Dim x, y variant variables


P=100 implicit variable (variant)
Dim m as integer explicit variable
Option explicit only explicit variables can be used

• Defining Named Constants


Syntax:
[public / private] const <constantname> [as type] = expression
Examples:
const pi = 3.14
public const a as integer = 9
const jdate = #2/1/05#
public const nm = ”raj”
public const x = 14, nm = “raj”

• 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

AND, OR, NOT, XOR


(5 > 7 AND 9 <= 81) …. false
false true

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

• The Print Statement


Syntax:
Print “Message Text“
Print constant / variable / expression
Example:
Print “this is visual basic”
Print 10
A = 30
Print A
Print A * 2 / 3
Print using comma:
Print 2, 3, 4, 5, 6
Output:
2 3 4 5 6
Print using semicolon:
Print 1;2;3;4
Output:
1234
print “hello“;“friends“
Output:
hellofriends
• Controls & their Properties

Control Name Property Value


Label Name
Appearance 0 – flat
1 – 3D
Alignment 0 – left justify
1 – right justify
2 – center
Autosize true / false
Backcolor
Backstyle 0 – transparent
1 – opaque
Borderstyle 0 – none
1 – fixed single
Caption
Enabled true / false
Font
Forecolor
Height
Left
Tooltiptext
Top
Visible true / false
Width
Control name Property Value
Textbox Name
Alignment 0 – left
1 – right
2 – center
Backcolor
Enabled True / false
Font
Forecolor
Height
Left
Maxlength
Multiline True / false
Passwordchar
Scrollbars 0 – none
1 – horizontal
2 – vertical
3 – both
Width
Borderstyle 0 – none
1 – fixed single
Appearance 0 – flat
1 – 3D
Text
Tooltiptext
Top
Visible true / false
Control Name Property Value
Command button Name
Appearance 0 – flat
1 – 3D
Backcolor
Caption
Disabledpicture
Downpicture
Enabled true / false
Font
Height
Left
Picture
Style 0 – standard
1 – graphical
Tooltiptext
Top
Visible true / false
Width
Control Name Property Value
Frame Name
Backcolor
Borderstyle 0 – none
1 – fixed single
Caption
Enabled true / false
Font
Forecolor
Height
Left
Tooltiptext
Top
Visible true / false
Width
Control Name Property Value
Checkbox Name
Allignment 0 – left
1 - right
Backcolor
Caption
Disabledpicture
Downpicture
Enabled true / false
Font
Forecolor
Height
Left
Picture
Style 0 – standard
1 – graphical
Tooltiptext
Top
Value 0 – unchecked
1 – checked
Visible true / false
Width
Control Name Property Value
Optionbutton Name
Allignment 0 – left
1 - right
Backcolor
Caption
Disabledpicture
Downpicture
Enabled true / false
Font
Forecolor
Height
Left
Picture
Style 0 – standard
1 – graphical
Tooltiptext
Top
Value true / false
Visible true / false
Width
Control Name Property Value
Picturebox Name
Align 0 - None
1 – Align top
2 – Align bottom
3 – Align left
4 – Align right
Autosize True / false
Backcolor
Borderstyle 0 – none
1 – fixed single
Enabled true / false
Font
Forecolor
Height
Left
Picture
Tooltiptext
Top
Visible true / false
Width
Control Name Property Value
Image Name
Borderstyle 0 – none
1 – fixed single
Enabled true / false
Height
Left
Picture
Stretch true / false
Tooltiptext
Top
Visible true / false
Width
Control Name Property Value
Combo box Name
Backcolor
Enabled True / false
Font
Forecolor
Height
Left
List
Style 0 – Drop down combo
1 – simple combo
2 – drop down list
Text
Tooltiptext
Top
Visible true / false
Width
Control Name Property Value
Listbox Name
Backcolor
Enabled True / false
Font
Forecolor
Height
Left
List
Multiselect 0 – none
1 – simple
2 – extended
Style 0 – standard
1 – checkbox
Tooltiptext
Top
Visible True / False
Width
Text
Control Name Property Value
Hscrollbar / vscrollbar Name
Enabled True / false
Height
Left
Max 32767
Min 0
Top
Value
Visible True / false
Width
Control Name Property Value
DriveListbox Name
Backcolor
Enabled True / false
Font
Forecolor
Height
Left
Tooltiptext
Top
Visible True / false
Width
Drive
Control Name Property Value
Directory list box Name
Backcolor
Enabled True / false
Font
Forecolor
Height
Left
Tooltiptext
Top
Visible True / false
Width
Path
Control Name Property Value
File list box Name
Backcolor
Enabled True / False
Font
Forecolor
Height
Left
Multiselect 0 – none
1 – simple
2 – extended
Pattern
Tooltiptext
Top
Visible True / false
Width
Path
Control Name Property Value
Shape Name
Backcolor
Backstyle 0 – transparent
1 – opaque
Bordercolor
Fillcolor
Height
Left
Shape 0 – rectangle
1 – square
2 – oval
3 – circle
4 – rounded rectangle
5 – rounded square
Top
Visible True / false
Width
Fillstyle 0 – solid
1 - transparent
Control Name Property Value
Timer Name
Enabled True /False
Interval
Left
Top
Control Name Property Value
Form Name
Backcolor
Caption
Enabled True / false
Font
Forecolor
Height
Left
Mdichild True / false
Maxbutton True / false
Minbutton True / false
Picture
Top
Visible True / False
Width
Windowstate 0 – normal
1 – minimized
2 - maximized

Events
• Event refers to the occurrence of an activity.

• Some useful events

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.

• Setting the Properties


The controls that you draw on your form, have some properties associated with them. The
properties can be set by two different ways.

1. At design time
2. At run time.

1. Properties can be set at design time using properties window.

2. Setting properties at run time.

• Setting the Alignment property

Example:
Label1.alignment = 0
Or
Text1.alignment = 1

• Setting Appearance property

Example:
Label1.appearance =1
Or
Command1.appearance = 1
Or
Option1.appearance = 0

• Setting Autosize Property


Example:
Label1.autosize = true
Or
Label1.autosize = false
Or
Picture1.autosize = true

• Setting the backcolor property


Example:
Label1.backcolor = RGB(100, 125, 190)

0-255 (red) 0-255(green) 0-255( blue)


Or
text1.backcolor = RGB(50, 50, 100)
Or
text1.backcolor = VbRed
• Setting Backstyle Property
Example:
Label1.Backstyle = 0
Or
Label1.backstyle = 1

• Setting Borderstyle Property


Example:
Label1.Borderstyle = 0
Or
Label1.Borderstyle = 1

• Setting the Enabled Property


Example
Text1.enabled = true
Or
Label1.enabled = false

• Setting caption property


Example:
Label1.caption = “this is my name”
Or
Command1.caption = “click me”

• Setting forecolor property


Example:
Label1.forecolor = RGB( 100, 255, 70)
Or
Label1.forecolor = VbBlue
• Setting Height Property

Example:
Label1.height = 100
Or
Command1.height = 200

• Setting the tooltiptext property


Example:
Text1.tooltiptext = “This is a text box”

• Setting the text property


Example:
Text1.text = “Amit sharma”

• Setting the visible property


Example:
Label1.visible = true
Or
Text1.visible = false

• Setting the width property


Example:
Text1.width = 250
Or
Command1.width = 100

• Setting the style property


Example:
Command1.style = 0
Or
Option1.style = 1

• Setting the picture property


Example:
Check1.picture = loadpicture ( “c:\abc.gif”)
Or
Command1.picture = loadpicture ( “c:\abc.gif”)
Or
Option1.picture = loadpicture ( “c:\abc.gif”)
Or
Picture1.picture = loadpicture ( “c:\abc.gif”)

• Setting the disabledpicture and downpicture property


Example:
Command1.disabledpicture = loadpicture(“c:\abc.gif”)
Or
Command1.downpicture = loadpicture(“c:\abc.gif”)

• Setting the value property of checkbox


Example:
Check1.value = 0
Or
Check1.value = 1

• Setting the value property of option button


Example:
Option1.value = true
Or
Option1.value = false

• Setting the stretch property of image control


Example
Image1.stretch = true
Or
Image1.stretch = false

• Setting the max/min property of Hscrollbar / Vscrollbar


Example
Hscroll1.max = 100

Hscroll1.min = 1

Vscroll1.max = 100

Vscroll1.min = 1
• Setting the fillstyle and fillcolor property
Example:
Shape1.fillstyle = 0

Shape1.fillcolor = RGB(100, 150, 200)


• Setting the Shape Property of Shape control
Example:
Shape1.shape = 1
Or
Shape1.shape = 3

• Setting the windowstate property of form


Example:
Form1.windowstate = 0
Or
Form1.windowstate = 1

Some Examples:
Example1:- Loading a picture in the picture box at run time.

Picture Box
Load

Command Button (cmdload)

Private sub cmdload_click()


Picture1.picture = loadpicture (“c:\abc.gif”)
End sub

Example2: – Loading a picture in the image control at run time.

Image control(image1)

Load

Command Button( cmdload )


Private sub cmdload_click()
Image1.picture = loadpicture (“c:\abc.gif”)
End sub

Example3: List Box


ListBox (list1)
Enter Your Name
Listcount
Add

Delete Listindex

Clear Display

Private sub cmdadd_click()


List1.additem text1.text
End sub

Private sub cmddel_click()


List1.removeitem list1.listindex
End sub

Private sub cmdclear_click()


List1.clear
End sub

Private sub cmdcount_click()


Msgbox List1.listcount
End sub

Private sub cmdindex_click()


Msgbox list1.listindex
End sub

Private sub cmddisp_click()


Msgbox list1.text
End sub
• Adding an item in the list box
List1.additem “Ravi”
List1.additem text1.text

• Removing an item from the list box


List1.removeitem 0
List1.removeitem list1.listindex

Example 4: Hscrollbar / Vscrollbar application

Hscroll1

Sum

Command Button (cmdsum) vscroll1

Private sub hscroll1_change()


Text1.text = hscroll1.value
End sub

Private sub Vscroll1_change()


Text2.text = vscroll1.value
End sub

Private sub cmdsum_click( )


Dim a as integer, b as integer
a = val (text1.text)
b = val (text2.text)
text3.text = a + b
End sub

Example 5 : Shape Control Application


Shape Control (shape1)
Rectangle Square Oval

Circle Rounded Rounded


Rectangle square

Private sub cmdrect_click( )


Shape1.shape = 0
End sub

Private sub cmdsquare_click( )


Shape1.shape = 1
End sub

Private sub cmdoval_click( )


Shape1.shape = 2
End sub

Private sub cmdcircle_click( )


Shape1.shape = 3
End sub

Private sub cmdroundrect_click( )


Shape1.shape = 4
End sub

Private sub cmdroundsquare_click( )


Shape1.shape = 5
End sub

Example 6 : Drive, Directory and file list box


Drive1 directory1 file1
C:

C:\
Abc.dll
Program
Rmn.dll
Files
Pqr.exe
……….
………
……….
………
……….
………

Enter the pattern

Private sub drive1_change( )


Dir1.path = drive1.drive
End sub

Private sub dir1_change( )


File1.path = dir1.path
End sub

Private sub text1_change( )


File1.pattern = text1.text
End sub

Example 7: Timer application. Label1

Reset

Timer1 cmdreset

Set the following properties:


Timer1.interval = 1000
Label1 - > font = MS sans serif, bold, 18
Dim I as integer
Private sub cmdreset_click( )
I=0
End sub
Private sub timer1_timer ( )
Label1.caption = i
I=I+1
End sub

Example 7: Option Button Application.

Percentage

90 80

70 60

Click

lblgrade cmdgrade

Private sub cmdgrade_click( )


If optone.value = true then
Lblgrade.caption = “ The grade is A”
Else if opttwo.value = true then
Lblgrade.caption = “ The grade is B”
Else if optthree.value = true then
Lblgrade.caption = “ The grade is C”
Else if optfour.value = true then
Lblgrade.caption = “ The grade is D”
End if
End sub

Private sub form_load( )


Optone.value = false
Opttwo.value = false
Optthree.value = false
Optfour.value = false
End sub

• Manipulating forms

• Setting the startup form

To change the startup form


1. Goto project menu - > project peoperties
2. Select General Tab
3. In startup object listbox select the form
4. Click Ok

• Loading and unloading the forms

Load form1
Unload form1
Unload me

• Showing and Hiding forms

Form1.show
Form2.show
Form1.hide
Me.hide

• Assigning the Access key


Command buttons can be used by clicking on them or by pressing the alt + <
access key >. To assign a keyboard access key to command button place ampersand( &) in
front of the letter that is to be used as access key, while setting the caption property of the
command button.

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

• The if…. Then… else statement


Syntax:
If (condition) then
statements
else
statements
End if
Example: Text1

Enter the marks

Result

cmdresult

Private sub cmdresult_click( )


Dim m as integer
m = val (text1.text)
if (m >= 40) then
print ”you are passed”
else
print ”you are failed”
end if
End sub
• If …then… else if statement
Syntax:
If (condition) then
statements
else if (condition) then
statements
else if (condition) then
statements
.
.
.
else
statements
End if

Example:
Text1

Enter a number

Print day

cmdprintday

Private sub cmdprintday_click( )


Dim x as integer
X = val(text1.text)
If(x = 1) then
Msgbox ”Sunday”
else if (x = 2) then
Msgbox ”Monday”
else if (x = 3) then
Msgbox ”Tuesday”
else if (x = 4) then
Msgbox ”Wednesday”
else if( x = 5) then
Msgbox ”Thursday”
else if (x = 6) then
Msgbox ”Friday”
else if (x = 7) then
Msgbox ”Saturday”
Else
Msgbox ”Invalid choice”
End if
End sub

• 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

• The iif( ) function:


Syntax:
iif(condition, value1, value2)
Example:
Dim num1 as integer, num2 as integer, big as integer
Num1 = 7
Num2 = 81
Big = iif (num1 >= num2, num1, num2)

• Select case statement:

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

Private sub cmdprintday_click( )


dim m as integer
m = val( text1.text )
select case m
case 1: Msgbox ”Sunday”
case 2
Msgbox ”Monday”
case 3
Msgbox ”Tuesday”
case 4: Msgbox ”Wednesday”
case 5: Msgbox ”Thursday”
case 6: Msgbox”Friday”
case 7: Msgbox”Saturday”
case else
Msgbox ”Invalid choice”
End select
End sub

Example 2:
Text1

Enter the marks

Result

cmdresult

Private sub cmdresult_click( )


Dim m as integer
M = val( text1.text )
Select case m
Case is < 50
Msgbox ”Fail”
Case is < 60: Msgbox ”Grade B”

Case is < 75: Msgbox ”Grade A”

Case else: Msgbox ”Grade A+”


End select
End sub

Example 3:
txtmonth

Enter the month no.

No. of days

cmdnod

Private sub cmdnod_click( )


Dim n as integer
n = val ( txtmonth.text )
select case n
case 1,3,5,7,8,10,12
Msgbox ”31 days in the month”
case 2
Msgbox ”28 days in the month”
Case 4,6,9,11
Msgbox ”30 days in the month”
End select
End sub

• 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.(b) Do…loop while


Example:
x=1
do
print “ABC”
x=x+1
loop while (x <= 10)
Example:
x = 10
do
print “raj”
x=x-1
loop while (x >= 1)

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

2.(d) Do…loop until

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

• Exiting from loops


• Exit For
• Exit Do
Example: (Exit For)
for i=1 to 10 step 1
if (I = 7) then
Exit for
End if
print i
next i
Example: (Exit do)
x=1
do while x<=10
if(x=7) then
Exit do
End if
print x
x=x+1
loop
• Arrays
Array is a collection of variables of same type that are referenced by a common
name.
Example:
Private sub Command1_Click()
dim a(4) as integer
dim i as integer
for i=0 to 4 step 1
a(i) = inputbox "enter a number "
next i

for i=0 to 4 step 1


print a(i)
next i
End Sub

dim x(1 to 5) as integer


dim x(5 to 9) as integer
• Redim statement
Example:
Private sub Cmdprint_Click( )
dim a( ) as integer
dim i as integer
redim a(4)
for i=0 to 4 step 1
a(i) = inputbox "Enter a number"
next i
for i=0 to 4 step 1
print a(i)
next i
redim a(9)
print "array after re-dimensioning "
for i=0 to 9 step 1
print a(i)
next i
End sub
• Preserve statement
Redim preserve a(9)

• Procedures & Functions

Types of Procedures

1. Sub Procedures
2. Function Procedures
3. Property Procedures

1. Sub Procedures: A sub procedure or subroutine or sub is a procedure


that performs a specific task but does not return a value.

Example: Procedure without arguments

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

Example: Procedures with arguments

Enter a number text 1


Enter second number text 2
Result is
text 3
sum

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:

Enter a number Text1

Enter a second number Text2

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

Private sub cmdprint_click( )


dim x as integer
x=5
print x …. 5
call change(x) or change x …. 100
print x …. 100
End sub
Private sub change (Byref n as integer)
n=100
print n
End sub
• Using Optional Arguments

Enter first number Text1

Enter second number Text2

Result is Text3

sum
cmdsum

Private sub cmdsum_click( )


dim x as integer, y as integer
x = val(text 1.text)
y = val(text 2.text)
findsum x, y
findsum x
findsum
findsum 2, y, 5 …. error
End sub
Private sub findsum (optional n as integer = 70, optional m as integer = 80)
text3.text = n + m
End sub

• Exiting from Procedures


1. Exit Sub
2. Exit Function

Enter a no. text1

check
cmdcheck

Private sub cmdcheck_click( )


dim x as integer
x = val(text1.text)
call samplepro(x)
End sub
Private sub samplepro (n as integer )
if (n > 50) then
msgbox "you have got a car"
else
Exit sub
End if
End sub

• 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

3. Trim, Ltrim, and Rtrim functions


Text1.text = Ltrim(“ daljeet”)
Text1.text = Rtrim(“sandeep ”)
Text1.text = trim(“ amit arora ”)
Text1.text = trim(text1.text)
Text1.text = rtrim(text1.text)

4. Left and Right functions


dim n as string, m as string
m = “daljeet”
n = right(m, 4)
print n - jeet
m = “raj arora”
print left (m,3) - raj

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 and Time Functions


1. Now( ) Function
print now( )
Or print now .… 1/17/06 11:25:50 AM

2. Date and Date$ Functions


Print date …. 1/17/04
Print date$ …. 01-17-2004

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

3. Time and Time$ Functions


Print time …. 9:11:50 PM
Print time$ …. 21:11:50

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

6. Hour, Minute and Second Functions


dim x as date
x = #12:10:41 AM#
Print hour(x) - 12
Print minute(x) - 10
Print second(x) - 41
Print minute(now)

7. Timer( ) Function
Print timer
Or Text1.text = timer( )

8. Dateadd and Datediff Functions


Syntax:
Dateadd (interval, number, olddate)
Example:
x = dateadd(“y”, 25, now)
Or x = dateadd(“yyyy”, 3, now)

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”)

Вам также может понравиться