Exforsys.com
 

Sponsored Links

 

VBScript Tutorials

 
Home Tutorials VBScript
 

VB Script - Working with Variables

 

VB Script - Working with Variables - Page 2

Page 2 of 2


.


.


 




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 >


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.




First Page: VB Script - Working with Variables


Read Next: Working with Objects in VB Script



 

 

Comments


sudhap said:

  hi
in the above program what is document variable and from where it comes
i tried this program
but it is not working
waiting for reply
veena
March 8, 2007, 8:15 am

Rick Shultz said:

  Good basic tutorial. Thanks.
June 24, 2008, 5:05 am

ghaazi said:

  hi

I have a problem i want to convert a value into a variable how can i do that? here is my sample code.

'dim onjName11
'dim onjName12
'dim onjName21
'dim onjName22

onjName11=22222222
onjName12=33333333
onjName21=44444444
onjName22=55555555

for i = 1 to 2
vale="onjName" & i &"1"

WScript.Echo vale
next
June 10, 2009, 2:36 pm

Mukesh said:

  Dear Ghaazi,
Its never possible to use for loop for values within a text

Regards,
Mukesh
June 25, 2009, 4:39 am

Scott said:

  VBScript does support contants, as of version 5: http://msdn.microsoft.com/en-us/library/16twy8ed(VS.80).aspx
July 22, 2009, 4:09 pm

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2009 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape