Welcome to www.programmer2programmer.net Its all about programming


programmer2programmer.net

 Tips
 Microsoft Certification
 Project Idea NEW
 Connection Strings
 Password Recovery
 SQL Injection
 Encryption & Decryption
  
 LIVE Academic Project
 Project #1 - VB6, Access
  Project #2 - VB.Net, SQL
 Project #3 - ASP, Access
 Project #4 - C#, SQL
 Project #5 - VB6, SQL
 Project #6 - Steganography
 Download MBA Project
  2012 New Projects Ad
  
 University Question Paper and Assignment
 SMU - Question Paper
 SMU - Assignment
 SCDL - Assignment
  
 Interview Question Answer
 General & HR Round
 Visual Basic 6
 VB.Net & C#
 ASP.Net
 SQL Server
  Oracle and DBA NEW
 My SQL
  

100% Tested
Ready Academic IT Projects

BE, ME, BTech, BCA, MCA, MBA, Bsc-IT, MS, BIT, ADIT, DOEACC, IGNOU, SMU
readymadeproject.com
One stop solution for VB6, VB.Net, C#, ASP.Net, Crystal Report, Oracle, SQL Server, MySql, PHP, XML, AJAX ....
Home Personal Member Forum Source Project Tips Contact  
 

 

VB Script Built in functions

You will find comprehensive VB Script function list, Here I provide alphabetic order of all VB Script bult in funtion, all functions are described and provide sample code for how to use the function.

You can found more sample code in VB Script sample code section.

 

Sr

Functions

Descriptins and Use

1

Abs

Abs(expression)

Returns the absolute value of a number.

Dim MyNumber
MyNumber = Abs(50.3)  ' Returns 50.3
MyNumber = Abs(-50.3) ' Returns 50.3

2

Array

Array(argumentList)

Returns a Variant containing an array

Dim A      ' Declare array

A = Array(50,20,30) ' Store 3 element

B = A(2)   ' B is now 30.

3

Asc

Asc(character)

The Asc() function returns the character code corresponding to the character argument, and it works on all sys-tems, regardless of whether they support Unicode characters.

 
If you call the Asc() function with a string instead of a character, the character code of the string’s first character is returned.

Dim MyNumber
MyNumber = Asc("A")  ' Returns 65
 

4

Atn

Atn(expression)

This function returns the arctangent of an angle. The value returned is in radians. To convert it to degrees, multiply by 180/¹, where ¹ is 3.14159…. To calculate ¹ with double precision, use the following statement: Atn(1)*4
 

5

CBool

CBool(expression)

Converts its argument to Boolean (True/False) type; evaluates to True if expression evaluates to any nonzero value.
 

6

CByte

CByte(expression)

Converts its argument to Byte type.
 

7

CCur

CCur(expression)

Converts its argument to Currency type.
 

8

CDate

CDate(expression)

Converts its argument to Date type.
 

9

CDbl

CDbl(expression)

Converts its argument to Double type.
 

10

Chr

Chr(number)

The Chr() function is the inverse of the Asc() function and returns the character associated with the specified character code. Use this function to print characters that don’t appear on the key-board (such as line feeds or special symbols).

Dim MyChr
MyChr = Asc(65)  ' Returns A
 

11

CInt

CInt(expression)

Converts its argument to Integer type.
 

12

CLng

CLng(expression)

Converts its argument to Long type.
 

13

Conversions

 

14

Cos

Cos(expression)

This function returns the cosine of an angle. The value of angle must be expressed in radians.
 

15

CreateObject

 

16

CSng

CSng(expression)

Converts its argument to Single type.
 

17

CStr

CStr(expression)

Converts its argument to String type.
 

18

Date

Date()

This function returns the current system date.

The following statement:
MsgBox "The system date is " & Date()
display a date such as 9/22/1998 in a message box.
 

19

DateAdd

DateAdd(interval, number, date)

