VBScript
VBScript, like JavaScript (Jscript) is an ActiveX-enabled scripting language that connects to scripting hosts such as Internet Explorer and performs functions locally using the Windows Script Host (WSH). As a general rule, scripting languages are coded faster and simpler than in the compiled languages of C and C++. VBScript is structured and used with smaller programs with limited capability. Programmers, developers and IT professionals learning VBScript should also be familiar with ASP (Active Server Pages) and possess SQL Server commercial experience. Tutorials on VBScript include controlling script routines, working with objects, variables, forms and general VBScript information.
VB Script - Introduction
Vb Script tutorials will be covered in the following topics which are given below :• What is VB Script ?
• Working with Variables?
• Objects and VB Script ?
• Controlling VB Script Routines ?
• Using VB Script with Forms?
In this tutorial you will learn about VB Script - What is VB Script ? Introduction, Description, How to Add VB script to web pages ? The Script Tag, How to handle Non Supporting Browsers and Conclusion
What is VB Script ?
Introduction :
VB Script is a scripting language developed by Microsoft.
With the help of this scripting language you can make your web pages more
dynamic and interactive. VB Script is a light version of Visual basic and it has an easy syntax.
Description :
VB Script is widely used and most popular as a client side
scripting language. In html language you use < and > around the tags. But
you can use many tags inside one pair of < % and % >. For Printing a
variable you can use < % = % >. How to Add VB script to web pages ?
There are scripting languages like Javascript and Vbscript and they are designed as an extension to html language.The Web browsers like Microsoft Internet Explorer receives the scripts along with the rest of the web page document. It is the browser responsibility to parse and process the scripts. These scripts are widely used as a client side scripting languages. Now you will start learning how the Script language works and we will show you step by step . So just concentrate on the further topics.The Script Tag
When ever you want to add scripts in your web pages you are preparing you have to use < script > starting tag and < / script > ending tag where the script is going to close. The example of this is shown below :
<
html >
< head > < title >vbscript example< /title > < script language=”vbscript” > Msgbox “Welcome to the world of VB Script” < /script > < /head > < body > < /body > < /html > |
I have used the example with a simple going “Welcome to the world of vbscript” as there is a tendency whenever a new learner is learning a new language we always welcome him.
Well, in the above example you have seen the language attribute. We have used VbScript as the scripting language.This Argument is required as there are more than one scripting language. Without the language argument, browser would not able to know if the text between the tags was vbscript or javascript or any other scripting language.
As the Script is totally dependent on the browser so next question arises is “ How to handle Non Supporting browsers”. If the client browser has disabled the script than your web page might now function properly. So for your advantage you can also the server side language. As the server side language is far from the scope of this tutorial.
How to handle Non Supporting Browsers
As not all browsers are compatible for scripting languages . As Vbscript is only supported by Microsoft Internet Explorer.So you might be thinking of the Non Supporting browsers. The Non Supporting browsers does not know how to handle this script part, so they will display all the script as part of your text on a web page. To resolve this Issue you can encase your script in the Comments tag as given in the below example.
<
html >
< head > < title >vbscript example< /title > < script language=”vbscript” > Msgbox “Welcome to the world of VB Script” < /script > < /head > < body > < /body > < /html > |
Now, When the Web browsers that does not support the scripting language will view your script as a comment and will simple ignore it.
The easiest way to learn any new language is to work on it.So we will use maximum examples so that you can practice more.
<
html >
< head > < title >First vbscript example< /title > < /head > < body > < form name="firstexample" > < Input type="button" name="cmdclickMe" value="Just Click" > < SCRIPT FOR="cmdClickMe" EVENT="onClick" LANGUAGE="VBScript" > MsgBox "A simple example of VBScript in action." < /SCRIPT > < /form > < /body >< /html > |
Save the above code with any name but with the extension .html and run it in your browser.You will see the result like in the below picture when you click button Just Click. This is functionality of the script.
As in the above example we have the script just after the html button created. But this is not the preferred approach and will lead to confuse state when the web pages are too large. So to avoid complexity and to ease the readablitiy of the web page document the below given approach is far
better.
<
html >
< head > < title >First vbscript example< /title > < SCRIPT LANGUAGE="VBScript" > Sub cmdclickMe_onClick MsgBox "A simple example of VBScript in action." End Sub < /SCRIPT > < /head > < body > < form name="firstexample" > < Input type="button" name="cmdclickMe" value="Just Click" > < /form > < /body >< /html > |
will explain you how it is working.The only difference of this code from the above code is that we have shifted our script tag to the head section in the html for easy readability. Just read the script tag here we have defined a sub procedure called cmdclickMe . This will be executed everytime the button is clicked as onClick event is attached with it. It means the script has to be executed everytime the button is clicked. Its simple.The output generated will be the same as in the previous example.
Conclusion
We have learned how to add script to the web pages.How to make your script not available to the non supporting browsers.
VB Script - Working with Variables
In this tutorial you will learn about VB Script - Wroking with variables, Introduction, Description, Naming Conventions, Variants and its sub types, How to Assign values to a variable ? What is the scope of a variable ? Constants, Arrays and Conclusion.
Introduction
A variable is a named location in your computer memory
which can be used for data storage during the working of your vb scripts or any
other language.Variables plays a vital role to store values . Variables can be
defined further into two categories. One is local variable and second are global variables. Local variables values will be only accessible in the part of the function in which that variable has been defined whereas global variable will be accessible in the whole program.
Description
Variables store input from the user that is being collected
via your web page. Please see the below example for a variable. Sub cmdClickMe_onClick
Name=InputBox(“Please Enter your Name”)
MsgBox “ The Name you entered was “ & Name
End Sub
Now, in the above example the second line shows a variable with the name of “Name”. Here one Input box will be displayed to the user with a message Please enter your name and what ever entered by the user will be stored in a variable called Name and later on displayed to the user with the help of MsbBox function.
There is something left in the variables i.e How to declare a variable. There are two types in which variable is declared one is explicitly and one is implicitly. Well the preferred method is explicitly with the help of dim statement.
For ex. Dim age
Here we have declared variable with the name of age . In this way you can also declare multiple variable in a single line only.
For ex. Dim name,age,status,birthdate
The above given examples are of explicit declaration of the variables. For implicit declaration you can declare and use the variable at any time within your code.But the main disadvantage of implicit declaration is that it is very hard to debug the code when the error occurs. So in my opinion always prefer the variables to declare explicitly. There are some naming conventions attached when you declare a variable.
Naming Conventions
Whenever you want to declare a variable always follow below
naming conventions. - The
variables always begin with an alphabet instead of any number.
- They
should not contain spaces.
- They must
be unique in their names.
- If you
want to define a long name than always concatenate the names with and
underscore.
Always start your variable name with capital alphabet letter and when the second word starts in the variable make it capital too.
They must not be larger than 255 characters in length.
Variants and its sub types
VbScript deals with a single datatype called variant.
Datatype means the kind of the data a variable will store.It can be a character
data,string data,number data , byte data and a Boolean data. But you don’t have
to worry in Vb Script as it only has a datatype of variant to store any type of
data.
Sub Type
|
Description
|
Boolean
|
True or False
|
Integer
|
Integers between –32768 and 32767
|
Long
|
Long data between –2147483648 and 2147483647
|
String
|
Character Strings
|
Single
|
Large Number with decimal points
|
Null
|
No valid data
|
Currency
|
Monetary values
|
Byte
|
Integers from 0 and 255
|
Date
|
Date and time
|
Double
|
Large numbers with decimal points
|
How to Assign values to a variable ?
Simple you have to declare a variable name and assign any
value. For ex. Name = “Chandra”
Status=False
Age=30
Now all the above variables has been assigned values.This is a simple way to declare and assign related values to a variable.
What is the scope of a variable ?
The scope of a variable defines whether a variable will be
accessible in the whole function or will be accessed only to its local
instance.I have defined earlier in the tutorials that they can also be deemed
as a local variables or can be deemed as a global variables. For ex.
< script >
Dim name
Sub cmdclickme_OnClick
Dim age
End Sub
< / script >
It is clear from the above example about the scope of the variable that the variable name will be available to the whole script as it is declared outside sub procedure so enhance his behaviour to the global as compared to the variable name age which is defined inside the sub procedure hence making his behaviour local and will be only accessed in this sub procedure only.
Constants
Vb Scripts does not support constants. Constants are the values
which are not changed throughout the program as you might have read in other
programming languages.
Arrays
If you have done any programming language you might have heard
about the Arrays and VbScript positively supports arrays. You can declare any
array with the help of dim statement. For ex. Dim country(20)
The above array has been created with the 20 elements, it means you can easily store 20 elements and will be referred by a single name. These type of arrays are sometimes called fixed arrays.Arrays index always start from zero. As given in the above example we have declared an array of 20 elements but when you put the values or retrieve the values from the array you have to start from the index 0 to index 19.To store values in an array just follow the simple strategy.
country(0)=”
country(1)=”
country(2)=”
Arrays too have a multi dimensions . To declare a multi dimensional array example is given below.Suppose you want to store a student name as well his/her age.To store the values in an array you have to reference both dimensions.
Dim nameage(30,0)=”Rahul”
Dim nameage(30,1)=”Mehta”
Dim nameage(30,2)=”Chandra”
As in the above example we know how many values will be stored in an array.But sometimes we don’t know how many values will be stored in an array than there arises a need for dynamic arrays.To declare a dynamic array see the below example.
Ex. Dim users()
In this way we can declare a dynamic array without initializing its size.But to change the size you can use the below given code.
Ex. Redim users(100)
In this way you can assign the size of the array. Now whenever you want to increase the size of the array again you must preserve the previous values you have stored in an array.To preserve the previous values use.
Redim Preserve users(100)
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 AmountofTax Dim CRLF Dim Message Dim Subtotal Dim TABSPACE Dim TAX_RATE Dim TotalCost ' Define our constant values. TAX_RATE = 0.06 CRLF = Chr(13) & Chr(10) TABSPACE = Chr(9) ' Perform order calculations. Subtotal = Document.myform.txtQuantity.Value * Document.myform.txtUnitPrice.Value AmountofTax = Subtotal * TAX_RATE TotalCost = Subtotal + AmountofTax ' Display the results. Message = "The total for your order is:" Message = Message & CRLF & CRLF Message = Message & "Subtotal:" & TABSPACE & "$" & Subtotal & CRLF Message = Message & "Tax:" & TABSPACE & "$" & AmountofTax & CRLF Message = Message & "Total:" & TABSPACE & "$" & TotalCost MsgBox Message,,"Your Total" End Sub --> < /SCRIPT > < /HEAD > < BODY > < FORM NAME="myform" > < TABLE > < TR > < TD >< B >Quantity:< /B >< /TD > < TD >< INPUT TYPE="Text" NAME="txtQuantity" SIZE=5 >< /TD > < /TR > < TR > < TD >< B >Unit price:< /B >< /TD > < TD >< INPUT TYPE="Text" NAME="txtUnitPrice" SIZE=5 >< /TD > < /TR > < /TABLE > < BR > < INPUT TYPE="Button" NAME="cmdCalculate" VALUE="Calculate Cost" > < /FORM > < /BODY > < /HTML > |
|
VB Script - Objects with VB Script
In this tutorial you will learn about VB Script - Objects with VB Script, Introduction, How to Add Objects to your web pages, How to Add Objects to your Web Pages and How to link Vb Script with Objects.Introduction
Objects which are in the form of ActiveX controls and java applets are useful for the enhancement of our web pages. By using a scripting languages like vbscript you can extend the capabilities of these controls by integrating in to scripts.
How to Add Objects to your web pages.
Objects are added to a web page with the < object >
tag.There are some attributes to this < object > tag called < param
> tags.
How to Add Objects to your Web Pages
The following below given example demonstrates how an
ActiveX control might appear when added to a page.
<
Object Id="lblstatus” width=54 height=20
ClassID="CLSID:978C9E23-D4BU-11CE-BF2D-00AA003F40D0” >
< Param name="Forecolor” value="0” > < Param name="Backcolor” value="16777215” > < Param name="Caption” value="Example of Vb script” > < Param name="Size” value="1582;635” > < Param name="Specialeffect” value="2” > < Param name="fontheight” value="200” > < Param name="Fontcharset” value="0” > < /Object > |
How to link Vb Script with Objects
When you add any control to
your webpage, it can easily be configured, manipulated through its properties,
methods and events.
Now the question arises, What are the properties.
Well Properties means the characteristics of an object. Properties can include items like caption, Background color, foreground color, font size, or the size. Next, questions arises in mind is What are methods.
If you had previously studies any programming language than you might be knowing what methods are. Methods can cause an object to perform a particular task and Events are the actions that are recognized by an object.For example : Submit button recognizes an OnClick event as we have used in our previous examples.
For example :
Now the question arises, What are the properties.
Well Properties means the characteristics of an object. Properties can include items like caption, Background color, foreground color, font size, or the size. Next, questions arises in mind is What are methods.
If you had previously studies any programming language than you might be knowing what methods are. Methods can cause an object to perform a particular task and Events are the actions that are recognized by an object.For example : Submit button recognizes an OnClick event as we have used in our previous examples.
For example :
<
script language=”Vbscript” >
Sub cmdCalculatesalary_onClick Dim payrate,totpay,hoursworked Hrsworked=InputBox(“Enter how many hours you have worked this week”) Payrate=InputBox(“Enter to Pay rate”) totpay=hrsworked * payrate lbltotpay.caption=totpay End Sub < /script > |
Now in the above example the caption is the property of lbltotpay and is used to set the results of our calculation.
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 AmountofTax Dim Subtotal Dim TAX_RATE Dim TotalCost ' Define our constant values. TAX_RATE = 0.06 ' Perform order calculations. Subtotal = Document.myform.txtQuantity.Value * Document.myform.txtUnitPrice.Value AmountofTax = Subtotal * TAX_RATE TotalCost = Subtotal + AmountofTax ' Display the results. Document.myform.lblSubtotal.Caption = Subtotal Document.myform.lblTaxes.Caption = AmountofTax Document.myform.lblTotalCost.Caption = TotalCost End Sub -- > < /SCRIPT > < /HEAD > < BODY > < FORM NAME="myform" > < TABLE > < TR > < TD >< B >Quantity:< /B >< /TD > < TD >< INPUT TYPE="Text" NAME="txtQuantity" SIZE=5 >< /TD > < /TR > < TR > < TD >< B >Unit price:< /B >< /TD > < TD >< INPUT TYPE="Text" NAME="txtUnitPrice" SIZE=5 >< /TD > < /TR > < TR > < TD >< INPUT TYPE="Button" NAME="cmdCalculate" VALUE="Calculate Cost" >< /TD > < TD >< /TD > < /TR > < TR > < TD >< B >Subtotal:< /B >< /TD > < TD > < OBJECT ID="lblSubtotal" WIDTH=45 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="FontWeight" VALUE="0" > < /OBJECT > < /TD > < /TR > < TR > < TD >< B >Taxes:< /B >< /TD > < TD > < OBJECT ID="lblTaxes" WIDTH=45 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="FontWeight" VALUE="0" > < /OBJECT > < /TD > < /TR > < TR > < TD >< B >Total Cost:< /B >< /TD > < TD > < OBJECT ID="lblTotalCost" WIDTH=45 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="FontWeight" VALUE="0" > < /OBJECT > < /TD > < /TR > < /TABLE > < /FORM > < /BODY > < /HTML > |
The output on the browser would be as under:
Here the script has
been modified to remove the MsgBox function and in its place we have set the
caption property of the three label controls.See the statements we have used in
the code.
' Display the results.
Document.myform.lblSubtotal.Caption = Subtotal
Document.myform.lblTaxes.Caption = AmountofTax
Document.myform.lblTotalCost.Caption = TotalCost
Just understand these points and you will be able to know how to reference a textbox or control.
Document means -- > Our web document
Myform means -- > The form on which Active X controls are placed.
LblTaxes -- > The name of the control.
Caption -- > The Property to set.
Hopefully now you are able to understand how to use the above statements in your vbscripts.
' Display the results.
Document.myform.lblSubtotal.Caption = Subtotal
Document.myform.lblTaxes.Caption = AmountofTax
Document.myform.lblTotalCost.Caption = TotalCost
Just understand these points and you will be able to know how to reference a textbox or control.
Document means -- > Our web document
Myform means -- > The form on which Active X controls are placed.
LblTaxes -- > The name of the control.
Caption -- > The Property to set.
Hopefully now you are able to understand how to use the above statements in your vbscripts.
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”)
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 . VB Script - How to use VBScript with Forms
In this tutorial you will learn about How to use VBScript with Forms. Introduction, How to do validate your Forms and Conclusion
Introduction
Now this is the last topic of the tutorial and you will
learn how to validate and submit the data to the web server.
How to do validate your Forms
The process to validate the
forms are as under. The two things should be checked.
1. All of the required data is entered.
2. The data entered is valid or not.
We will learn this by an example.
1. All of the required data is entered.
2. The data entered is valid or not.
We will learn this by an example.
<
html > < head > < title > Vb Script example< /title > < script language=”vbscript” > Option Explicit Sub cmdSubmit_onClick ‘ Check whether user has entered the required text or not. If(Len(document.myform.txtage.value)= 0) then MsgBox “Please enter your age before submitting.” Exit Sub End If ‘ Check the data entered is numeric or not. If ( Not (IsNumeric(document.myform.txtage.value))) then MsgBox “Please enter numeric data” Exit Sub End If ‘Check the range of the age If (document.myform.txtage.value < 0) or (document.myform.txtage.value > 100) then Msgbox “Age entered is Invalid.” Exit Sub End If ‘If all is fine submit the data MsgBox “Thanks for providing your age.” Document.myform.submit End sub < /script > < /head > < body > < form name=”myform” > < table > < tr > < td >Please enter your age:< /td > < td >< Input type=”text” name=”txtage” size=”2” > < /tr > < tr > < td > < /td > < td >< Input type=”Button” name=”cmdSubmit” value=”Submit” >< /td > < /tr > < /table > < /form > < /body > < /html > |
In the above example first we are checking whether the user has entered any value or not. If he has not entered any value we will prompt to enter any value. Second validation is that if he has entered any value , just check whether the data entered is numeric or not. If the data is not numeric prompt him to enter the numeric value otherwise move to next step. In the next step we are validating that the age value is entered as per the prescribed range or not.
At last if all the validations are met by the user , he will be shown a message that “Thanks for providing your age” and this data will be submitted to the web server.
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 If (Len(Document.myform.txtQuantity.Value) = 0) Then MsgBox "You must enter a quantity." Exit Sub End If If (Not IsNumeric(Document.myform.txtQuantity.Value)) Then MsgBox "Quantity must be a numeric value." Exit Sub End If If (Len(Document.myform.cmbProducts.Value) = 0) Then MsgBox "You must select a product." Exit Sub End If ' 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 cmdSubmit_onClick ' Submit this order for processing. MsgBox "Your order has been submitted." Document.myform.Submit 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="BackColor" value="2147483653" > < param name="ForeColor" value="2147483656" > < param name="MaxLength" value="0" > < param name="BorderStyle" value="0" > < param name="ScrollBars" value="0" > < param name="DisplayStyle" value="1" > < param name="MousePointer" value="0" > < param name="Size" value="2646;635" > < param name="PasswordChar" value="0" > < param name="ListWidth" value="0" > < param name="BoundColumn" value="1" > < param name="TextColumn" value="65535" > < param name="ColumnCount" value="1" > < param name="ListRows" value="8" > < param name="cColumnInfo" value="0" > < param name="MatchEntry" value="2" > < param name="ListStyle" value="0" > < param name="ShowDropButtonWhen" value="0" > < param name="ShowListWhen" value="1" > < param name="DropButtonStyle" value="1" > < param name="MultiSelect" value="0" > < param name="Value" value > < param name="Caption" value > < param name="PicturePosition" value="458753" > < param name="BorderColor" value="2147483654" > < param name="SpecialEffect" value="2" > < param name="Accelerator" value="0" > < param name="GroupName" value > < param name="FontName" value="MS Sans Serif" > < param name="FontEffects" value="1073741824" > < param name="FontHeight" value="200" > < param name="FontOffset" value="0" > < 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="VariousPropertyBits" value="8388635" > < param name="Caption" value > < param name="PicturePosition" value="458753" > < param name="Size" value="2646;635" > < param name="MousePointer" value="0" > < param name="BorderColor" value="2147483654" > < param name="BorderStyle" value="0" > < param name="SpecialEffect" value="2" > < param name="Accelerator" value="0" > < param name="FontName" value="MS Sans Serif" > < param name="FontEffects" value="1073741824" > < param name="FontHeight" value="200" > < param name="FontOffset" value="0" > < 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="VariousPropertyBits" value="8388635" > < param name="Caption" value > < param name="PicturePosition" value="458753" > < param name="Size" value="2646;635" > < param name="MousePointer" value="0" > < param name="BorderColor" value="2147483654" > < param name="BorderStyle" value="0" > < param name="SpecialEffect" value="2" > < param name="Accelerator" value="0" > < param name="FontName" value="MS Sans Serif" > < param name="FontEffects" value="1073741824" > < param name="FontHeight" value="200" > < param name="FontOffset" value="0" > < 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="VariousPropertyBits" value="8388635" > < param name="Caption" value > < param name="PicturePosition" value="458753" > < param name="Size" value="2646;635" > < param name="MousePointer" value="0" > < param name="BorderColor" value="2147483654" > < param name="BorderStyle" value="0" > < param name="SpecialEffect" value="2" > < param name="Accelerator" value="0" > < param name="FontName" value="MS Sans Serif" > < param name="FontEffects" value="1073741824" > < param name="FontHeight" value="200" > < param name="FontOffset" value="0" > < 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="VariousPropertyBits" value="8388635" > < param name="Caption" value > < param name="PicturePosition" value="458753" > < param name="Size" value="2646;635" > < param name="MousePointer" value="0" > < param name="BorderColor" value="2147483654" > < param name="BorderStyle" value="0" > < param name="SpecialEffect" value="2" > < param name="Accelerator" value="0" > < param name="FontName" value="MS Sans Serif" > < param name="FontEffects" value="1073741824" > < param name="FontHeight" value="200" > < param name="FontOffset" value="0" > < 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="VariousPropertyBits" value="8388635" > < param name="Caption" value > < param name="PicturePosition" value="458753" > < param name="Size" value="2646;635" > < param name="MousePointer" value="0" > < param name="BorderColor" value="2147483654" > < param name="BorderStyle" value="0" > < param name="SpecialEffect" value="2" > < param name="Accelerator" value="0" > < param name="FontName" value="MS Sans Serif" > < param name="FontEffects" value="1073741824" > < param name="FontHeight" value="200" > < param name="FontOffset" value="0" > < 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="VariousPropertyBits" value="8388635" > < param name="Caption" value > < param name="PicturePosition" value="458753" > < param name="Size" value="2646;635" > < param name="MousePointer" value="0" > < param name="BorderColor" value="2147483654" > < param name="BorderStyle" value="0" > < param name="SpecialEffect" value="2" > < param name="Accelerator" value="0" > < param name="FontName" value="MS Sans Serif" > < param name="FontEffects" value="1073741824" > < param name="FontHeight" value="200" > < param name="FontOffset" value="0" > < 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="cmdSubmit" VALUE="Submit Order" >< /TD > < TD >< /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 > |
The output on the browser would be as follows.
Conclusion
This all wraps up our vbscript tutorials . Till now you
must be aware of the basics and how to use vbscript with your web pages.
Rewrite ; Source
article : http://www.exforsys.com/tutorials/vbscript.html
Good Luck !!!
Thanks for visit
Visual Basic Script (VBScript) : Full Tutorials
Reviewed by Maulpetru
on
2:42 AM
Rating:
No comments: