Built-In Functions tip
 















    VBScript ships with many functions that you can use to complete your scripts. These functions are highly reliable and can tremendously reduce your work. This means that, before creating your own procedure or function, first make sure it has not been written already because if an existing function already implements the behavior you want to apply, you should such a function.


Because VBScript is an interpreted language (and not a compiled language), all of the possible built-in functions are already known to the browser. Therefore, you don't need to include a file or library.


The best place to find out what functions already exist is by consulting the MSDN library or web site for its documentation.













Conversion Functions










We saw already that the users of your web pages will be presented with objects called controls. These objects allow the user to type values or select something from a list. Anything the user types in a text-based field is primarily considered as a string. Before performing any type of operation that involves such a value, you should make sure you can identify what kind of value it is. For example, you shouldn't try to multiply a string by a date such as FirstName * January 16. Although you will not be able to avoid every single type of problem that could occur in a page, you can reduce errors by checking the value that a control holds.


The first thing you should do with a value retrieved from a control is to convert it to the appropriate type. There are various conversion functions adapted to the different possible kinds of values. The general syntax of the conversion functions is:



ReturnType = Function(Expression)

The expression could be of any kind, depending on how the expression would be supplied. For example, it could be a string or value the user would have entered in form. It could also be the result of a calculation performed on another procedure or function. The function would take such a value, string, or expression and attempt to convert. If the conversion is successful, the function would return a new value that is of the type specified by the ReturnType in our syntax.


The conversion functions are as follows:



































































Function
NameReturn TypeDescription
CBoolBooleanConverts an expression into a Boolean value
CByteByteConverts an expression into Byte number
CDateDateConverts and expression into a date or time value
CDblDoubleConverts an expression into a flowing-point (decimal) number
CIntIntegerConverts an expression into an integer (natural) number
CCurCurrencyConverts an expression into a currency (monetary) value
CLngLongConverts an expression into a long integer (a large natural) number
CSngSingleConverts an expression into a flowing-point (decimal) number
CStrStringConverts an expression into a string