This function returns a date that corresponds to a date plus some interval. The interval variable is a time unit (days, hours, weeks, and so on), number is the number of intervals to be added to the initial date, and date is the initial date. If number is positive, the date returned by DateAdd is in the future. If it’s negative, the date returned is in the past.

To find out the date one month after January 31, 1996, use the following statement:
Dim MyD
MyD = DateAdd("m", 1, "31-Jan-96")
The result is: 2/29/96

 

20

DateDiff

DateDiff(interval, date1, date2[, firstdayofweek[, firstweekofyear]])

This function is the counterpart of the DateAdd() function and returns the num-ber of intervals between two dates. The interval argument is the interval of time you use to calculate the difference between the two dates.

The date1 and date2 arguments are dates to be used in the calculation, and firstdayofweek and firstweekofyear are optional arguments that specify the first day of the week and the first week of the year.

 

21

DatePart

DatePart(interval, date[,firstdayofweek[, firstweekofyear]])

This function returns the specified part of a given date. The interval argument is the desired format in which the part of the date will be returned

Dim day1
Dim D2
day1=#03/23/1996 15:03.30#
D2 = DatePart("yyyy", day1) ' Returns A
 

22

DateSerial

DateSerial(year, month, day)

This function accepts three numeric arguments that correspond to a year, a month, and a day value and returns the corresponding date.

MsgBox DateSerial(1999, 10, 1) displays the string “10/1/99” in a message box.
 

23

DateValue

DateValue(date)

This function returns a variant of type Date. This function is handy if you are doing financial calculations based on the number of days between two dates.

The difference in the following statement:
MsgBox DateValue("12/25/1996") - DateValue("12/25/1993")  ' Returns 1096
 

24

Day

Day(date)

This function returns the day number of the date specified by the argument. The date argument must be a valid date (such as the value of the Date() or the Now() function). If the following function were called on 12/01/95, it would have returned 1.
 

25

Derived Math

 

26

Escape

 

27

Eval

 

28

Exp

Exp(expression)

This function returns the base of the natural logarithms to a power. The expression variable is the power, and its value can be a non integer, positive or negative value. The Exp() function complements the operation of the Log() function and is also called antilogarithm.
 

29

Filter

Filter(InputStrings, Value, Include, Compare)

This function returns a zero-based array containing part of a string array, based on specified filter crite-ria. The InputStrings argument is a one-dimensional array of the strings to be searched, and the Value argument is the string to search for. The last two argu-ments are optional, and they indicate whether the function should contain sub-strings that include or exclude the specified value. If True, the Filter() function returns the subset of the array that contains Value as a substring. If False, the Fil-ter() function returns the subset of the array that does not contain Value as a sub-string. The Compare argument indicates the kind of string comparison to be used.
 

30

FormatCurrency

FormatCurrency(Expression, NumDigitsAfterDecimal, IncludeLeading-Digit, UseParensForNegativeNumbers, GroupDigits)

This function returns a numeric expression formatted as a currency value (dollar amount) using the currency symbol defined in Control Panel. All arguments are optional, except for the Expression argument, which is the number to be formatted as cur-rency. NumDigitsAfterDecimal is a value indicating how many digits will appear to the right of the decimal point. The default value is -1, which indicates that the computer’s regional settings must be used. IncludeLeadingDigit is a tristate con-stant that indicates whether a leading zero is displayed for fractional values. The UseParensForNegativeNumbers argument is also a tristate constant that indicates whether to place negative values within parentheses. The last argument, Group-Digits, is another tristate constant that indicates whether numbers are grouped using the group delimiter specified in the computer’s regional settings.
 

31

FormatDateTime

FormatDateTime(Date, NamedFormat)

This function formats a date or time value. The Date argument is a date value that will be formatted, and the optional argument NamedFormat indicates the date/time format to be used.
 

32

FormatNumber

FormatNumber(Expression, NumDigitsAfterDecimal, IncludeLeading-Digit, UseParensForNegativeNumbers, GroupDigits)

