VB Script Functions

Built-In Functions of VB Script
o Conversions (25)
o Dates/Times (19)
o Formatting Strings (4)
o Input/Output (3)
o Math (9)
o Miscellaneous (3)
o Rounding (5)
o Strings (30)
o Variants (8)
Important Functions
1) Abs Function
Returns the absolute value of a number.
Dim num
num=abs(-50.33)
msgbox num
2) Array Function
Returns a variant containing an Array
Dim A
A=Array(“hyderabad”,”chennai”,”mumbai”)
msgbox A(0)
ReDim A(5)
A(4)=”nellore”
msgbox A(4)
3) Asc Function
Returns the ANSI character code corresponding to the first letter in a string.
Dim num
num=Asc(“A”)
msgbox num
* It returns the value 65 *
4) Chr Function
Returns the character associated with the specified ANSI character code.
Dim char
Char=Chr(65)
msgbox char
* It returns A *
5) CInt Function
Returns an expression that has been converted to a Variant of subtype Integer.
Dim num
num=123.45
myInt=CInt(num)
msgbox MyInt
6) Date Function
Returns the Current System Date.
Dim mydate
mydate=Date
msgbox mydate
7) Day Function
Ex1) Dim myday
myday=Day(“17,December,2009″)
msgbox myday
Ex2) Dim myday
mydate=date
myday=Day(Mydate)
msgbox myday
8) DateDiff Function
Returns the number of intervals between two dates.
Dim Date1, Date2, x
Date1=#10-10-09#
Date2=#10-10-11#
x=DateDiff(“yyyy”, Date1, Date2)
Msgbox x ‘Differnce in Years
Date1=#10-10-09#
Date2=#10-10-11#
x=DateDiff(“q”, Date1, Date2)
Msgbox x ‘Differnce in Quarters
Date1=#10-10-09#
Date2=#10-10-11#
x=DateDiff(“m”, Date1, Date2)
Msgbox x ‘Differnce in Months
Date1=#10-10-09#
Date2=#10-10-11#
x=DateDiff(“w”, Date1, Date2)
Msgbox x ‘Differnce in weeks
Date1=#10-10-09#
Date2=#10-10-11#
x=DateDiff(“d”, Date1, Date2)
Msgbox x ‘Differnce in days
Date1=#10-10-09#
Date2=#10-10-11#
x=DateDiff(“h”, Date1, Date2)
Msgbox x ‘Differnce in Hours
Date1=#10-10-09#
Date2=#10-10-11#
x=DateDiff(“n”, Date1, Date2)
Msgbox x ‘Differnce in Minutes
Date1=#10-10-09#
Date2=#10-10-11#
x=DateDiff(“s”, Date1, Date2)
Msgbox x ‘Differnce in Seconds
Date1=#10-10-09#
Date2=#10-10-11#
x=DateDiff(“y”, Date1, Date2)
Msgbox x  ‘Differnce in day of years
Date1=#10-10-09#
Date2=#10-10-11#
x=DateDiff(“a”, Date1, Date2)
Msgbox x ‘Error
Date1=#10-10-09#
Date2=#10-10-11#
x=DateDiff(Date1, Date2)
Msgbox x ‘Error
9) Hour Function
Returns a whole number between 0 and 23, inclusive, representing the hour of the day.
Dim mytime, Myhour
mytime=Now
myhour=hour (mytime)
msgbox myhour
10) Join Function
Returns a string created by joining a number of substrings contained in an array.
Dim mystring, myarray(3)
myarray(0)=”Chandra “
myarray(1)=”Mohan “
myarray(2)=”Reddy”
mystring=Join(MyArray)
msgbox mystring
11) Eval Function
Evaluates an expression and returns the result.
12) Time Function
Returns a Variant of subtype Date indicating the current system time.
Dim mytime
mytime=Time
msgbox mytime
13) VarType Function
Returns a value indicating the subtype of a variable.

Dim x,y
x=100
y=VarType(x)
Msgbox y ‘2 (Integer)

x=”Hyderabad”
y=VarType(x)
Msgbox y ‘8 (String)

x=#10-10-10#
y=VarType(x)
Msgbox y ‘7(Date format)

x=100.56
y=VarType(x)
Msgbox y ‘ 5(Double)

y=VarType(a)
Msgbox y ‘0 (Empty)

Set x =CreateObject(“Scripting.FileSystemObject”)
y=VarType(x)
Msgbox y ‘9(Automation Object)

14) Left Function

Dim Val, x,y
Val=”Hyderabad”
x=Left(Val,3)
Msgbox x ‘Hyd

Val=100
x=Left(Val,1)
Msgbox x ‘1

Val=”Hyderabad”
x=Left(Val,0)
Msgbox x ‘Null

