VB common functions

Introduction to commonly used functions in VB

1. Commonly used internal functions

VB provides a large number of internal functions. Among these functions, some are universal. Some are related to certain operations. They are roughly divided into five categories: conversion functions, mathematical functions, date functions, time functions and random functions.

(1) Conversion function

Function name Function

Int(x) Returns the largest integer that is not greater than the independent variable.

Fix(x) removes the decimal part of a floating point number and retains its integer part.

Hex(x) Converts a decimal number to a hexadecimal number.

Oct(x) Converts a decimal number to an octal number.

Asc(x) returns the ASCII code of the first character in the string.

Chr(x) Converts a value to the corresponding ASCII character.

Str(x) Converts a value to a string

Cint(x) Rounds the decimal part and converts it to an integer.

Ccur(x) Converts the value to currency type, retaining up to 4 decimal places.

CDbl(x) Converts a value to a double.

CLng(x) Converts the value to a long integer value, rounding off the decimal part.

CSng(x) Converts a value to a single-precision number.

CVar(x) Converts a value to a variant type value.

(2) Mathematical function

Function name Function

Sin(x) returns the sine value.

Cos(x) returns the cosine value.

Tan(x) returns the tangent value.

Atn(x) returns the arctangent value.

Abs(x) returns the absolute value.

Sgn(x) returns the sign of the argument. That is:

When the value is a negative number, the function returns -1

When the value is 0, the function returns 0

When the value is a positive number, the function returns 1 < /p>

Sqr(x) returns the square root of the independent variable x, which must be greater than or equal to 0.

Exp(x) returns the value with e as the base and x as the exponent, that is, e is raised to the power of x.

(3) Date function

Function name Function

Day(Now) returns the current date.

WeekDay(Now) returns the current weekday.

Month(Now) returns the current month.

Year(Now) returns the current year.

(4) Time function

Function name Function

Hour(Now) returns the hour (0~23).

Minute(Now) returns the minute (0~59).

Second(Now) returns the second (0~59).

(5) Random function

Rnd(x) generates a single-precision random number between 0 and 1

The Rnd function can generate random numbers. When an application continuously reuses random numbers, the same sequence of random numbers will appear repeatedly. Use the Randomize statement to eliminate this situation.

2. String function

(1) Delete blank character function

Function name function

LTrim (string) remove " The blank characters on the left side of "string";

RTrim(string) removes the blank characters on the right side of "string";

Trim(string) removes the blank characters on both sides of "string" Character;

(2) String interception function

①Left interception

Format: Left (string, n)

Returns the first n characters of the string (n≥0).

②Middle interception

Format: Mid (string, p, n)

Starting from the P-th character, intercept n characters backward (P >0, n≥0). The third argument of the function can be omitted. If omitted, truncation is performed backwards from the position specified by the second argument to the end of the string.

③Right interception

Format: Right (string, n)

Returns the last n characters of the string (n≥0).

(3) String length test function

Format: Len (string) Len (variable name)

Use the Len function to test the length of a string , you can also test the storage space of variables.

(4) String function

Format: String (n, ASCII code) String (n, string)

Returns a string consisting of the numbers specified by n String. The second argument can use ASCII code or a string.

(5) Space function

Format: Space(n)

Return n spaces.

(6) String matching function

Format: InStr ([first character position,] string 1, string 2 [,n])

Search "String 2" in "String 1" in the function. If found, return the position of the first character of "String 2". If it is the first character of the string, the position is 1.

Note:

①The length of "String 2" is limited to 65535 characters.

②"First character position" is optional. If it contains the "first character position", start searching from this position, otherwise start searching from the starting position of "String 1". "First character position" is a long integer.

③The last n of the function is optional and the type is an integer, used to specify string comparison. The range is: 0, 1, 2.

If it is 0, use binary comparison, distinguishing the upper and lower case of letters;

If it is 1, ignore the case when comparing;

If it is 2, it will be based on the data contained in the database. information for comparison (limited to Access database).

(7) Letter case conversion

Format: Ucase (string)’ lowercase letters to uppercase and lowercase letters.

Lcase(string)’ Convert uppercase letters to lowercase letters.

3. MsgBox function

The format of MsgBox function is as follows:

MsgBox(msg[,Type][,Title][,HelpFile,Context]) < /p>

  ① ② ③   ④

The function *** has 5 parameters. Except for the ① parameter, the rest are optional parameters.

①msg is a string type with a length limit of 1024 characters. Exceeding characters will be automatically truncated.

②type is an integer value or symbolic constant used to control the type of buttons and icons displayed in the dialog box. The parameter value is generated by the addition of four types of numerical values. These four types of numerical values ??or symbolic constants respectively represent the type of button, the type of displayed icon, the position of the active button and forced return.

Constant Value Description

vbOKOnly 0 Displays only the OK button.

VbOKCancel 1 Displays OK and Cancel buttons.

VbAbortRetryIgnore 2 Displays the Abort, Retry, and Ignore buttons.

VbYesNoCancel 3 Displays Yes, No and Cancel buttons.

VbYesNo 4 Displays Yes and No buttons. VbRetryCancel 5 Displays Retry and Cancel buttons.

VbCritical 16 displays the Critical Message icon.

VbQuestion 32 displays the Warning Query icon.

VbExclamation 48 displays the Warning Message icon.

VbInformation 64 displays the Information Message icon. vbDefaultButton1 0 The first button is the default.

vbDefaultButton2 256 The second button is the default.

vbDefaultButton3 512 The third button is the default value.

vbDefaultButton4 768 The fourth button is the default. vbApplicationModal 0 The application is forced to return; the application is suspended until the user responds to the message box.

vbSystemModal 4096 The system returns forcefully; all applications are suspended until the user responds to the message box.

vbMsgBoxHelpButton 16384 Adds the Help button to the message box

VbMsgBoxSetForeground 65536 Specifies the message box window as the foreground window

vbMsgBoxRight 524288 The text is right-aligned

< p>vbMsgBoxRtlReading 1048576 Specifies that the text should be displayed from right to left in Hebrew and Arabic systems

vbSystemModal 4096 System force return; all applications are suspended until the user makes a comment on the message box Continue to work only after responding.

The Type parameter consists of the four types of values ??in the above table. The principle is: select a value from each type (only one value, no repetitions), and add these values ??together to get the Type parameter. values ??(generally, only the first three categories need to be used).

Example: MsgBox "Whether to pause printing!",16,"Prompt"

The Type parameter 16 is decomposed into:

16=16+0 Display the "OK" button (0) + "Pause" icon (16) + the default button is "OK" (0) MsgBox "Whether to exit the program.", 35, "Exit"

Type parameter is 35 Decomposed into:

35=3+32+0 Display 3 buttons of "Yes, No, Cancel" (3) + "?" icon (32) + Default button (0) ③Title is a string , used to display the title of the dialog box.

④HelpFile, Context: HelpFile is a string variable or string expression used to represent the name of the help file. Context is a numeric variable or expression used to represent the help directory number of the related help topic. .

The return value of the MsgBox function is an integer, which is related to the selected command button.

Return value parameter table:

Constant value description

vbOK 1 OK

vbCancel 2 Cancel

vbAbort 3 Abort

vbRetry 4 Retry

vbIgnore 5 Ignore

vbYes 6 Yes

vbNo 7 No

Example :Dim Tmp as long

Tmp=MsgBox("Whether to exit the program.

",35,"Exit")

Msgbox "The button value you selected is:"&Tmp