Practical Learning:












  1. Start your text editor.

  2. In the empty file, type the following:







    <html>
    <head>
    <Script Language="VBScript">
    <!--
    Sub GiveFocus
    Document.EmployeesPayroll.txtFirstName.Focus()
    End Sub
    -->
    </Script>

    <title>Employees Payroll</title>
    </head>

    <body>

    <h1>Oddenton Auto Repair</h1>
    <h2>Employees Payroll</h2>
    <form name="EmployeesPayroll" method="POST">
    <table border="0" width="548">
    <tr>
    <td width="104">First Name:</td>
    <td width="430"><input type="text" name="txtFirstName" size="20"></td>
    </tr>
    <tr>
    <td width="104">LastName:</td>
    <td width="430"><input type="text" name="txtLastName" size="20"></td>
    </tr>
    </table>

    <table border="0" width="548">
    <tr>
    <td width="104">Hourly Salary</td>
    <td width="428"><input type="text" name="txtHourlySalary" size="10"value="0.00"></td>
    </tr>
    </table>
    <table border="0" width="563">
    <tr>
    <td width="124">Weekly Hours</td>
    <td width="62" align="center">Mon</td>
    <td width="62" align="center">Tue</td>
    <td width="62" align="center">Wed</td>
    <td width="62" align="center">Thu</td>
    <td width="62" align="center">Fri</td>
    <td width="62" align="center">Sat</td>
    <td width="62" align="center">Sun</td>
    </tr>
    <tr>
    <td width="124"></td>
    <td width="62" align="center"><input type="text" name="txtMon" size="6" value="0.00"></td>
    <td width="62" align="center"><input type="text" name="txtTue" size="6" value="0.00"></td>
    <td width="62" align="center"><input type="text" name="txtWed" size="6" value="0.00"></td>
    <td width="62" align="center"><input type="text" name="txtThu" size="6" value="0.00"></td>
    <td width="62" align="center"><input type="text" name="txtFri" size="6" value="0.00"></td>
    <td width="62" align="center"><input type="text" name="txtSat" size="6" value="0.00"></td>
    <td width="62" align="center"><input type="text" name="txtSun" size="6" value="0.00"></td>
    </tr>
    </table>
    <table border="0" width="558">
    <tr>
    <td width="104"></td>
    <td width="440">
    <p><input type="button" value="Process" name="btnProcess">
    &nbsp;&nbsp;
    <input type="reset" value="Reset" name="B2">
    </p>
    </td>
    </tr>
    </table>
    </form>
    </body>

    </html>



  3. Save the file as payroll2.htm in the vbstutorial folder and preview it in the browser:


  4. After previewing the page, return to your text editor.

  5. To perform calculations that involve values stored in text boxes, change the file as follows:







    <html>
    <head>
    <Script Language="VBScript">
    <!--
    ' -- This procedure gives focus to the First Name text box
    Sub GiveFocus
    Document.EmployeesPayroll.txtFirstName.Focus()
    End Sub

    ' This function retrieves the first and last names
    ' It concatenates them and returns a full name
    Function GetFullName()
    Dim FirstName, LastName

    FirstName = Document.EmployeesPayroll.txtFirstName.Value
    LastName = Document.EmployeesPayroll.txtLastName.Value

    GetFullName = LastName & ", " & FirstName
    End Function

    ' This function uses an hourly salary and the number of hours in a week
    ' It calculates the total earnings for the week
    Function CalculateWeeklySalary(HourlySalary, WeeklyHours)
    CalculateWeeklySalary = HourlySalary * WeeklyHours
    End Function

    ' This is the central procedure of the script
    ' It processes the form and displays the values in the other form
    Sub ProcessPayroll()
    Dim dMon, dTue, dWed, dThu, dFri, dSat, dSun, TotalHours
    Dim HourlySalary, WeeklySalary
    Dim FullName

    dMon = CDbl(Document.EmployeesPayroll.txtMon.Value)
    dTue = CDbl(Document.EmployeesPayroll.txtTue.Value)
    dWed = CDbl(Document.EmployeesPayroll.txtWed.Value)
    dThu = CDbl(Document.EmployeesPayroll.txtThu.Value)
    dFri = CDbl(Document.EmployeesPayroll.txtFri.Value)
    dSat = CDbl(Document.EmployeesPayroll.txtSat.Value)
    dSun = CDbl(Document.EmployeesPayroll.txtSun.Value)

    HourlySalary = Document.EmployeesPayroll.txtHourlySalary.Value

    TotalHours = dMon + dTue + dWed + dThu + dFri + dSat + dSun
    WeeklySalary = CalculateWeeklySalary(HourlySalary, TotalHours)
    FullName = GetFullName()


    PayrollResults.txtFullName.Value = FullName
    PayrollResults.txtWeeklyEarnings.Value = "$" & WeeklySalary
    End Sub
    -->
    </Script>

    <title>Employees Payroll</title>
    </head>

    <body OnLoad="GiveFocus()">

    <h1>Odenton Auto Repair</h1>
    <h2>Employees Payroll</h2>
    <form name="EmployeesPayroll" method="POST">
    <table border="0" width="548">
    <tr>
    <td width="104">First Name:</td>
    <td width="430"><input type="text" name="txtFirstName" size="20"></td>
    </tr>
    <tr>
    <td width="104">LastName:</td>
    <td width="430"><input type="text" name="txtLastName" size="20"></td>
    </tr>
    </table>

    <table border="0" width="548">
    <tr>
    <td width="104">Hourly Salary</td>
    <td width="428"><input type="text" name="txtHourlySalary" size="10" value="0.00"></td>
    </tr>
    </table>
    <table border="0" width="563">
    <tr>
    <td width="124">Weekly Hours</td>
    <td width="62" align="center">Mon</td>
    <td width="62" align="center">Tue</td>
    <td width="62" align="center">Wed</td>
    <td width="62" align="center">Thu</td>
    <td width="62" align="center">Fri</td>
    <td width="62" align="center">Sat</td>
    <td width="62" align="center">Sun</td>
    </tr>
    <tr>
    <td width="124"></td>
    <td width="62" align="center"><input type="text" name="txtMon" size="6" value="0.00"></td>
    <td width="62" align="center"><input type="text" name="txtTue" size="6" value="0.00"></td>
    <td width="62" align="center"><input type="text" name="txtWed" size="6" value="0.00"></td>
    <td width="62" align="center"><input type="text" name="txtThu" size="6" value="0.00"></td>
    <td width="62" align="center"><input type="text" name="txtFri" size="6" value="0.00"></td>
    <td width="62" align="center"><input type="text" name="txtSat" size="6" value="0.00"></td>
    <td width="62" align="center"><input type="text" name="txtSun" size="6" value="0.00"></td>
    </tr>
    </table>
    <table border="0" width="558">
    <tr>
    <td width="104"></td>
    <td width="440">
    <p><input type="button" value="Process" name="btnProcess" OnClick="ProcessPayroll()">
    &nbsp;&nbsp;
    <input type="reset" value="Reset" name="B2">
    </p>
    </td>
    </tr>
    </table>
    </form>

    <hr>

    <form name="PayrollResults">
    <table border="0" width="546">
    <tr>
    <td width="113">Full Name:</td>
    <td width="419"><input type="text" name="txtFullName" size="20"></td>
    </tr>
    </table>
    <table border="0" width="545">
    <tr>
    <td width="112">Weekly Earnings:</td>
    <td width="419"><input type="text" name="txtWeeklyEarnings" size="20" value="$0.00"></td>
    </tr>
    </table>
    </form>

    </body>

    </html>



  6. Save the file and preview it in the browser. Type the first and last names in the corresponding fields.
    Type the employees hours for each day and click the Process button


  7. After previewing the page, return to your text editor.