Val=”Hyderabad”
x=Left(Val,12)
Msgbox x ‘Hyderabad

Val=#10-10-10#
x=Left(Val,3)
Msgbox x ’10/

Val=”Hyderabad”
x=Left(Val)
Msgbox x ‘Error (Lengnth is Manditory)

14) Right Function
Dim AnyString, MyStr
AnyString = “Hello World” ‘ Define string.
MyStr = Right(AnyString, 1) ‘ Returns “d”.
MyStr = Right(AnyString, 6) ‘ Returns ” World”.
MyStr = Right(AnyString, 20) ‘ Returns “Hello World”.
15) Len Function
Returns the number of characters in a string or the number of bytes required to store a variable.
Ex 1):
Dim Mystring
mystring=Len(“G.C.Reddy”)
msgbox mystring
Ex 2):
Dim Mystring
Mystring=Inputbox(“Enter a Value”)
Mystring=Len(Mystring)
Msgbox Mystring
16) Mid Function
Returns a specified number of characters from a string.

Dim Val, x,y
Val=”Hyderabad”
x=Mid(Val,3,4)
Msgbox x ‘dera

Val=100
x=Mid(Val,1)
Msgbox x ‘100

Val=”Hyderabad”
x=Mid(Val,6,7)
Msgbox x ‘abad

Val=”Hyderabad”
x=Mid(Val,6,1)
Msgbox x ‘a

Val=”Hyderabad”
x=Mid(Val,6,0)
Msgbox x ‘Null

Val=”Hyderabad”
x=Mid(Val,12)
Msgbox x ‘Null

Val=#10-10-10#
x=Mid(Val,3,3)
Msgbox x ‘/10

Val=#2010-10-10#
x=Mid(Val,5)
Msgbox x ‘/2010

Val=”Hyderabad”
x=Mid(Val)
Msgbox x ‘Error

17) Timer Function
Returns the number of seconds that have elapsed since 12:00 AM (midnight).
Function myTime(N)
Dim StartTime, EndTime
StartTime = Timer
For I = 1 To N
Next
EndTime = Timer
myTime= EndTime – StartTime
msgbox myTime
End Function
Call myTime(2000)
17) isNumeric Function
Dim MyVar, MyCheck
MyVar = 53
MyCheck = IsNumeric(MyVar)
msgbox MyCheck
MyVar = “459.95”
MyCheck = IsNumeric(MyVar)
msgbox MyCheck
MyVar = “45 Help”
MyCheck = IsNumeric(MyVar)
msgbox MyCheck
* It Returns True/False like Result *
18) Inputbox Function
Displays a prompt in a dialog box, waits for the user to input text or click a button, and returns the contents of the text box.
Dim Input
Input = InputBox(“Enter your name”)
MsgBox (“You entered: ” & Input)
19) Msgbox Function
Displays a message in a dialog box, waits for the user to click a button, and returns a value indicating which button the user clicked.
Dim MyVar
MyVar = MsgBox (“Hello World!”, 65, “MsgBox Example”)
20) CreateObject
creates and returns reference of the filesytemobject to an Automation object. It can be used for performing operations on computer file system
Set objFso=createobject (“Scripting.FileSystemObject”)
‘creates and returns reference of  the Excel bject to an Automation object. It can be used for performing operations on Spreed sheet (Ms-Excel files)
Set objExcel = CreateObject(“Excel.Application”)
‘creates and returns reference of  the Word Object to an Automation object. It can be used for performing operations on Ms-Word documents
Set objWord = CreateObject(“Word.Application”)
‘creates and returns reference of  the Database Connection to an Automation object. It can be used for Connecting, opening and Closing databases
Set objConnection = CreateObject(“ADODB.Connection”)
‘creates and returns reference of  the Database Recordset to an Automation object. It can be used for performing operations on database tables(Records)
Set objRecordSet = CreateObject(“ADODB.Recordset”)
‘creates and returns reference of  the Ms-Power point object to an Automation object. It can be used for performing operations on Power point presentations
Set objPPT = CreateObject(“PowerPoint.Application”)
Set xmldoc = WScript.CreateObject(“msxml2.domdocument”)
21) Round
Returns a number rounded to a specified number of decimal places.
Dim num
num=172.499
num=Round(num)
msgbox num
22) StrReverse
It returns reverse value of the given sring
x=strreverse (“dabaraedyh”)
msgbox x
23) strComp
It compares two strings based on ASCII Values and Returens -1 (1st less than 2nd ), 0 (Equal) and 1 (1st greater than 2nd)
Dim x, y
x=”cd”: y=”bcd”
comp=strcomp(x,y)
msgbox comp
24) Replace
It replace a sub string with given value (another sub string)
mystring=Replace(“kb script”, “k”,”v”)
msgbox mystring

Comments