VB Script – Controlling VB Script Routines

VB Script – Controlling VB Script Routines

In this tutorial you will learn about Controlling VB Script Routines, Introduction, VB Script Description, If then else, Select Case, Looping Statements – For Next, For Each Next, Do loop, Do While, Do Until, While wend and Conclusion.

Introduction

Every programming language allows you to control the flow of your program like this VbScript also allows you to control how your scripts process the data by using conditional statements and iteration statements i.e sometimes called the looping statements. With the help of these conditional statements you can develop scripts that can evaluate data and you can use any criteria to determine what tasks can be performed. With the help of looping statements you can make your statements executes n number of times you want. You can create more functional pages with the help of this.

Description

We will discuss all the conditional statements. If you are new to the language world than you can find this in every programming language.VbScript provides two forms of conditional statements.

1. If then else
2. Select case

If then else

If then else statement is used to evaluate a condition to see whether the statement is true or false and depending on the conditions the statement executes. The below is the defined example how you can use these conditions.

If totalamount > 20000 then
Discountamount = totalamount * .10

The explanation of the above example is as under.

In the above example statement is

If totalamount > 20000

Which checks that the total amount for which you have purchased is greater than 20000 or not.If the condition is true than you are eligible for the discount and the below given statement is executed.

Discountamount = totalamount * .10

Repeating the same example we are adding one more line to it.

If totalamount > 20000 then
Discountamount = totalamount * .10
Subtotal = totalamount – Discountamount
End If

All the statements will be executed if the condition is true and none of the statements will be executed if the condition is false.

Now we will be discussing If then else condition.Example is as under.

If totalamount > 20000 then
Discountamount = totalamount * .10
Subtotal = totalamount – Discountamount
Else
Handlingfees = totalamount * .03
Subtotal= totalamount + Handlingfees
End If

The overview of this example is that, If the customer purchases items of more than 20000 than he is eligible for the discount of 10% and if totalamount is less than 20000 he is charged with the 3% of the handling fees.

Select Case

The Select case provides an alternative for If then else statements. When the conditions to be checked are more and it is quite complex to check it with If then else statements than you can choose the Select case to iterate that conditions.

The syntax is as under:

Select case condition
Case value
Case value
Case value
Case value
Case else
End Select


For example you want to set the shipping charges on the basis of state. It means every state are having different shipping charges. Now to handle this type of situation you can use select case statement.

Select case document.myform.txtstate.value
Case “ Florida”
Shippingcharges=.04
Case “California”
Shippingcharges=.03
Case else
Shippingcharges= .10
End Select

In the above example select statement checks each case until it finds its condition true and depending on the state it calculates the shippingcharges for that state. It no case is true than the condition of else will be true and that statement will be executed.

Looping Statements

VbScript provides four types of looping statements.

1. For Next
2. For Each Next
3. Do loop
4.While wend

For Next

It is used when you want to iterate the statements n number of times.You can increment or decrement it .The below given example explains you a simple loop.

For counter = 1 to 10
Tot= 5 * counter
MsgBox counter & “ multiply by 5 is “ & Tot
Next counter

The variable counter is taken as a numeric value . It can be incremented or decremented as per the requirement. The number 1 defines the starting point of the loop and number 10 defines the end of the loop. The output of the above example will be the product of multiples of table 5 .
If we take the another case.

For counter = 1 to 10 step 2
Tot= 5 * counter
MsgBox counter & “ multiply by 5 is “ & Tot
Next counter

In the above case step is used as 2 . It means the loop has to incremented by 2 and by default is 1.

For Each Next

For Each Next loop is similar to the For Next Loop but instead of repeating a loop for certain number of times , it repeats the loop for each member of a specified collection.

Do loop

The do loop condition statement is executed until a specified condition is met. The do loop are further categorized in to two :

Do While

Do loop that contains the while keyword is performed till the condition being tested is true. You can check the condition in the beginning.