This function returns a numeric value formatted as a number. The arguments of the Format-Number() function are identical to the arguments of the FormatCurrency() function.
 

33

FormatPercent

FormatPercent(Expression, NumDigitsAfterDecimal, IncludeLeading-Digit, UseParensForNegativeNumbers, GroupDigits)

This function returns an expression formatted as a percentage (multiplied by 100) with a trail-ing % character. Its syntax and arguments are identical to the FormatCurrency() and FormatNumber() functions.
 

34

GetLocale

 

35

GetObject

 

36

GetRef

 

37

Hex

Hex(expression)

This function accept a decimal numeric value as an argument and return the hexadecimal representation of the number in a string. The function Hex(47) returns the value “2F”.
 

38

Hour

Hour(time)

This function returns an integer in the range 0 through 24 that rep-resents the hour of the specified time.

Dim h
h = Hour(Now)  
 

39

InputBox

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

The InputBox() function displays a dialog box with a prompt and a TextBox control and waits for the user to enter some text and click on the OK or Cancel button.

Dim VDate
VDate = InputBox("Please enter a date")
If IsDate(Vdate) Then
    MsgBox "You entered proper date"
Else
    MsgBox "Please try again with a valid date"
End If

 

40

InStr

InStr([startPos,] string1, string2[, compare])

The InStr() function returns the position of string2 within string1. The first argument, which is optional, determines where in string1 the search begins. If the startPos argument is omitted, the search begins at the first character of string1.

Str1 = "www.google.co.in"
str2 = "google"
Pos = Instr(str1, str2)    'Return 5

 

41

InStrRev

InStrRev(string1, string2, start, compare)

This function returns the position of one string within another (as does the InStr() function), but it starts from the end of the string. The string1 argument is the string being searched, and the string2 argument is the string being searched for. The other two arguments are optional. The start argument is the starting position for the search. If it is omitted, the search begins at the last character. The compare argument indicates the kind of comparison to be used in locating the substrings, and its values are explained in the Filter entry. If compare is omitted, a binary comparison is performed.
 

42

Int, Fix

Int(expression), Fix(expression)

Both these functions accept a numeric argu-ment and return an integer value. If expression is positive, both functions behave the same. If it’s negative, the Int() function returns the first negative integer less than or equal to expression, and Fix returns the first negative integer greater than or equal to expression. 

Int(-1.3) 'Returns -2,
Fix(-1.3) 'Returns -1.
 

43

IsArray

IsArray(variable)

This function returns True if its argument is an array.

Dim StArray(15)
IsArray(StArray)    'Returns True.

 

44

IsDate

IsDate(expression)

This function returns True if expression is a valid date. Use the IsDate() function to validate user data. Dates can be specified in various formats, and validating them without the help of the IsDate() function would be a task on its own.

Vdate = InputBox("Please enter a valid date")
If IsDate(VDate) Then
    MsgBox "Date accepted"
End If
 

45

IsEmpty

IsEmpty(variable)

This function returns True if the variable is empty. An empty variable hasn’t been initialized or explicitly set to Empty.

After the execution of the following statements:
numVar = 0
stringVar = ""

the variables numVar and stringVar are not empty because they have been initialized. If a variable has been declared with a Dim statement but not initialized or otherwise used by your program, it’s empty. This variable must be initialized before it can be used; you can find out its status with the IsEmpty() function.
 

46

IsNull

IsNull(expression)

This function returns True if expression is Null. A Null value is a non valid value and is different from an Empty value. Regular variables can’t be Null unless you assign the Null value to them. Object variables can be Null.
 

47

IsNumeric

IsNumeric(expression)

This function returns True if expression is a valid number.

age = InputBox("Please enter your age")
If IsNumeric(age) = False Then
    MsgBox("Please try with a valid number")
End If

 

48

IsObject

IsObject(expression)

This function returns a Boolean (True/False) value indicating whether expression represents an object variable.
 

49

Join

Join(list, delimiter)

