Intranet Journal   Earthweb  
Events Jobs Premium Services Media Kit Network Map E-mail Offers Vendor Solutions Webcasts

   Intranet Journal Subjects
Search Earthweb

Privacy Policy



internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

internet commerce
Be a Commerce Partner
















 

[ Home | Discussion Forum | How Do I... | Lotus Notes Intranets | Microsoft SharePoint | Products | Shopping  ]

free news!


Your Third VBScript Exercise

In Exercise 3 we modify the web page created in Exercise 2. These modifications will be made so that we can display the results of our calculations not with the MsgBox function, but rather to ActiveX objects that are part of the page. Just follow the step-by-step instructions below to begin learning how to use VBScript with ActiveX.

Exercise 3: Working with Objects

In this exercise, you will create an HTML document that contains a script that will retrieve data from a web page, perform calculations and output a result back to the web page.

Testing the HTML Document

Load the file exer3_v1.html into a text editor. This is the HTML component of this exercise already typed in for you. Look over the HTML document. It contains three ActiveX label controls named lblSubtotal, lblTaxes and lblTotalCost. Save the file under a different name. We are going to be modifying this source and wouldn't want to work with the original.

Test the file by loading it into Internet Explorer. The result is shown below. I'd have you try out the Calculate Cost button, but you have probably already figured out from the previous two exercises that it doesn't do anything.

Figure: 0685_apph_05


As we did in Exercise 2, we will now add a script to provide functionality for the Calculate Cost command button's click event.

Adding VBScript

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

We're going to modify the document, by adding the scripting lines as shown by the shading below:

<HTML>

<HEAD>

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

           * document.frmExercise3.txtUnitPrice.value

  AmountofTax = Subtotal * TAX_RATE

  TotalCost = Subtotal + AmountofTax

' Display the results.

  document.frmExercise3.lblSubtotal.caption = Subtotal

  document.frmExercise3.lblTaxes.caption = AmountofTax

  document.frmExercise3.lblTotalCost.caption = TotalCost

End Sub

-->

</SCRIPT>

</HEAD>

...

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:

Working With VBScript: Exercise 3


How It Works

Exercise 3 was just a modification of Exercise 2. As such, we will focus on how they differ, rather than going over the script line by line again.

There were minimal changes involving variable declarations and the defining of constant values. We simply didn't need them in this version, so they were removed.

Dim AmountofTax

Dim Subtotal

Dim TAX_RATE

Dim TotalCost

' Define our constant values.

TAX_RATE = 0.06

We won't discuss the method used to calculate the subtotal, taxes and total amount, as it is identical between the two versions.

The way results are displayed is different in Example 3. The script has been modified to remove the MsgBox function and in its place we set the caption property of three label controls.

' Display the results.

document.frmExercise3.lblSubtotal.caption = Subtotal

document.frmExercise3.lblTaxes.caption = AmountofTax

document.frmExercise3.lblTotalCost.caption = TotalCost

The format used when referencing properties is:

 
document Our web document
frmExercise3 The form on which the ActiveX controls were placed
lblTaxes The name of the control
caption The property to set

Hopefully, by this point you are starting to get comfortable reading and working with VBScript. The best way to strengthen your knowledge of VBScript is to take some of the examples that we have been working with in the first three lessons and modify them to suit your own needs.

Summary

Well that's it for Exercise 3. I know, objects are a pretty hefty topic for a small lesson. What we wanted to do was to give you an exposure to objects and how they can be utilized in VBScript Along the way, you have learned:

  • What objects are and how they could be used with VBScript

  • About properties, methods and events

Next is a lesson in how you can control your script files using conditional and looping statements.


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