Do While condition
Statement…
Statement….
Loop
Do While
Statement…
Statement…
Loop while condition


The difference between the above two formats are very simple. In the first format will never execute the statement including within its and the second format always perform its statement at least once.

Do Until

A do loop that contains the until keyword will continue to loop as long as the condition being tested is false. As with the help of do while statement you have the option to check the condition at the starting of the iteration.

Do until condition
Statement
Statement
Loop
Do
Statement
Statement
Loop until condition

Example:

Pass = InputBox(“Please Enter your password”)
Do until pass=”whoami”
Msgbox “ Password Invalid – Please try again”
Pass = InputBox(“Please Enter your password”)
Loop

In the above example the user will be prompted to enter the password first time and if the user has correctly entered the password do until statement would never be executed. If the password entered by the user is wrong then the loop will start iterating till the correct password is not entered.

While wend

The while wend statement is executed till the condition being found is true. The syntax format is as under.

While condition
Statement
Statement
Wend

Example:

< HTML >
< HEAD >
< TITLE >vbscript example< /TITLE >
< SCRIPT LANGUAGE="VBScript" >
< !– Add this to instruct non-IE browsers to skip over VBScript modules.
Option Explicit

Sub cmdCalculate_OnClick
Dim AmountofDiscount
Dim AmountofTax
Dim DISCOUNT_LIMIT
Dim DISCOUNT_RATE
Dim SubtotalBefore
Dim SubtotalAfter
Dim TAX_RATE
Dim TotalCost

‘ Define our constant values.
DISCOUNT_LIMIT = 1000
DISCOUNT_RATE = .10
TAX_RATE = 0.06

‘ Calculate the subtotal for the order.
SubtotalBefore = Document.myform.txtQuantity.Value * Document.myform.lblUnitCost.Caption

‘ Check to see if the order is large enough to offer discounts.
If (SubtotalBefore > DISCOUNT_LIMIT) Then
AmountofDiscount = SubtotalBefore * DISCOUNT_RATE
Else
AmountofDiscount = 0
End If
SubtotalAfter = SubtotalBefore – AmountofDiscount

‘ Calculate taxes and total cost.
AmountofTax = SubtotalAfter * TAX_RATE
TotalCost = SubtotalAfter + AmountofTax

‘ Display the results.
Document.myform.lblSubtotalBefore.Caption = SubtotalBefore
Document.myform.lblDiscount.Caption = AmountofDiscount
Document.myform.lblSubtotalAfter.Caption = SubtotalAfter
Document.myform.lblTaxes.Caption = AmountofTax
Document.myform.lblTotalCost.Caption = TotalCost
End Sub

Sub cmbProducts_Change()
Select Case Document.myform.cmbProducts.Value
Case "NEC MultiSync E1100"
Document.myform.lblUnitCost.Caption = 1590
Case "NEC MultiSync P1150"
Document.myform.lblUnitCost.Caption = 880
Case "NEC MultiSync E750"
Document.myform.lblUnitCost.Caption = 1940
Case Else
Document.myform.lblUnitCost.Caption = 0
End Select
End Sub
— >
< /SCRIPT >
< /HEAD >

< BODY >

