Monday, May 10, 2010

VB Script - 2

VB Script Operators

An operator is a symbol which helps the user to command the computer to do a certain mathematical or logical manipulations. Operators are used in VB scripting to operate on 'data' and 'variables'.

VB script has following types of Operators.
Click here for the list of operators used in Vb script.

The operator precedence is an important concept.

Always, Parenthesis takes the first precedence.

For example:
a = (2*3*3) / 18
Here, 2*3*3 will be calculated first which is 18 and then divided by 18.
There fore, a = 1.

The standard operator precedence is as follows.
Arithmetic operators - First
Comparison operators - Next
Logical operators - In the end

*************************************************
We are just two more topics away before we start VBScript conditional statements.
*************************************************

VB Script Constants

What is a Constant?

A constant is a meaningful name that takes the place of a number or string and never changes. VBScript defines a number of intrinsic constants(built-in). You can get information about these built-in constants by clicking here.

Creating Constants

You create user-defined constants in VBScript using the Const statement. Using the Const statement, you can create string or numeric constants with meaningful names and assign them literal values.
For example:

Const MyString = "This is my string."
Const MyAge = 49

Note that the string literal is enclosed in quotation marks (" "). Quotation marks are the most obvious way to differentiate string values from numeric values. Date literals and time literals are represented by enclosing them in number signs (#).

For example:

Const CutoffDate = #6-1-97#

You may want to adopt a naming scheme to differentiate constants from variables. This will prevent you from trying to reassign constant values while your script is running. For example, you might want to use a "vb" or "con" prefix on your constant names, or you might name your constants in all capital letters. Differentiating constants from variables eliminates confusion as you develop more complex scripts.

Input / Output operations
--------------------------------

InputBox function
The InputBox function displays a dialog box, where the user can write some input and/or click on a button. If the user clicks the OK button or presses ENTER on the keyboard, the InputBox function will return the text in the text box. If the user clicks on the Cancel button, the function will return an empty string ("").

Syntax
InputBox(prompt[,title][,default][,xpos][,ypos][,helpfile,context])

Example:

Dim Input
Input = InputBox("Enter your name")
MsgBox ("You entered: " & Input)

MsgBox Function
The MsgBox function displays a message box, waits for the user to click a button, and returns a value that indicates which button the user clicked.

The MsgBox function can return one of the following values:

1 = vbOK - OK was clicked
2 = vbCancel - Cancel was clicked
3 = vbAbort - Abort was clicked
4 = vbRetry - Retry was clicked
5 = vbIgnore - Ignore was clicked
6 = vbYes - Yes was clicked
7 = vbNo - No was clicked

Syntax
MsgBox(prompt[,buttons][,title][,helpfile,context])

Example:

Dim MyVar
MyVar = MsgBox ("Hello World!", 65, "MsgBox Example")

' MyVar contains either 1 or 2, depending on which button is clicked.

*************************************************
Coming soon with my next posting
*************************************************

No comments:

Post a Comment