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!


Lesson 4-Controlling Your VBScript Routines

VBScript allows you to control how your scripts process data through the use of conditional and looping statements. By using conditional statements you can develop scripts that evaluate data and use criteria to determine what tasks to perform. Looping statements allow you to repetitively execute lines of a script. Each offers benefits to the script developer in the process of creating more complex and functional web pages.

Conditional Statements

VBScript provides two forms of conditional statements:

If..Then..Else

Select..Case

If..Then..Else

The If..Then..Else statement is used, first to evaluate a condition to see if it is true or false and second, depending upon the condition, to execute a statement or set of statements. Rather than discussing an If statement in theory, we will examine some examples to see how they work.

The simplest version of an If statement is one that contains only a condition and a single statement:

If AmountPurchased > 10000 Then 
    DiscountAmount = AmountPurchased * .10

In this example statement the condition is:

If AmountPurchased > 10000

which simply checks to see if the contents of the variable AmountPurchased is greater than ten thousand. If it is, the condition is true. In this simple version of the If statement when the condition is true the following statement is executed:

DiscountAmount = AmountPurchased * .10

Next we will look at a more complicated version of the If statement. In this version we will perform a series of statements when the condition is true:

If AmountPurchased > 10000 Then 

  DiscountAmount = AmountPurchased * .10

  Subtotal = AmountPurchased - DiscountAmount

End If

In this form of the If statement, one or more statements can be executed when the condition is true, by placing them between the If statement on top and the End If statement on the bottom.

The next form of the If statement uses the If..Then..Else format. This version of the If statement differs from the two previous versions in that it will perform one set of statements if the condition is true and another set when the condition is false:

If AmountPurchased > 10000 Then 

  DiscountAmount = AmountPurchased * .10

  Subtotal = AmountPurchased - DiscountAmount

Else

  HandlingFee = AmountPurchased *.03

  Subtotal = AmountPurchased + HandlingFee

End If

In this example when the condition is true, that is the customer's order is over $10,000, they receive a 10% discount. When the order is under $10,000, they are charged a 3% handling fee.

The final version of the If statement that we will look at is the If..Then..Else If. In this form the If statement checks each of the conditions until it either finds one that is true or an Else statement:

If AmountPurchased > 10000 Then 

  DiscountAmount = AmountPurchased * .10

  Subtotal = AmountPurchased - DiscountAmount

Else If AmountPurchased > 5000 Then

  DiscountAmount = AmountPurchased * .05

  Subtotal = AmountPurchased - DiscountAmount

Else

  HandlingFee = AmountPurchased *.03

  Subtotal = AmountPurchased + HandlingFee

End If

In this example the customer receives a 10%discount for orders over $10000, a 5% discount for orders over $5000 and a handling fee of 3% for orders under $5000.

As you see, VBScript offers you plenty of options when it comes to If statements.

Select Case

The Select Case statement provides an alternative to the If..Then..Else statement, providing additional control and readability when evaluating complex conditions. It is well suited for situations where there are a number of possible conditions for the value being checked. Like the If statement the Select Case structure checks a condition, and based upon that condition being true, executes a series of statements.

The syntax of the Select Case statement is:

Select Case condition

  Case value

  Case value

  ...

  Case Else

End Select

For example, the following Select statement assigns different shipping fees based upon the State where the order is being sent:

Select Case Document.frmOrder.txtState.Value

  Case "California"

    ShippingFee= .04

  Case "Florida"

    ShippingFee = .03

  Case Else

    ShippingFee = .02

End Select

The Select Case statement checks each of the Case statements until it finds one that will result in the condition being true. If none are found to be true, it executes the statements within the Case Else.

Note

Even though it is not required, always include a Case Else when working with Select Case statements to process conditions that you may not have considered possible. For these conditions you can display something as simple as a message dialog to inform you that a branch was executed that you hadn't planned for.

Have a question about VBScript? The Intranet Journal community can help. Start a thread in the Intranet Journal Discussion Forum.

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

email this page

Tutorials
and more at:
Intranet Journal's Tutorials
Intranet Journal Favorites

Creating a PHP-Based Content Management System

The Spyware Guide

Introduction to Microsoft SharePoint Portal

Intranet Journal
Part of the EarthWeb Network

Managing Editor
Intranet Journal

Tom Dunlap

EarthWeb Home Page
Jupitermedia Home Page

Media Kit




The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers