Common VBScript Functions

Source: Internet
Author: User
Tags dbase open dbf file key string random seed
Common VBScript functions:

1. Numeric functions:

ABS (Num): returns the absolute value.
SGN (Num): num> 0 1; num = 0 0; num <0-1; Positive and Negative values
Hex (Num): returns the hexadecimal value for direct representation: & hxx maximum 8 bits
Oct (Num): returns the octal value and indicates the maximum 8 bits of & oxx
Sqr (Num): returns the square root num> 0
INT (Num): integer int (99.8) = 99; int (-99.2) = 100
Fix (Num): Get the integer fix (99.8) = 99; fix (-99.2) = 99
Round (Num, n): rounded to the decimal place round (3.14159, 3) = 3.142 the midpoint value is rounded to the nearest even round (3.25, 1) = 3.2
Log (Num): returns the base-e logarithm num> 0.
Exp (n): The N power of e is usually num ^ n.
Sin (Num): trigonometric function, which is calculated in radians (angle * PAI)/180 = radian con (Num); Tan (Num); ATN (Num)

2. string function:

Len (STR): calculates the length of a string. The length of a Chinese character is also counted as one!
mid (STR, start character, [read length]): truncates the substring in the middle of the string
left (STR, nlen ): extract the nlen length substring from the left
right (STR, nlen): extract the nlen length substring from the right
lcase (STR ): string to lowercase
ucase (STR): string to uppercase
trim (STR): removes spaces at both ends of the string
ltrim (STR ): remove spaces on the left of the string
rtrim (STR): Remove spaces on the right of the string
Replace (STR, search string, replace string, [start character, replacement times, comparison method]): replacement string
Note: Default Value: Start character 1; unlimited number of replacements; Case sensitivity (0)
instr ([start character,] STR, search string [, comparison method]): checks whether the Sub-string contains optional parameters. You must select the return start position at the same time.
reverse Rev (STR, search string [, start character] [, comparison method]): returns the start position of a substring.
space (n): Creates a string with N spaces.
string (n, STR ): construct a string consisting of the First n str strings
strreverse (STR): reverse string
split (STR, split string [, times] [, comparison method]): Convert string into character array using the separator string. Optional parameters must be selected at the same time

3. data Type Conversion Function:
CINT (STR): Convert positive values to true-1; false 0; Date distance to 1899/12/31 days; time in the morning segment 0; afternoon segment 1;
CSTR (STR): Date output format: yyyy/mm/DD; Time Output Format: AM/PM hh: mm: SS
clng (STR): and CIN () similar to
cbool (Num): num is not 0 true; otherwise false
cdate (STR): conversion date format 0: # Am 12:00:00 #; A date with a positive number ranging from 1899/12/31 days; floating point date + decimal time
cbyte (Num): num <255 to byte
csng (STR ): convert to single-precision value
cdbl (STR): Convert to double-precision value
ccur (STR): Convert to cash format

4. time Function:
date: obtains the current system date.
time: obtains the current system time.
now: datetime type of the current system time and date value
Timer: take the current time from zero second value, timer, Time Difference Calculation
dateadd (interval unit, interval value, date): Calculate the adjacent date
datediff (interval unit, date 1, date 2): calculate the time difference date 2-date 1
datepart (interval unit, date ): calculate the interval unit value of a date
dateserial (date): Output date value (calculated by sequence)
timeserial (time): Output Time Value (calculated by sequence)
datevalue (datetime): extract the date value in the string
timevalue (datetime): extract the time value in the string
weekday (date ): calculate the day of the week
monthname (date): name of the output month
year (datetime): Year to be truncated
month (datetime ): trash month
day (datetime): trash day
hour (datetime): trash hour
minute (datetime): trash minute
second (datetime ): truncation seconds

5. Other functions:
Array (unit,...): dynamically generates an array
ASC (STR): the ASCII code of the first character of the output string.
CHR (ASC): Convert ASCII to enter: CHR (13) & CHR (10)
Filter (array name, key string, [, including] [, comparison method]):
Store the elements containing key strings in the string array into a new array (default). If [include] is false, the uncontained elements are obtained.
Join (arrayname): concatenates elements in the array into strings.
Ubound (arrayname [, dimension]): obtains the upper bound of the corresponding dimension of the array.
Lbound (arrayname [, dimension]): the lower bound to obtain the corresponding dimension of the array is generally 0.
Randmize N: Start Random Seed
RND (n): obtain a random number. n> 0 or null. Obtain the next random value of the sequence. n <0. The random value is the same. n = 0, produce the same number as the previous random Value
Take random positive numbers C between A and B, formula: c = int (B-A + 1) * RND + a) condition (B>)

ChildProgramAnd user-defined functions

Sub strsubname function strfunname (ARG [1],...)
Subroutine body
Exit sub
End sub strfunname = value return value
End Function
[Call] strsubname reference subroutine Var = strfunname (ARG [1],...) Reference Function

Subprograms and user-defined functions can be called recursively;

Examples of common statements for six ASP objects:

Response:

Response. Write strvar/"string": writing a parameter value or string to a webpage is equivalent to embedding <% = strvar/"string" %> in an HTML Tag.
Response. End: Stop page compilation and output the compiled content to the browser.
Response. Buffer = true | false: whether to use cache settings during page compilation. This is generally set in the page header.
Response. Flush: forces the output of partially compiled page content
Response. Clear: clears data in the buffer.
Response. Redirect URL: Stop page compilation or output and reprint the specified page
Response. isclientconnected: returns true | false to check whether the user is still connected.
Response. charset (charsetname): sets the page encoding type, that is, <meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
Response. contenttype [= contenttype]: Set the page file type, same as above
Response. expires [= number]: Set the page expiration time, in minutes
Response. expiresabsolute [= [date] [time]: sets the absolute time when the page fails.
Response. Status = statusdescription: sets the page status description.

Request:

Request ("passstrname"): Read the webpage transfer value, including the form and? Passstrname = Value & passstrname_n = value_n form
Request [. Collection | property | method] (variable)
Request. querystring ("passstrname"): Read the form value passed by the get method and? Passstrname = Value
Request. querystring (varible) [(INDEX). Count]
Request. Form ("passstrname"): Read the value of the pure form field passed by the POST method.
Request. Form (parameter) [(INDEX). Count]
Request. servervaribles (server environment variable): Read the environment variables of the client system. For details, see references.
Request. binaryread (count): Read the transfer value of the specified number of bytes
Request. totalbytes: the length of the query body, in bytes, read-only

Note: For a form with the same name, for example, if checkbox has multiple values, the transfer form is right, strname = value1, value2,... you need to use the split function to split the values.
The Select form of multiple is similar to the checkbox. The value of textarea can contain line breaks and be converted to <br> using replace to meet the format requirements.

Session: (User global variables)

Session ("sesname") = value: stores the session variable value, which can also be read.
Session ("sesname") = empty: two methods to determine whether the session value exists
Isempty (Session ("sesname") = true | false: two methods to determine whether the session value exists
Session. Timeout = num: Set the validity period of the session variable, in minutes
Session. Abandon: Clear all session variable values
Session. sessionid: Id serial number of the session variable, read-only

Application :( global variables of the Application)

Application ("appname") = value: stores the value of the application variable. You can also read this value.
Application ("appname") = empty: two methods to determine whether the application value exists
Isempty (Application ("appname") = true | false: two methods to determine whether the application value exists
Application. Lock: the value of the application variable is locked to prevent changing the value at the same time.
Application. Unlock: Unlock the value of the application variable, allowing you to change the value.

Note: Both session and Application variables can be used to store arrays and system objects. The reference method is that the variable name is equivalent to the array name,
However, the value cannot be changed directly. You need to use the temporary array to modify the value and then assign it to the session and Application variables.

Structure of the global. Asa file: <% @ Language = "VBScript" %>
<% Sub application_onstart... end sub
Sub application_onend... end sub
Sub session_onstart... end sub
Sub session_onend... end sub %>

Server:

Server. mappath ("fileurl"): the absolute address of the server site mapped to the file name. Path = server. mappath (./) can obtain the root path of the virtual directory.
Server. htmlencode ("string"): converts to HTML-formatted strings, such as: <,>.
Server. urlencode ("string"): Convert to browser address encoding.
Set Var = server. creatobject ("objname"): creates an object variable.
Server. scripttimeout = numseconds: time limit for executing ASP program pages, in seconds

Cookies: temporary variables stored on the user's local machine. The maximum size of each cookie is 4 kb. A maximum of 300 cookies can be 1.2 MB.

Response. Cookies ("strcookiename") = value: stores the cookie variable value, which can also be read.
Response. Cookies ("strcookiename") = "": determines whether it is empty.
Response. Cookies ("strcookiename"). expires = Date: The variable validity period, in days. The variable is invalid immediately when it is less than the current time.
Response. Cookies (cookie [(key). Attribute]): standard syntax

Objectcontext controls ASP Transaction Processing

Objectcontext. ontransactionabort: triggered by the abandoned Transaction Processing Event, which occurs after the script completes processing.
Objectcontext. ontransactioncommit: triggered by a successful transaction processing event.
Objectcontext. setabort: explicitly discard a transaction
Objectcontext. setcomplete: overwrite any previous call to the objectcontext. setabort method.
Msgbox "string"/strname: VBScript prompt box

Form onsubmit event: Write function formname_onsubmit ()... formname_onsubmit = true/false... end function on the same page,
The page executes the statement before submission and determines whether the task is submitted based on the returned value.

Database connection string example:

Access2000:
Conn. Open "provider = Microsoft. Jet. oledb.4.0; Data Source =" & server. mappath ("DBASE \ liuyan. mdb ")
Conn. Open "provider = Microsoft. Jet. oledb.4.0; Data Source =" & server. mappath ("DBASE \ liuyan. mdb") & "; Password = admin"
Recordset. Open "data table name", Conn, 2, 2

Sqlserver2000:
Conn. Open "provider = sqloledb.1; user id = sa; Password = admin; initial catalog = pubs; Data Source = Server \ garlmrm"
Conn. Open "provider = sqloledb; Data Source = hyserver; uid = sa; Pwd =; database = pubs"
Recordset. Open strsql, Conn, 2, 2

DBASE :(?)
Conn open "driver = {Microsoft DBASE driver}; sourcetype = dB; sourcedb =" & server. mappath ("directory name ")
Recordset. Open strsql, Conn, 2, 2

FOXBASE:
Conn open "driver = {Microsoft Visual FoxPro driver}; sourcetype = DBF; sourcedb =" & server. mappath ("directory name ")
Recordset. Open DBF file name or select statement, Conn, 2, 2
Conn open "driver = {Microsoft Visual FoxPro driver}; sourcetype = dBc; sourcedb =" & server. mappath ("DBC database file name ")
Recordset. Open DBF file name or select statement, Conn, 2, 2

Excel:
Conn open "driver = {Microsoft Excel Driver (*. xls)}; DBQ =" & server. mappath ("xls file name ")
Recordset. Open "select * from [" & sheet & "$]", Conn, 2, 2

Note: When Using DBF, DBC, and Excel databases, note the following: Excel databases can only read, add, and modify records, but cannot delete records;
DBF and DBC can read, add, delete, and modify records. However, when adding a record, the value of any field cannot be blank.
It can be seen that the limitations are great, so try to use MDB or SQL database as much as possible.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.