What are VBScript Constants: Best Facts in Just 5 Minutes

VBScript Constants

A constant is such a variable or a meaningful name that takes the place of a number or string and it never changes during runtime. Constants have defined storage. In VBScript there two types of constants:

  1. User-defined or named constants or custom constants
  2. Built-in Constant

Interestingly, we can not change the value of the constants from another script that is outside of our main script.

Named Constants or User-defined constants or custom constants:

Named constants or user-defined constants or custom constants are the constants defined using the const keyword. The named constant’s value can not be changed during the execution time. Named constants have the scope and can be of private or public type.(We have to use const keyword instead of Dim). The scope will follow variable scope qualification.

  • A named constant is similar to a variable as it represents some value but its value can not be changed at runtime.
  • A variable is transient but named constant is static.

Note: Constant value cannot be the output of some function. As they have been designed during design time and run time it will be constant.

Creating Constants:

We create user-defined constants in VBScript using the Const statement. Using the Const statement, we can create a string or numeric constants with meaningful names and assign them literal values.Even though there is no hard and fast rule for User-defined constant but a good practice i to declare as all uppercase. This will clearly differentiate a variable from a constant.

Advantages of Constants:

  • Constants are easier to understand and code.
  • Constants improve code clarity as the constant name carries the actual meaning of usage of the constant.
  • It removes the occasional spelling mistakes.

When to use constants over variables or literals?

Below are the scenarios when we need to use constants instead of variable or literals:
  • If we want to use literals many times in our script, it is better to go with constants instead of variables or literals.
  • if we want to reciprocate the variable name in a proper and clear way, we need to choose constants over variables or literals.
See also  How to Write VBScript using XML?

Const statement

Declares constants for use in place of literal values.


[public|private] const cConstantName=value

public or private is the access control mechanism. const is the keyword to define a constant. Value is the final value. However, the public or private keyword is optional though.

cConstantName is the constant name.


[public|private] Const MyString = "This is my string."
[public|private] Const MyAge = 49
[public|private] Const CutoffDate = #6-1-97# 

Note that String literal is enclosed in quotation marks (” “). Represent Date literals and time literals by enclosing them in number signs (#). We declare multiple constants by separating each constant name and value with a comma. For example:

Const price= 100, city= "Kolkata", x= 27

Scope of constants

a constant declared at the script level has script level scope and a constant declared inside the procedure is having procedure level scope.

Coding conventions of Constants

Constants can be declared by the following way

  1. The constant name should carry the constant.
  2. use capital letters- like COLOR. It is a general convention. And constants are easy to differentiate amongst the other variables.
  3. use con prefix or c prefix- ConColour or color
  4. In case of the constant needs, two consecutive words then use underscore(-) to concatenate them. like cMy_Tickets
  5. Arrays should be prefixed with arr like arrMyTickets

Usage of User-defined Constants

  • We can use constantly it to fix a value that the script will refer to throughout the execution cycle. like cPie=3.14
  • We can use it constantly to create a common title bar for the message box.


const cTitleBar="Welcome"
msgbox "How are you?",cTitleBar
msgbox "Hello there",cTitleBar

When to use named constant and when to use literals?

  • Named constants can reduce the defects in your application as you need to mention literals several times and chances are there that they may be misspelled and can be executed unnoticed.
  • Named constants increase clarity over literals.
  • Named constants are short, clear, and crisp hence easy to maintain, read and debug.

VBScript also provides a set of Inbuilt Constant’s:

They are as follows:

VarType Constants:

They are useful to determine the subtype of a variant.
VbEmptyUninitialized or default
VbNull1Contains nothing or no valid data
VbVariant12Array -Variant
VbDataObject13Data Object
See also  How to Work With Arrays in VBScript or UFT?

Tristate Constants:

These constants are useful to switch arguments:
TristateUseDefault/VbUseDefault−2It is taken from computer default regional settings
TristateTrue/VbTrue−1Denotes True
TristateFalse/VbFalse  0Denotes False

String Constants:

String constants allow us to modify Strings by inserting the below constants:
VbCrChr(13)Carriage return.
VbCrLfChr(13) & Chr(10)Carriage return along with line feed
VbFormFeedChr(12)Form feed.Not much useful while working with windows and Testing.
VbLfChr(10)Line feed.very useful
VbNewLineChr(13) or Chr(10)computer specific newline
VbNullCharChr(0)Signifies a Character with 0 value
VbNullStringString with no valueit contains no value.
VbTabChr(9)Equivalent to press a Horizontal tab.
VbVerticalTabChr(11)Equivalent to press a Vertical tab.

Note: VbNullString is not the same as a String containing ” ” that is a space.

Color Constants:

These constants are useful for advanced scripting where we design HTML reporting for test automation. Passed test cases will take the TR color aVbGreen, Failed test cases will take VbRed whereas the Table structure will be in VbBlue or VbBlack and Warning test cases will take VbCyan.

Comparison Constants

These constants generally are used to switch between textual and binary comparison. In UFT/QTP we generally use the first two.

VbBinaryCompareused to perform a binary comparison
VbTextCompare1used to perform a plain textual comparison.
vbUseSystemDayOfWeekUsed to calculate the first day of the week as per local computer
vbFirstJan11Used to calculate the week in which 1st January happens to be the first week of the year.
VbFirstFourDays2Used to calculate the week which is having at least four first days in the new year.
VbFirstFullWeek3Used to calculate the first full week of the year.

Msgbox Constants:

These constants signify that which of the buttons and icons appear on the message box, along with the default. Some of the constants are also used to determine the modality of the MsgBox.

See also  Learn File System Object(FSO) in UFT or QTP or VBScript Free
VbOKOnlyDisplays the Ok option only
VbOKCancel1Displays the OK and Cancel option
VbAbortRetryIgnore2Display the below options:

  • Abort
  • Retry
  • Ignore
VbYesNoCancel3Display the below options:

  • Yes
  • No
  • Cancel
VbYesNo4Display the below options:

  • Yes
  • No
VbRetryCancel5Display the below options:

  • Retry
  • Cancel
VbCritical16Displays the critical message icon
VbQuestion32Displays the warning query icon
VbExclamation48Displays the warning message icon
VbInformation64Displays the information message icon
VbDefaultButton1‘0The first button is the default.
VbDefaultButton2256The second button is the default.
VbDefaultButton3512The third button is the default.
VbDefaultButton4768The fourth button is the default.
VbApplicationModalIt signifies a modal that means the user needs to choose some option
VbSystemModal4096It signifies a modal that means the user needs to choose some option and it will be always on top of the application.
VbOK1Tracked if Ok button is clicked
VbCancel2Tracked if Cancel button is clicked
VbAbort3Tracked if Abort button is clicked
VbRetry4Tracked if Retry button is clicked
VbIgnore5Tracked if Ignore button is clicked
VbYes6Tracked if Yes button is clicked
VbNo7Tracked if No button is clicked

Error Constant

VbObjectError2147221504Indicates as the base of the User-defined Error number.

Date Constants

VbGeneralDateDisplays  date,time or both as per local system
VbLongDate1Display date of the local system in long format
VbShortDate2Display date of the local system in short format
VbLongTime3Display time of the local system in long format
VbShortTime4Display time of the local system in short format
VBUseSystemDisplays the date format as per your computer

How to use the runtime constants?

Option Explicit
Dim todatDate
if (todayDate=VBMonday) Then
msgbox "working day"
End if


Share and Enjoy !

Leave a Reply

Your email address will not be published. Required fields are marked *