In VBScript< t+ere are t6o >inds o procedures a7ailableE t+e S procedure and t+e Fnction procedure5
##.# S Procedres
Sub procedure is a series o VBScript statements (enclosed by Sub and 'nd Sub statements) t+at perorm actions but don9t return a 7alue5
Sub procedure can ta>e ar,uments (constants< 7ariables< or epressions t+at are passed by a callin, procedure)5
I a Sub procedure +as no ar,uments< its Sub statement must include an empty set o parent+eses ()5
SyntaF
Sub -rocedure name () Statements
...
...
'nd Sub Or
Sub -rocedure name (ar,ument1< ar,ument2) Statements
...
...
'nd Sub ,0ample: #
Sub Con7ertTemp()
temp G InputBo(-lease enter t+e temperature in de,rees /5< 1) 8s,Bo T+e temperature is P Celsius(temp) P de,rees C5
'nd Sub ,0ample: (
##.( Fnction Procedres
/unction procedure is a series o VBScript statements enclosed by t+e /unction and 'nd /unction statements5
/unction procedure is similar to a Sub procedure< but can also return a 7alue5
/unction procedure can ta>e ar,uments (constants< 7ariables< or epressions t+at are passed to it by a callin, procedure)5
/unction returns a 7alue by assi,nin, a 7alue to its name in one or more statements o t+e procedure5 T+e return type o a /unction is al6ays a Variant5 SyntaF
/unction -rocedure name () Statements
...
...
'nd /unction Or
/unction -rocedure name (ar,ument1< ar,ument2) Statements
...
...
'nd /unction ,0ample: #
/unction Celsius(De,rees)
Celsius G (De,rees . 32) N " % 'nd /unction
,0ample: (
/unction cal(a<b<c) cal G (aAbAc) 'nd /unction
##.+ Jetting Data into and ot of Procedres
o 'ac+ piece o data is passed into our procedures usin, an ar,ument5
o r,uments ser7e as place+olders or t+e data 6e 6ant to pass into our procedure5 :e can name our ar,uments any 7alid 7ariable name5
o :+en 6e create a procedure usin, eit+er t+e Sub statement or t+e /unction statement< parent+eses must be included ater t+e name o t+e procedure5
o ny ar,uments are placed inside t+ese parent+eses< separated by commas5
##.- 2sing S and Fnction Procedres in Code
/unction in our code must al6ays be used on t+e ri,+t side o a 7ariable assi,nment or in an epression5
/or eampleF
Temp G Celsius(De,rees) .Or.
8s,Bo T+e Celsius temperature is P Celsius(De,rees) P de,rees5
To call a Sub procedure rom anot+er procedure< type t+e name o t+e procedure alon, 6it+ 7alues or any reuired ar,uments< eac+ separated by a comma5 T+e Call statement is not reuired< but i you do use it< you must enclose any ar,uments in parent+eses5
T+e ollo6in, eample s+o6s t6o calls to t+e 8y-roc procedure5 One uses t+e Call statement in t+e codeE t+e ot+er doesn9t5 Bot+ do eactly t+e same t+in,5
Call 8y-roc(irstar,< secondar,) 8y-roc irstar,< secondar,
@otice t+at t+e parent+eses are omitted in t+e call 6+en t+e Call statement isn9t used5
!4 Script 4ilt in Fnctions
Types of Fnctions
o Con7ersions (2)
o Dates"Times (1%)
o /ormattin, Strin,s (4)
o Input"Output (3)
o 8at+ (%)
o 8iscellaneous (3)
o 0oundin, ()
o Strin,s (3)
o Variants ($)
Important Fnctions
#' As Fnction
0eturns t+e absolute 7alue o a number5 Dim num
numGabs(.533) ms,bo num
(' Array Fnction
0eturns a 7ariant containin, an rray Dim
Grray(+yderabad<c+ennai<mumbai) ms,bo ()
0eDim ()
(4)Gnellore
ms,bo (4) +' Asc Fnction
0eturns t+e @SI c+aracter code correspondin, to t+e irst letter in a strin,5 Dim num
numGsc() ms,bo num
N It returns t+e 7alue ! N -' C)r Fnction
0eturns t+e c+aracter associated 6it+ t+e speciied @SI c+aracter code5 Dim c+ar
C+arGC+r(!) ms,bo c+ar N It returns N 7' CInt Fnction
0eturns an epression t+at +as been con7erted to a !ariant o subtype Integer5 Dim num
numG12354
myIntGCInt(num) ms,bo 8yInt 9' Date Fnction
0eturns t+e Current System Date5 Dim mydate
mydateGDate ms,bo mydate
' Day Fnction '1) Dim myday
mydayGDay(1#<December<2%) ms,bo myday
'2) Dim myday mydateGdate
mydayGDay(8ydate) ms,bo myday
;' DateDiff Fnction
0eturns t+e number o inter7als bet6een t6o dates5 Dim myday
mydateGK/(@#@(//K
0GDatediffLdL?mydate?*ow' msgo0 0
' %or Fnction
0eturns a 6+ole number bet6een and 23< inclusi7e< representin, t+e +our o t+e day5
Dim mytime< 8y+our mytimeG@o6
my+ourG+our (mytime) ms,bo my+our
#/' Moin Fnction
0eturns a strin, created by ?oinin, a number o substrin,s contained in an array5 Dim mystrin,< myarray(3)
myarray()GC+andra myarray(1)G8o+an myarray(2)G0eddy
##' ,val Fnction
'7aluates an epression and returns t+e result5
#(' Time Fnction
0eturns a !ariant o subtype Date indicatin, t+e current system time5 Dim mytime
mytimeGTime ms,bo mytime
#+' !arType Fnction
0eturns a 7alue indicatin, t+e subtype o a 7ariable5 Dim 8yC+ec>
8yC+ec> G VarType(3) 9 0eturns 25 8s,bo 8yc+ec>
8yC+ec> G VarType(1"1%"!2) 9 0eturns #5 8s,bo 8yc+ec>
8yC+ec> G VarType(VBScript) 9 0eturns $5 8s,bo 8yc+ec>
#-' 8eft Fnction
Dim 8yStrin,< *etStrin, 8yStrin, G VBSCript
*etStrin, G *et(8yStrin,< 3) 9 *etStrin, contains VBS5
#-' 6ig)t Fnction Dim nyStrin,< 8yStr
nyStrin, G ;ello :orld 9 Deine strin,5 8yStr G 0i,+t(nyStrin,< 1) 9 0eturns d5 8yStr G 0i,+t(nyStrin,< !) 9 0eturns :orld5
8yStr G 0i,+t(nyStrin,< 2) 9 0eturns ;ello :orld5
#7' 8en Fnction
0eturns t+e number o c+aracters in a strin, or t+e number o bytes reuired to store a 7ariable5
' 1)F Dim 8ystrin,
mystrin,G*en(&5C50eddy) ms,bo mystrin,
' 2)F Dim 8ystrin,
8ystrin,GInputbo('nter a Value) 8ystrin,G*en(8ystrin,)
8s,bo 8ystrin,
#9' "id Fnction
0eturns a speciied number o c+aracters rom a strin,5 Dim 8yVar
8yVar G 8id(VB Script is un< 4< !) 8s,bo 8yVar
N It 0eturns XScriptY N
#' Timer Fnction
0eturns t+e number o seconds t+at +a7e elapsed since 12F 8 (midni,+t)5 /unction myTime(@)
Dim StartTime< 'ndTime StartTime G Timer
/or I G 1 To @ @et
'ndTime G Timer
myTimeG 'ndTime . StartTime ms,bo myTime
'nd /unction
Call myTime(2)
#' is*meric Fnction Dim 8yVar< 8yC+ec>
8yVar G 3
8yC+ec> G Is@umeric(8yVar) ms,bo 8yC+ec>
8yVar G 4%5%
8yC+ec> G Is@umeric(8yVar) ms,bo 8yC+ec>
8yVar G 4 ;elp
8yC+ec> G Is@umeric(8yVar) ms,bo 8yC+ec>
N It 0eturns True"/alse li>e 0esult N
#;' Inpto0 Fnction
Displays a prompt in a dialo, bo< 6aits or t+e user to input tet or clic> a button<
and returns t+e contents o t+e tet bo5 Dim Input
Input G InputBo('nter your name) 8s,Bo (Lou enteredF P Input)
Dim 8yVar
8yVar G 8s,Bo (;ello :orld< !< 8s,Bo 'ample)
!4Script synta0 rles and gidelines
(#.# Case@sensitivity:
By deault< VBScript is not case sensiti7e and does not dierentiate bet6een upper case and lo6er.case spellin, o 6ords< or eample< in 7ariables< ob?ect and met+od names< or constants5
/or eample< t+e t6o statements belo6 are identical in VBScriptF
Bro6ser(8ercury)5-a,e(/ind a /li,+tF)5:eb*ist(toDay)5Select 31
bro6ser(mercury)5pa,e(ind a li,+tF)56eblist(today)5select 31
(#.( Te0t strings:
:+en 6e enter a 7alue as a tet strin,< 6e must add uotation mar>s beore and ater t+e strin,5 /or eample< in t+e abo7e se,ment o script< t+e names o t+e :eb site< :eb pa,e< and edit bo are all tet strin,s surrounded by uotation mar>s5
@ote t+at t+e 7alue 31 is also surrounded by uotation mar>s< because it is a tet strin, t+at represents a number and not a numeric 7alue5
In t+e ollo6in, eample< only t+e property name (irst ar,ument) is a tet strin, and is in uotation mar>s5 T+e second ar,ument (t+e 7alue o t+e property) is a 7ariable and t+ereore does not +a7e uotation mar>s5 T+e t+ird ar,ument (speciyin, t+e timeout) is a numeric 7alue< 6+ic+ also does not need uotation mar>s5
Bro6ser(8ercury)5-a,e(/ind a /li,+tF)5:ait-roperty(items count< TotalZItems<
2)
(#.+ !ariales:
:e can speciy 7ariables to store strin,s< inte,ers< arrays and ob?ects5 Hsin, 7ariables +elps to ma>e our script more readable and leible
(#.- Parent)eses:
To ac+ie7e t+e desired result and to a7oid errors< it is important t+at 6e use parent+eses () correctly in our statements5
(#.7 Indentation:
:e can indent or outdent our script to relect t+e lo,ical structure and nestin, o t+e statements5
(#.9 Comments:
:e can add comments to our statements usin, an apostrop+e (9)< eit+er at t+e be,innin, o a separate line< or at t+e end o a statement5 It is recommended t+at 6e add comments 6+ere7er possible< to ma>e our scripts easier to understand and maintain5
(#. Spaces:
:e can add etra blan> spaces to our script to impro7e clarity5 T+ese spaces are i,nored by VBScript5
,rrors
:e +a7e t6o types 'rrors in VB ScriptE t+ey are VBScript 0un.time 'rrors and VBScript Synta 'rrors
#+.# !4Script 6n@time ,rrors
VBScript run.time errors are errors t+at result 6+en our VBScript script attempts to perorm an action t+at t+e system cannot eecute5 VBScript run.time errors occur 6+ile our script is bein, eecutedE 6+en 7ariable epressions are bein, e7aluated<
and memory is bein, dynamic allocated5
#+.( !4Script Synta0 ,rrors
VBScript synta errors are errors t+at result 6+en t+e structure o one o our VBScript statements 7iolates one or more o t+e ,rammatical rules o t+e VBScript scriptin, lan,ua,e5 VBScript synta errors occur durin, t+e pro,ram compilation sta,e< beore t+e pro,ram +as be,un to be eecuted5