This function returns a string created by joining a number of substrings contained in an array. The list argument is a one-dimensional array containing substrings to be joined, and the optional delimiter argument is a character used to separate the substrings in the returned string. If it is omitted, the space character (“ “) is used. If delimiter is a zero-length string, all items in the list are concatenated with no delimiters.

Dim StrArray(3)
Dim RString
StrArray(0) = "Microsoft"
StrArray(1) = "VB"
StrArray(2) = "Script"
RString = Join(StrArray," ")
'Return "Microsoft VB Script"
 

50

LBound

LBound(arrayname[, dimension])

This function returns the smallest sub-script for the indicated dimension of an array. The arrayName variable is the name of the array, and dimension is an integer indicating the dimension whose lower bound will be returned. If dimension is omitted, the first dimension is assumed. The LBound() function is used with the UBound() function, which returns the largest subscript of a dimension of a given array, to determine the size of the array.
 

51

LCase

LCase(string)

The LCase() function accepts a string as an argument and converts it to lowercase.
 

52

Left

Left(string, number)

This function returns a number of characters from the beginning of a string. It accepts two arguments: the string and the number of characters to extract.
 

53

Len

Len(string)

The Len() function returns the length of a string.
 

54

LoadPicture

 

55

Log

Log(expression)

The Log() function returns the natural logarithm of a number. The expression variable must be a positive number.
 

56

LTrim; RTrim; and Trim

LTrim(string), RTrim(string), Trim(string)

These functions trim the spaces in front of, after, and on either side of a string. The are frequently used in validating user input, as in the following.

If Trim(EMail) <> "" Then
   MsgBox "Email address can not be blank"
End If
 

57

Maths

 

58

Mid

Mid(string, start, [length]), MidB(string, start[, length])

The Mid() function returns a section of a string of length characters, starting at position start.

The following function: Mid("09 February, 1957", 4, 8) extracts the name of the month from the specified string. If you omit the length argument, the Mid() function returns all the characters from the starting position to the end of the string. If the specified length exceeds the number of characters in the string after the start position, the remaining string from the start location is returned. 

 

59

Minute

Minute(time)

This function returns an integer in the range 0 through 60 that represents the minute of the specified time.
 

60

Month

Month(date)

This function returns an integer in the range 1 through 12, representing the number of the month of the specified date. Month(Date) returns the current month number.
 

61

MonthName

MonthName(month[, abbreviate])

This function returns the name of the month specified by the month argument (a numeric value, which is 1 for January, 2 for February, and so on). The optional abbreviate argument is a Boolean value that indicates if the month name is to be abbreviated. By default, month names are not abbreviated.
 

62

MsgBox

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

The MsgBox function displays a dialog box with a message and waits for the user to close it by clicking on a button. The message is the first argument (prompt).
 

63

Now

Now()

This function returns both the system date and time, in the same format as they are reported by the Date() and Time() functions.
 

64

Oct

Oct(expression)

This function accept a decimal numeric value as an argument and return the octal representation of the number in a string. Thefunction Oct(47) returns the value “57”.
 

65

Replace

Replace(expression, find, replacewith, start, count, compare)

This function returns a string in which a specified substring has been replaced with another substring a specified number of times. The expression argument is a string containing the string to be replaced, on which the Replace function acts. The find argument is the substring to be replaced, and replace with is the replacement string. The remaining arguments are optional. The start argument is the character position where the search begins. If it is omitted, the search starts at the first character. The count argument is the number of replacements to be performed. If it is omitted, all possible replacements will take place. Finally, the compare argument specifies the kind of comparison to be performed. The values of the compare argument are described in the Filter entry.
 

66

RGB

RGB(red, green, blue)

This function returns a Long Integer representing a color value. The red, green, and blue arguments are integer values in the range 0 through 255, representing the values of the three basic colors.
 

67

Right

Right(string, number)

This function is similar to the Left function, except that it returns a number of characters from the end of a string.
 

68

Rnd

Rnd([expression])

