Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

VB Script Basics

1. It is a scripting language

2. Declare variables and Display the values of variables


dim x, myname
x=4
myname=”abcd”
document.write(“The value of x is “ &x&”and name is”&myname)

3. Single line comment,need to put quote symbol at the first


‘this is single line comment,add quote at the beginning

4. Goto new line using <br> tag


document.write(“<br> this will goto new line”)

5. Alert and message Boxes


alert(“This is alert box”)
msgbox “This is message box”

6. A function will be like this


function myfunction()
document.write(“This is function content”)
end function
call the above function
myfunction()

7. Conditional Statements
x=4
if x=4 then
document.write(x)
elseif x=5 then
document.write(“x=5”)
else
document.write(“x=6”)
end if

8.for loops
for a=1 to 3
document.write("<br>"&a)
next
for a=1 to 3 step 2
document.write("<br>"&a)
next
for a=3 to 1 step -2
document.write("<br>"&a)
next
dim names(1)
names(0)="abcd"
names(1)="bcde"
for each i in names
document.write("<br>"&i)

9.do while loops


i=1
do while i<3
document.write("<br>"&i)
i=i+1
loop
i=1
do
document.write("<br>"&i)
i=i+1
loop while i<3

10.Array Creation
dim names[1]
names[0]=”abcd”
names[1]=”bcde”
for i=0 to 1
document.write(“<br>”,&i)

11.Predefined Functions : June 25th 2009

1. document.write(date())-----------------------------------------6/25/2009
2. document.write(time())-----------------------------------------10:19:58 AM
3. document.write(WeekDayName(1))----- --------------------Sunday
4. document.write(WeekDayName(1,true))---------------------Sun
5. document.write(WeekdayName(weekday(date)))----------- Thursday
6. document.write(MonthName (month(date)))----------------- June
7. document.write(IsDate("10/30/99"))--------------------------- True
8. document.write(ucase("abcd"))---------------------------------ABCD
9. document.write(Lcase("ABCD"))------------------------------abcd
10. document.write(strReverse("ABCD"))------------------------DCBA
11. document.write(Round(48.66776677))------------------------49
12. document.write(Round(48.36776677))------------------------48
13.document.write(now())------------------------------------------6/25/2009 11:50:27 AM
14. document.write(Hex(460))-------------------------------------1CC
15. document.write(SQR(16))--------------------------------------4
16. document.write(len("india"))-----------------------------------5
17. x=Array(12,13,14,15,16)
document.write(x(3))-------------------------------------------15
18. randomize()
randomNumber=Int(100 * rnd())
document.write("A random number: <b>" & randomNumber & "</b>").
Ex: A random number: 56 ---First Run
A random number: 78 ---Second Run

One Simple VB Script program having all the basics :

<html>
<head>
<script type="text/vbscript">
<!--
'How to declare variables and print them
a=8
name="hari"
document.write("The value of a is : " &a&"<br>The name is : " &name)

'how to write a function


function myfunction()
document.write("<br>myfunction is called")
End function

'how to call a function


myfunction()
-->
</script>
</head>
<body>
<script type="text/vbscript">
<!--
'call the function in header
myfunction()

'how to use if and else statements


x=4
y=67
z=3
if (x>y and x>z) then
document.write("x is bigger than y and z ")
elseif (y>x and y>z) then
document.write("<br>y is bigger than x and z ")
else
document.write("z is bigger than x and y ")
end if
'How to put an alert box—below commented
'alert("This is alert box")

'How to put a message box—below commented


'msgbox("This is mesage box")

'how to use case tags


i=2
select case i
case 0
document.write("i value is 0 :")
case 1
document.write("i valus is 1 :")
case 2
document.write("<br>i valus is 2 :")
case else
document.write("i does not have any value")
end select

'how to write for loops


document.write("<br>First for loop")
for a=1 to 3
document.write("<br>"&a)
next
document.write("<br>Second for loop")
for a=1 to 3 step 2
document.write("<br>"&a)
next
document.write("<br>Third for loop")
for a=3 to 1 step -2
document.write("<br>"&a)
next
document.write("<br>Using for each")
dim names(1)
names(0)="abcd"
names(1)="bcde"
for each i in names
document.write("<br>"&i)
next

document.write("<br>Using do while loops")


'while loop
i=1
do while i<3
document.write("<br>"&i)
i=i+1
loop

'do while loop


i=1
do
document.write("<br>"&i)
i=i+1
loop while i<3

'How to write sub procedures


sub mysub()
document.write("<br>This is sub procedure")
end sub
call mysub()

'How functions can have values like return type


function value()
value=12
end function
document.write("<br> Function returns the value : " &value)

'Predefined methods : using at June 25th 2009


document.write("<br>Today's date is : " &date())
document.write("<br>Now the time is : " &time())
document.write("<br>The weekday 1 represents : " &WeekDayName(1))
document.write("<br>The weekday 1 shortform represents : " &WeekDayName(1,true))
document.write("<br>Today is : " &WeekdayName(weekday(date)))
document.write("<br>This month is : " &MonthName(month(date)))
document.write("<br>Date format is true or false : " &IsDate("10/30/99"))
document.write("<br>Converting the letters to uppercase : " &ucase("abcd"))
document.write("<br>Converting the letters to lowercase : " &Lcase("ABCD"))
document.write("<br>Reverse the string: " &strReverse("ABCD"))
document.write("<br>Round the number to nearer value: " &Round(48.66776677))
document.write("<br>Round the number to nearer value: " &Round(48.36776677))
document.write("<br>The date and time is : " &now())
document.write("<br>square root value of 16 is : " &SQR(16))
document.write("<br>The length of string usa is : " &len("usa"))
x=Array(12,13,14,15,16)
document.write("<br>The value at the 4th position in the array x is : " &x(3))
randomize()
randomNumber=Int(100 * rnd())
document.write("<br>A random number: " &randomNumber)

-->
</script>
</body>
</html>

Output :

The value of a is : 8
The name is : hari
myfunction is called
myfunction is called
y is bigger than x and z
i valus is 2 :
First for loop
1
2
3
Second for loop
1
3
Third for loop
3
1
Using for each
abcd
bcde
Using do while loops
1
2
1
2
This is sub procedure
Function returns the value : 12
Today's date is : 6/25/2009
Now the time is : 3:07:28 PM
The weekday 1 represents : Sunday
The weekday 1 shortform represents : Sun
Today is : Thursday
This month is : June
Date format is true or false : True
Converting the letters to uppercase : ABCD
Converting the letters to lowercase : abcd
Reverse the string: DCBA
Round the number to nearer value: 49
Round the number to nearer value: 48
The date and time is : 6/25/2009 3:07:28 PM
square root value of 16 is : 4
The length of string usa is : 3
The value at the 4th position in the array x is : 15
A random number: 8

You might also like