Besides the conversion functions, VBScript provides many other functions you can use in your scripts. Some of these functions are:









































































































































































































































































































































































































Language ElementDescription
Abs FunctionReturns the absolute value of a number.
Array FunctionReturns a Variant containing an array.
Asc FunctionReturns the ANSI character code corresponding to the first letter in a string.
Atn FunctionReturns the arctangent of a number.
CBool FunctionReturns an expression that has been converted to a Variant of subtype Boolean.
CByte FunctionReturns an expression that has been converted to a Variant of subtype Byte.
CCur FunctionReturns an expression that has been converted to a Variant of subtype Currency.
CDate FunctionReturns an expression that has been converted to a Variant of subtype Date.
CDbl FunctionReturns an expression that has been converted to a Variant of subtype Double.
Chr FunctionReturns the character associated with the specified ANSI character code.
CInt FunctionReturns an expression that has been converted to a Variant of subtype Integer.
CLng FunctionReturns an expression that has been converted to a Variant of subtype Long.
Cos FunctionReturns the cosine of an angle.
CreateObject FunctionCreates and returns a reference to an Automation object.
CSng FunctionReturns an expression that has been converted to a Variant of subtype Single.
CStr FunctionReturns an expression that has been converted to a Variant of subtype String.
Date FunctionReturns the current system date.
DateAdd FunctionReturns a date to which a specified time interval has been added.
DateDiff FunctionReturns the number of intervals between two dates.
DatePart FunctionReturns the specified part of a given date.
DateSerial FunctionReturns a Variant of subtype Date for a specified year, month, and day.
DateValue FunctionReturns a Variant of subtype Date.
Day FunctionReturns a whole number between 1 and 31, inclusive, representing the day of the month.
Eval FunctionEvaluates an expression and returns the result.
Exp FunctionReturns e (the base of natural logarithms) raised to a power.
Filter FunctionReturns a zero-based array containing subset of a string array based on a specified filter criteria.
Fix FunctionReturns the integer portion of a number.
FormatCurrency FunctionReturns an expression formatted as a currency value using the currency symbol defined in the system control panel.
FormatDateTime FunctionReturns an expression formatted as a date or time.
FormatNumber FunctionReturns an expression formatted as a number.
FormatPercent FunctionReturns an expression formatted as a percentage (multiplied by 100) with a trailing % character.
GetLocale FunctionReturns the current locale ID value.
GetObject FunctionReturns a reference to an Automation object from a file.
GetRef FunctionReturns a reference to a procedure that can be bound to an event.
Hex FunctionReturns a string representing the hexadecimal value of a number.
Hour FunctionReturns a whole number between 0 and 23, inclusive, representing the hour of the day.
InputBox FunctionDisplays a prompt in a dialog box, waits for the user to input text or click a button, and returns the contents of the text box.
InStr FunctionReturns the position of the first occurrence of one string within another.
InStrRev FunctionReturns the position of an occurrence of one string within another, from the end of string.
Int FunctionReturns the integer portion of a number.
IsArray FunctionReturns a Boolean value indicating whether a variable is an array.
IsDate FunctionReturns a Boolean value indicating whether an expression can be converted to a date.
IsEmpty FunctionReturns a Boolean value indicating whether a variable has been initialized.
IsNull FunctionReturns a Boolean value that indicates whether an expression contains no valid data (Null).
IsNumeric FunctionReturns a Boolean value indicating whether an expression can be evaluated as a number.
IsObject FunctionReturns a Boolean value indicating whether an expression references a valid Automation object.
Join FunctionReturns a string created by joining a number of substrings contained in an array.
LBound FunctionReturns the smallest available subscript for the indicated dimension of an array.
LCase FunctionReturns a string that has been converted to lowercase.
Left FunctionReturns a specified number of characters from the left side of a string.
Len FunctionReturns the number of characters in a string or the number of bytes required to store a variable.
LoadPicture FunctionReturns a picture object. Available only on 32-bit platforms.
Log FunctionReturns the natural logarithm of a number.
LTrim FunctionReturns a copy of a string without leading spaces.
Mid FunctionReturns a specified number of characters from a string.
Minute FunctionReturns a whole number between 0 and 59, inclusive, representing the minute of the hour.
Month FunctionReturns a whole number between 1 and 12, inclusive, representing the month of the year.
MonthName FunctionReturns a string indicating the specified month.
MsgBox FunctionDisplays a message in a dialog box, waits for the user to click a button, and returns a value indicating which button the user clicked.
Now FunctionReturns the current date and time according to the setting of your computer's system date and time.
Oct FunctionReturns a string representing the octal value of a number.
Replace FunctionReturns a string in which a specified substring has been replaced with another substring a specified number of times.
RGB FunctionReturns a whole number representing an RGB color value.
Right FunctionReturns a specified number of characters from the right side of a string.
Rnd FunctionReturns a random number.
Round FunctionReturns a number rounded to a specified number of decimal places.
RTrim FunctionReturns a copy of a string without trailing spaces.
ScriptEngine FunctionReturns a string representing the scripting language in use.
ScriptEngineBuildVersion FunctionReturns the build version number of the scripting engine in use.
ScriptEngineMajorVersion FunctionReturns the major version number of the scripting engine in use.
ScriptEngineMinorVersion FunctionReturns the minor version number of the scripting engine in use.
Second FunctionReturns a whole number between 0 and 59, inclusive, representing the second of the minute.
SetLocale FunctionSets the global locale and returns the previous locale.
Sgn FunctionReturns an integer indicating the sign of a number.
Sin FunctionReturns the sine of an angle.
Space FunctionReturns a string consisting of the specified number of spaces.
Split FunctionReturns a zero-based, one-dimensional array containing a specified number of substrings.
Sqr FunctionReturns the square root of a number.
StrComp FunctionReturns a value indicating the result of a string comparison.
String FunctionReturns a repeating character string of the length specified.
StrReverse FunctionReturns a string in which the character order of a specified string is reversed.
Tan FunctionReturns the tangent of an angle.
Time FunctionReturns a Variant of subtype Date indicating the current system time.
Timer FunctionReturns the number of seconds that have elapsed since 12:00 AM (midnight).
TimeSerial FunctionReturns a Variant of subtype Date containing the time for a specific hour, minute, and second.
TimeValue FunctionReturns a Variant of subtype Date containing the time.
Trim FunctionReturns a copy of a string without leading or trailing spaces.
TypeName FunctionReturns a string that provides Variant subtype information about a variable.
UBound FunctionReturns the largest available subscript for the indicated dimension of an array.
UCase FunctionReturns a string that has been converted to uppercase.
VarType FunctionReturns a value indicating the subtype of a variable.
Weekday FunctionReturns a whole number representing the day of the week.
WeekdayName FunctionReturns a string indicating the specified day of the week.
Year FunctionReturns a whole number representing the year.


     Source    http://www.functionx.com/vbscript

Reviews:

  1. Robert/Nick: Very goodopinions. I had be interested to listen regarding virtually any gains to your
    [url=http://http://www.brands333.com]coach outlet sale[/url]

    ReplyDelete

Asian Tour Trip © 2014 - Designed by Templateism, Distributed By Blogger Templates | Templatelib

Contact us

Powered by Blogger.