This function returns a pseudo-random number in the range 0 to 1. The optional argument is called seed and is used as a starting point in the calculations that generate the random number. If the seed is negative, the Rnd() function always returns the same number. As strange as this behavior may sound, you may need this feature to create repeat-able random numbers to test your code. If seed is positive (or omitted), the Rnd() function returns the next random number in the sequence. Finally, if seed is zero, the Rnd() function returns the most recently generated random number.

In most cases, you don’t need a random number between 0 and 1, but between two other integer values. A playing card’s value is an integer in the range 1 through 13. To simulate the throw of a dice, you need a number in the range 1 through 6.

To generate a random number in the range lower to upper, in which both bounds are integer numbers, use the following statement:

randomNumber = Int((upper - lower + 1)*rnd() + lower)

The following statement displays a random number in the range 1 to 49.

Int(Rnd * 49 + 1)

If you repeat this statement six times, you will get the Lotto’s six lucky numbers.

The sequence of random numbers produced by Visual Basic is always the same! Let’s say you have an application that displays three random numbers. If you stop and rerun the application, the same three numbers will be displayed. This is not a bug. It’s a feature of Visual Basic that allows you to debug applications that use random numbers (if the sequence were different, you wouldn’t be able to re-create the problem). To change this default behavior, call the Randomize statement at the beginning of your code. This statement will initialize the random number generator based on the value of the computer’s Timer, and the sequences of random numbers will be different every time you run the application.
 

69

Round

Round(expression[, numdecimalplaces])

This function returns a numeric expression rounded to a specified number of decimal places. The numdecimalplaces argument is optional and indicates how many places to the right of the decimal are included in the rounding. If it is omitted, an integer value is returned. The expression Round (3.49) returns 3, and the expression Round(3.51) returns 4.
 

70

ScriptEngine

 

71

ScriptEngineBuildVersion

 

72

ScriptEngineMajorVersion

 

73

ScriptEngineMinorVersion

 

74

Second

Second(time)

This function returns an integer in the range 0 through 60 that represents the seconds of the specified time.
 

75

SetLocale

 

76

Sgn

Sgn(expression)

This function returns an integer indicating the sign of its argument: 1 if the argument is greater than zero, 0 if the argument is 0, and –1 if the argument is less than zero. 

 

77

Sin

Sin(expression)

This function returns the sine of an angle, specified in radians.
 

78

Space

Space(number)

This function returns a string consisting of the specified num-ber of spaces. The number argument is the number of spaces you want in the string.
 

79

Split

Split(expression, delimiter, count, compare)

This function is the counter-part of the Join() function. It returns a zero-based, one-dimensional array containing a specified number of substrings. The expression argument is a string that contains the original string that will be broken into substrings, and the optional delimiter argument is a character used to delimit the substrings.
If delimiter is omitted, the space character (“ “) is assumed to be the delimiter. If delimiter is a zero-length string, a single-element array containing the entire expression string is returned. The count argument is also optional, and it determines the number of sub-strings to be returned. If it’s -1, all substrings are returned. The last argument, com-pare, is also optional and indicates the kind of comparison to use when evaluating substrings. Its valid values are described in the Filter entry.

Dim StrS
StrS="Microsoft VB Script"
Dim StrArray()
StrArray=Split(StrS, " ")
 

80

Sqr

Sqr(expression)

This function returns the square root of a positive number. If the argument number is negative, the Sqr() function causes a runtime error, because by definition the square root of a negative number is undefined.

Dim MyNumber
MyNumber = Sqr(25)  'Return 5

 

81

StrComp

StrComp(string1, string2 [, compare])

This function compares two strings and returns a value indicating the result, it gives following result depend on string1 and string2.

-1     string1 is less than string2
0      string2 is equal to string2
1      string1 is greater than string2
Null   string1 and/or string2 is Null
 

82

String

String(number, character)

This function returns a string of number characters, all of which are character.

String(12, "*") 'Returns "************"
 

83

