VBscript Complete Reference Guide - Chapter 1

13
Comparing VBScript control structures to Comparing VBScript control structures to JavaScript control JavaScript control structures structures JavaScriptControlStructure V BScriptControlStructure Equivalent sequence sequence if If/Then/End If if/else If/Then/Else/End If while While/Wend or Do While/Loop for For/Next do/while Do/Loop While switch Select Case/End Select none Do Until/Loop none Do/Loop Until http://www.apextg i.in

description

VBscript is a very light weight and user friendly language. It is currently running in industry as one of the prominent scripting language due to its fast and easy access. www.apextgi.in

Transcript of VBscript Complete Reference Guide - Chapter 1

Page 1: VBscript Complete Reference Guide - Chapter 1

Comparing VBScript control structures to Comparing VBScript control structures to JavaScript control JavaScript control structuresstructures

JavaScript Control Structure VBScript Control Structure Equivalent

sequence sequence

if If/Then/End If

if/else If/Then/Else/End If

while While/Wend or Do While/Loop

for For/Next

do/while Do/Loop While

switch Select Case/End Select

none Do Until/Loop

none Do/Loop Until

http://www.apextgi.in

Page 2: VBscript Complete Reference Guide - Chapter 1

Data Types and Control StructuresData Types and Control Structures

J avaScript VBScript

1 if ( s == t ) 2 u = s + t; 3 else if ( s > t ) 4 u = r; 5 else 6 u = n;

1 If s = t Then 2 u = s + t 3 ElseIf s > t Then 4 u = r 5 Else 6 u = n 7 End If

J avaScript VBScript

1 switch ( x ) { 2 case 1: 3 alert("1"); 4 break; 5 case 2: 6 alert("2"); 7 break; 8 default: 9 alert("?"); 10 }

1 Select Case x 2 Case 1 3 Call MsgBox("1") 4 Case 2 5 Call MsgBox("2") 6 Case Else 7 Call MsgBox("?") 8 End Select

Comparing JavaScript’s if structure to VBScript’s If structure

Comparing JavaScript’s switch to VBScript’s Select Case

Page 3: VBscript Complete Reference Guide - Chapter 1

Data Types and Control StructuresData Types and Control Structures

J avaScript VBScript

1 while ( !( x == 10 ) ) 2 ++x;

1 Do Until x = 10 2 x = x + 1 3 Loop

JavaScript VBScript

1 do { 2 ++x; 3 } while ( !( x == 10 ) );

1 Do 2 x = x + 1 3 Loop Until x = 10

J avaScript VBScript

1 x = 8; 2 for ( y = 1; y < x; y++ ) 3 x /= 2;

1 x = 8 2 For y = 1 To x 3 x = x \ 2 4 Next

Comparing JavaScript’s while to VBScript’s Do Until

Comparing JavaScript’s do/while to VBScript’s Do Loop/Until

Comparing JavaScript’s for to VBScript’s For

Page 4: VBscript Complete Reference Guide - Chapter 1

Data Types and Control Data Types and Control StructuresStructures

◦ Select Case/End Select Does not require break type statement

◦ VBScript structures without direct JavaScript equivalents: Do Until/Loop Do/Loop Until Loop until condition becomes True

◦ Exit Do Immediate exit from Do While/Loop, Do/Loop While, Do

Until/Loop or Do/Loop Until◦ Exit For

Immediate exit from For/Next◦ For loop

Optional Step keyword to increment or decrement1 ’ VBScript2 For y = 2 To 20 Step 23 Call MsgBox( "y = " & y )4 Next

Page 5: VBscript Complete Reference Guide - Chapter 1

VBScript FunctionsVBScript Functions

Predefined functions

◦ Variant functions IsEmpty

Returns True if variant not initialized

◦ Math functions Cos, Sin, etc.

Take arguments in radians radians = degrees π/180

◦ InputBox Displays dialog in which user can input data

◦ MsgBox Displays message dialog

◦ VBScript functions often take optional arguments

◦ Formatting functions FormatCurrency, FormatDateTime, etc.

Page 6: VBscript Complete Reference Guide - Chapter 1

VBScript FunctionsVBScript Functions

◦ Functions for getting info about scripting engine ScriptEngine

Returns “Jscript”, “VBScript” or “VBA” ScriptEngineBuildVersion

Returns current build version; ID number for current release ScriptEngineMajorVersion

Returns major version number for script engine ScriptEngineMinorVersion

Returns minor release number Line continuation character

◦ Underscore character, _◦ Statements cannot extend beyond current line without character

Page 7: VBscript Complete Reference Guide - Chapter 1

VBScript Example ProgramsVBScript Example Programs

Always place VBScript code inside HTML comments

◦ Prevent code from being displayed as text in browsers that do not understand VBScript

Script variables

◦ Variables declared outside of procedures Const keyword

◦ Create constants

Page 8: VBscript Complete Reference Guide - Chapter 1

VBScript Example ProgramsVBScript Example Programs

Comments

◦ Single quote (‘)

◦ Keyword Rem (remark) Considered outdated

Procedures:

◦ Sub keyword Procedure that does not return a value Exit Sub exits Sub procedure

◦ Function keyword Procedure that returns a value Exit Function exits Function procedure

Page 9: VBscript Complete Reference Guide - Chapter 1

VBScript Example ProgramsVBScript Example Programs

Function InputBox

◦ Displays a dialog for user to input data

Call InputBox ( prompt, caption, help File,_ x-coord, y-coord)

◦ Coordinates measured from top left (0,0). Measured in twips (1440 twips = 1 inch)

◦ Optional parameter help File can be left out by writing consecutive commas , ,

◦ _ (underscore) - line continuation character, required if statement extends beyond a line Use as many as necessary

Page 10: VBscript Complete Reference Guide - Chapter 1

VBScript Example ProgramsVBScript Example Programs Calling functions

◦ If function call has arguments in parentheses, use keyword Call If function assigns a variable, Call not needed

a = Abs( z ) If parentheses not used, keyword Call not needed

Function MsgBox

◦ Displays message dialog

MsgBox "VBScript is fun!", , "Results"

◦ Displays "VBScript is fun!" with "Results" in the title bar

◦ Optional argument to customize buttons and icon ignored

Page 11: VBscript Complete Reference Guide - Chapter 1

1.1 Set language to VBScript

1.2 Option Explicit statement

1.3 Define procedure On Click for the cmAdd button

1.4 Use CInt to convert input values from string subtype to integer subtype

1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 2<HTML>3<!--Fig. 22.15: addition.html -->5<HEAD>6<TITLE>Our first VBScript</TITLE>78<SCRIPT LANGUAGE = "VBScript">9<!--10 Option Explicit11 Dim intTotal1213 Sub cmdAdd_OnClick()14 Dim intValue15 16 intValue = InputBox("Enter an integer", "Input Box", , _17 1000, 1000)18 intTotal = CInt( intTotal ) + CInt( intValue )19 Call MsgBox("You entered " & intValue & _20 "; total so far is " & intTotal, , "Results")21 End Sub22-->23</SCRIPT>24</HEAD>2526<BODY>27Click the button to add an integer to the total.28<HR>

29<FORM>

30<INPUT NAME = "cmdAdd" TYPE = "BUTTON" 31 VALUE = "Click Here to Add to the Total">32</FORM>33</BODY>34</HTML>

Page 12: VBscript Complete Reference Guide - Chapter 1

Adding integers on a Web page using Adding integers on a Web page using VBScriptVBScript

Input dialog

Message dialog

Page 13: VBscript Complete Reference Guide - Chapter 1

facebook.com/apex.tgi

twitter.com/ApextgiNoida

pinterest.com/apextgi

Stay Connected with us for more chapters on VB Script and JAVA Script

http://www.apextgi.in