Exforsys

Home arrow Technical Training arrow VBScript Tutorials

VB Script - Working with Variables Page - 2

Page 2 of 2
Author : Exforsys Inc.     Published on: 20th Apr 2006

VB Script - Working with Variables

.

.


Ads


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 >

Ads

The output on the browser would be as under :

The commonly used operands are addition,subtraction, multiplication and subtraction. The result of calculations are stored in a variable called subtotal.All the calculations are than displayed by using the MsgBox function. The ampersand character is used to concatenate two strings.

Conclusion :

Till now you must have learn the types of variables the vbscript uses. How to declare variables and use it within the script and what a comment line in a script.



 
This tutorial is part of a VBScript Tutorials tutorial series. Read it from the beginning and learn yourself.

VBScript Tutorials

 

Comments