Your Second VBScript Exercise

In this exercise we will create a page that performs a simple calculation involving sub-totals, sales tax and final totals. Follow the step-by-step instructions that will introduce you to using variables with VBScript. In this exercise you will create an HTML document which contains a script that will retrieve data from a web page, perform calculations and output a result.

Creating the HTML Document

A completed copy of this part of the exercise can be found in the file exer2_v1.html.

Open up a text editor and insert the following HTML code:

<HTML>

<HEAD>

<TITLE>Working With VBScript: Exercise 2</TITLE>

</HEAD>

<BODY>

<H1>Your Second VBScript Exercise</H1>

<P> Variables can be used to store and manipulate values. To 

see a demonstration of this enter a quantity and unit price 

in the fields below and click the "Calculate Cost" button.</P>

<FORM NAME="frmExercise2">

  <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>

Save the file, and load it into Internet Explorer. The result is shown below.

Your Second VBScript Exercise

 

Adding VBScript

In this part we will be adding a script to provide functionality for when the Calculate Cost command button is clicked. A completed copy of this part of the exercise can be found in the file exer2_v2.html.

Re-open the HTML document that you created in part 1, if necessary. Modify the document adding the scripting lines as shown by the shading:

Please note that the apostrophes " are there to comment out code - there is more on this on the next page - and the _at the end of the line Subtotal = document.frmExercise2.txtQuantity.value_ is a coding convention which is telling you to type the following line on the same line as this one and to discard the _.

<HTML>

<HEAD>

<TITLE>Working With VBScript: Exercise 2</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.frmExercise2.txtQuantity.value _

           * document.frmExercise2.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> 

...

  • Save the file and test it by loading it into Internet Explorer. Enter 100 into the Quantity field and 10 into the Unit Price field. Try out the Calculate Cost button. The result is shown below:

Exercise 2


How It Works

What should be obvious right from the start is that this script is far more involved than the one used with Exercise 1. Don't be intimidated by its size. As with the previous lesson, we will work through this script line-by-line.

After the starting <SCRIPT> tag and HTML comment we find:

Option Explicit

Do you remember what this statement does? It forces you to declare all of your variables.

Next we create a sub procedure for the click event of the cmdCalculate button.

Sub cmdCalculate_OnClick

Following that we declare seven variables, three of which we are going to use as constants. They can be identified by the fact that they are all in uppercase. In VBScript, case doesn't matter (though it does in JavaScript). We are using it to make the script easier to read. Are the variables procedure-level or script-level variables? They are procedure-level since they are declared within a procedure.

In VBScript, anything after an apostrophe is a comment. As such, they are ignored when the script is processed. Comments can appear on a line by themselves or at the end of a line of script. Comments at the end of a line are referred to as inline comments.

' Define our constant values.

The constants are assigned values in the following lines. Chr() is a VBScript function that returns the character associated with a specified ASCII code. ASCII codes 13, 10 and 9 are carriage return, line feed and tab, respectively.

CRLF = Chr(13) & Chr(10)

TABSPACE = Chr(9)

The next line demonstrates how values are taken from a form on a web page, and used within a script. The two fields on our form were named txtQuantity and txtUnitPrice in their HTML <INPUT> tags. The form was named frmExercise2. Here we are referencing our web document, then the form, then the input field and finally the value of that field. The value associated with each field contains what the user entered into that field on the web page. The * says to multiply the value of the first field, txtQuantity, by the second field, txtUnitPrice.

Note

The commonly used VBScript operands are + for addition, - for subtraction, * for multiplication and / for division.

The result of this calculation is then stored in the variable Subtotal. Next we perform some additional calculations. Finally, we display the result of our calculations using the MsgBox function. The ampersand character, &, is used to concatenate two strings.

As with the previous lesson, don't get too worried about understanding all of the details of this example right now. As you continue to work with VBScript you will begin to "pickup" the language.

Summary

That completes Exercise 2. You just created a web page that interacts with the user to gather data, perform calculations and present results-the fundamental components of most applications. Along the way you have learned:

  • The types of variables that VBScript supports
  • How to declare and use variables within a script
  • A technique to work around the absence of constants in VBScript
  • What a comment line is in a script

In the next lesson we will look at objects. You will learn what they are and how they are used with VBScript.


Previous  Next

Intranet Journal
VBScript Tutorial
Contents
· What is VBScript?
· How to Use this Tutorial

Lesson 1
· Adding VBScript to Web Pages
· The <SCRIPT> Tag
· Non-Supporting Browsers
· Your 1stirst VBScript Exercise

Lesson 2
· Working with Variables
· Declaring Variables
· Scope of Variables
· Constants
· Arrays
· Your 2nd VBScript Exercise

Lesson 3
· Objects and VBScript
· Adding Objects to Web Pages
· Linking VBScript with Objects
· Your 3rd VBScript Exercise

Lesson 4
· Controlling VBScript Routines
· Conditional Statements
· Looping Statements
· Your 4th VBScript Exercise

Lesson 5
· Using VBScript with Forms
· Validating Your Forms
· Your 5th VBScript Exercise

· Summary

OF INTEREST
· Intranet Journal's JavaScript FAQ
· Basic JavaScript by Example