< FORM NAME="myform" >
< TABLE >
< TR >
< TD >< B >Monitor:< /B >< /TD >
< TD >
< OBJECT ID="cmbProducts" WIDTH=159 HEIGHT=24
CLASSID="CLSID:8BD21D30-EC42-11CE-9E0D-00AA006002F3" >
< PARAM NAME="DisplayStyle" VALUE="7" >
< PARAM NAME="Size" VALUE="4202;635" >
< PARAM NAME="FontHeight" VALUE="200" >
< PARAM NAME="MatchEntry" VALUE="1" >
< PARAM NAME="ShowDropButtonWhen" VALUE="2" >
< PARAM NAME="FontCharSet" VALUE="0" >
< PARAM NAME="FontPitchAndFamily" VALUE="2" >
< PARAM NAME="FontWeight" VALUE="0" >
< /OBJECT >
< /TD >
< /TR >
< TR >
< TD >< B >Quantity:< /B >< /TD >
< TD >
< OBJECT ID="txtQuantity" WIDTH=100 HEIGHT=24
CLASSID="CLSID:8BD21D10-EC42-11CE-9E0D-00AA006002F3" >
< PARAM NAME="VariousPropertyBits" VALUE="746604571" >
< PARAM NAME="Size" VALUE="3037;635" >
< PARAM NAME="FontHeight" VALUE="200" >
< PARAM NAME="FontCharSet" VALUE="0" >
< PARAM NAME="FontPitchAndFamily" VALUE="2" >
< PARAM NAME="ParagraphAlign" VALUE="2" >
< PARAM NAME="FontWeight" VALUE="0" >
< /OBJECT >
< /TD >
< /TR >
< TR >
< TD >< INPUT TYPE="Button" NAME="cmdCalculate" VALUE="Calculate Cost" >< /TD >
< TD >< /TD >
< /TR >
< TR >
< TD >< B >Unit Cost:< /B >< /TD >
< TD >
< OBJECT ID="lblUnitCost" WIDTH=100 HEIGHT=24
CLASSID="CLSID:978C9E23-D4B0-11CE-BF2D-00AA003F40D0" >
< PARAM NAME="ForeColor" VALUE="0" >
< PARAM NAME="BackColor" VALUE="16777215" >
< PARAM NAME="Caption" VALUE="" >
< PARAM NAME="Size" VALUE="1582;635" >
< PARAM NAME="SpecialEffect" VALUE="2" >
< PARAM NAME="FontHeight" VALUE="200" >
< PARAM NAME="FontCharSet" VALUE="0" >
< PARAM NAME="FontPitchAndFamily" VALUE="2" >
< PARAM NAME="ParagraphAlign" VALUE="2" >
< PARAM NAME="FontWeight" VALUE="0" >
< /OBJECT >
< /TD >
< /TR >
< TR >
< TD >< B >Subtotal before discount:< /B >< /TD >
< TD >
< OBJECT ID="lblSubtotalBefore" WIDTH=100 HEIGHT=24
CLASSID="CLSID:978C9E23-D4B0-11CE-BF2D-00AA003F40D0" >
< PARAM NAME="ForeColor" VALUE="0" >
< PARAM NAME="BackColor" VALUE="16777215" >
< PARAM NAME="Caption" VALUE="" >
< PARAM NAME="Size" VALUE="1582;635" >
< PARAM NAME="SpecialEffect" VALUE="2" >
< PARAM NAME="FontHeight" VALUE="200" >
< PARAM NAME="FontCharSet" VALUE="0" >
< PARAM NAME="FontPitchAndFamily" VALUE="2" >
< PARAM NAME="ParagraphAlign" VALUE="2" >
< PARAM NAME="FontWeight" VALUE="0" >
< /OBJECT >
< /TD >
< /TR >
< TR >
< TD >< B >Discount:< /B >< /TD >
< TD >
< OBJECT ID="lblDiscount" WIDTH=100 HEIGHT=24
CLASSID="CLSID:978C9E23-D4B0-11CE-BF2D-00AA003F40D0" >
< PARAM NAME="ForeColor" VALUE="0" >
< PARAM NAME="BackColor" VALUE="16777215" >
< PARAM NAME="Caption" VALUE="" >
< PARAM NAME="Size" VALUE="1582;635" >
< PARAM NAME="SpecialEffect" VALUE="2" >
< PARAM NAME="FontHeight" VALUE="200" >
< PARAM NAME="FontCharSet" VALUE="0" >
< PARAM NAME="FontPitchAndFamily" VALUE="2" >
< PARAM NAME="ParagraphAlign" VALUE="2" >
< PARAM NAME="FontWeight" VALUE="0" >
< /OBJECT >
< /TD >
< /TR >
< TR >
< TD >< B >Subtotal after discount:< /B >< /TD >
< TD >
< OBJECT ID="lblSubtotalAfter" WIDTH=100 HEIGHT=24
CLASSID="CLSID:978C9E23-D4B0-11CE-BF2D-00AA003F40D0" >
< PARAM NAME="ForeColor" VALUE="0" >
< PARAM NAME="BackColor" VALUE="16777215" >
< PARAM NAME="Caption" VALUE="" >
< PARAM NAME="Size" VALUE="1582;635" >
< PARAM NAME="SpecialEffect" VALUE="2" >
< PARAM NAME="FontHeight" VALUE="200" >
< PARAM NAME="FontCharSet" VALUE="0" >
< PARAM NAME="FontPitchAndFamily" VALUE="2" >
< PARAM NAME="ParagraphAlign" VALUE="2" >
< PARAM NAME="FontWeight" VALUE="0" >
< /OBJECT >
< /TD >
< /TR >
< TR >
< TD >< B >Taxes:< /B >< /TD >
< TD >
< OBJECT ID="lblTaxes" WIDTH=100 HEIGHT=24
CLASSID="CLSID:978C9E23-D4B0-11CE-BF2D-00AA003F40D0" >
< PARAM NAME="ForeColor" VALUE="0" >
< PARAM NAME="BackColor" VALUE="16777215" >
< PARAM NAME="Caption" VALUE="" >
< PARAM NAME="Size" VALUE="1582;635" >
< PARAM NAME="SpecialEffect" VALUE="2" >
< PARAM NAME="FontHeight" VALUE="200" >
< PARAM NAME="FontCharSet" VALUE="0" >
< PARAM NAME="FontPitchAndFamily" VALUE="2" >
< PARAM NAME="ParagraphAlign" VALUE="2" >
< PARAM NAME="FontWeight" VALUE="0" >
< /OBJECT >
< /TD >
< /TR >
< TR >
< TD >< B >Total Cost:< /B >< /TD >
< TD >
< OBJECT ID="lblTotalCost" WIDTH=100 HEIGHT=24
CLASSID="CLSID:978C9E23-D4B0-11CE-BF2D-00AA003F40D0" >
< PARAM NAME="ForeColor" VALUE="0" >
< PARAM NAME="BackColor" VALUE="16777215" >
< PARAM NAME="Caption" VALUE="" >
< PARAM NAME="Size" VALUE="1582;635" >
< PARAM NAME="SpecialEffect" VALUE="2" >
< PARAM NAME="FontHeight" VALUE="200" >
< PARAM NAME="FontCharSet" VALUE="0" >
< PARAM NAME="FontPitchAndFamily" VALUE="2" >
< PARAM NAME="ParagraphAlign" VALUE="2" >
< PARAM NAME="FontWeight" VALUE="0" >
< /OBJECT >
< /TD >
< /TR >
< /TABLE >
< /FORM >

< SCRIPT LANGUAGE="VBScript" >
< !–
Document.myform.cmbProducts.Additem "NEC MultiSync E1100"
Document.myform.cmbProducts.Additem "NEC MultiSync P1150"
Document.myform.cmbProducts.Additem "NEC MultiSync E750"
— >
< /SCRIPT >
< /BODY >

< /HTML >



In the above example We have simply make use of select case statement to check the value of the control.The script is used to implement discounts by using constants values and by setting a discount limit of $ 1000 and a discount of 10%..We compare our subtotal amount against the discount limit If our amount is greater than the limit, the discount amount is calculated and stored in a variable and if it is less than or equal the discount amount is set to zero.

Conclusion :

Till now you must have known that how to use loops statements .Well they also provide you the flexibility to execute statements repetitively .

[catlist id=168].

Related posts