StrReverse

StrReverse(string)

This function reverses the character order of its argument.

Dim MyString
MyString = StrReverse("Atanu Maity")
'Return "ytiaM unatA"
 

84

Tan

Tan(expression)

This function returns the tangent of an angle, which must be expressed in radians.
 

85

Time

Time()

This function returns the system’s time in AM/PM format. The following statement:

MsgBox "The system time is " & Time()

displays a time such as 5:13:05 PM in a message box. To set the system time, use the following statement:

Time = "13:00.00"
 

86

Timer

Timer()

This function returns a single number representing the number of seconds elapsed since midnight. It is frequently used for timing purposes, as long as the desired accuracy is not less than a second. To time an operation that takes a while to complete.
 

87

TimeSerial

TimeSerial(hours, minutes, seconds)

This function returns a time, as specified by the three arguments. 

TimeSerial(4, 10, 55)  'Returns: 4:10:55 AM
 

88

TimeValue

TimeValue(time)

This function returns a variant of type Time. Like the DateValue() function, it can be used in operations that involve time.
If the variables Time1 and Time2 are defined as follows:

Time1 = "04.10.55"
Time2 = "18.50.00"

you can find out the hours, minutes, and seconds between the two times with the following statements:

Diff = TimeValue(Time2) - TimeValue(Time1)
HourDiff = Hour(Diff)
MinDiff = Minute(Diff)
SecDiff = Second(Diff)


In this example, the values returned will be:

HourDiff=14
MinDiff=25
SecDiff=05
 

89

TypeName

TypeName(variable_name)

This function returns a string that identifies the variable’s type. It returns the name of the variable’s type. The variable whose type you’re examining with the TypeName function may have been declared implicitly or explicitly.
 

90

UBound

UBound(arrayname[, dimension])

This function returns the largest sub-script for the indicated dimension of an array. The arrayName variableis the name of the array, and dimension is an integer indicating the dimension whose upper bound will be returned. If dimension is omitted, the first dimension is assumed. To scan all the elements of a one-dimensional array, use both the LBound() and the UBound() functions. The following statements convert the elements of the string array Strings() to uppercase:

Lower = LBound(Strings)
Upper = UBound(Srtings)
For i = Lower to Upper
    Strings(i) = Ucase(Strings(i))
Next

 

91

UCase

UCase(string)

the Ucase() function accepts a string as an argument and converts it to uppercase.
 

92

Unescape

 

93

VarType

VarType(variable)

The VarType() function returns a value indicating the sub-type of a variable.
 

94

Weekday

Weekday(date, [firstdayofweek])

This function returns an integer in the range 1 through 7, representing the day of the week (1 for Sunday, 2 for Monday, and so on). The first argument, date, can be any valid date expression. The second argument, which is optional, specifies the first day of the week. Set it to 1 to start counting from Sunday (the default), or set it to 2 to start counting from Monday. The value 3 corresponds to Tuesday, the value 4 corresponds to Wednesday, and so on. The following code segment displays the name of the day:

DayNames = Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")

dayname = "Today it is " & DayNames(Weekday(Now)-1)

 

95

WeekdayName

 

96

Year

Year(date)

This function returns an integer representing the year of the date passed to it as an argument. The following function:
 



Try Yourself
1. Open a text editor type following code.

<HTML>
<HEAD><TITLE>Hello World</TITLE>
<SCRIPT LANGUAGE="VBScript">
<!--
Sub Button1_OnClick
MsgBox "Hello to VBScript"
End Sub
-->
</SCRIPT>
</HEAD>
<BODY>
<H3>Welcome to VBScript</H3><HR>
<FORM><INPUT NAME="Button1" TYPE="BUTTON" VALUE="Click Here"></FORM>
</BODY>
</HTML>


2. Save the file "Hello.HTM", open the file in browser. 
3. Click the button it shows the message "Hello to VBScript"

Next Topics    10. VBScript Sample Code

C) Atanu Maity, 2006-2007