Visual Basic's Built-in Functions
Visual Basic has many built-in functions for manipulating text and carrying out mathematical operations, as well as the ability to format data in both standard and user-defined styles. We start by looking at two functions that are both frequently used and at the same time relatively easy to get to grips with - the MsgBox() function and the InputBox() function. We will then look at some of the more commonly-used string-handling and math functions, and the ways in which numerical, date and time values can be represented.
The MsgBox() function
The MsgBox() function displays a message in a pop-up message box (no surprises there!). The user is expected to respond by clicking on a button in order to be able to continue. The format of the statement used to invoke a message box is as follows:
returnVal = MsgBox (prompt, styleVal, title)
The prompt parameter is a string value (either a string literal or a string variable) that supplies the message to be displayed. The styleVal parameter is either an integer style value from 0 to 5, or a named constant that can be used instead of the corresponding integer value, that determines which command buttons will appear on the message box. The title parameter is another string literal or string variable that supplies the title for the message box.
The styles available are listed in the table below.
Style value | Named constant | Buttons displayed |
---|---|---|
0 | vbOkOnly | OK |
1 | vbOkCancel | OK, Cancel |
2 | vbAbortRetryIgnore | Abort, Retry, Ignore |
3 | vbYesNoCancel | Yes, No, Cancel |
4 | vbYesNo | Yes, No |
5 | vbRetryCancel | Retry, Cancel |
The returnVal value returned by the MsgBox() function is an integer value that indicates which button the user has clicked in response to the message box being displayed. The possible return values together with the named constants that may be used in their stead, are listed in the table below.
Return value | Named constant | Button clicked |
---|---|---|
1 | vbOk | OK |
2 | vbCancel | Cancel |
3 | vbAbort | Abort |
4 | vbRetry | Retry |
5 | vbIgnore | Ignore |
6 | vbYes | Yes |
7 | vbNo | No |
The message box can be further embellished by the inclusion of an icon that will appear beside the message. In Visual Basic, there are four icons available. The icon is included either by incrementing the style value by a fixed constant value, or by the inclusion of a second named constant (see table below).
Value | Named constant | Icon |
---|---|---|
16 | vbCritical | |
32 | vbQuestion | |
48 | vbExclamation | |
64 | vbInformation |
The following statements would produce exactly the same message box, and are essentially equivalent to one another:
MsgBox ("This message is for info!", 64, "Info")
MsgBox ("This message is for info!", vbOKOnly + vbInformation, "Info")
The InputBox() function
The InputBox() function displays a pop-up input box into which the user can type a message. The user is expected to enter their message, then click on a button ("Cancel" or "OK") in order to continue. The format of the statement used to invoke an input box is as follows:
returnString = InputBox (prompt, title, defaultText, xpos, ypos)
The returnString returned by the InputBox() function is the text entered by the user. The prompt parameter is a text string (either a string literal or a string variable) that prompts the user to enter some information. The title parameter is another string literal or string variable that supplies the title for the input box. The defaultText parameter can be used to enter default content in the input box (although it would probably be left blank in most cases). The xpos and ypos parameters specify the x and y coordinates for the input box on screen.
String functions
Function | Description |
---|---|
Mid (str, pos, n) | Returns n characters from the text string referred to by str, starting at character position pos. |
Microsoft.VisualBasic.left (str, n) | Returns the n left-most characters from the text string referred to by str. |
Microsoft.VisualBasic.right (str, n) | Returns the n right-most characters from the text string referred to by str. |
Trim (str) | Removes the white space (if any) at either end of the text string referred to by str, and returns the result. |
LTrim (str) | Removes the white space (if any) at the left-hand end of the text string referred to by str, and returns the result. |
RTrim (str) | Removes the white space (if any) at the right-hand end of the text string referred to by str, and returns the result. |
InStr (n, str1, str2) | Looks for an occurrence of the substring str2 within the string str1, starting at character position n. If successful, the function returns the character position at which the substring is found (or zero if the substring cannot be found). |
UCase (str) | Converts the string referred to by str to all upper-case characters, and returns the result. |
LCase (str) | Converts the string referred to by str to all lower-case characters, and returns the result. |
Chr (charCode) | Returns the ASCII character corresponding to the 8-bit character code charCode (note – some characters are non-printing characters, and will not be visible on screen). |
Asc (character) | Returns the 8-bit ASCII character code corresponding to character. |
Maths functions
Function | Description |
---|---|
Math.Sqrt (n) | Returns the square root of a number, n. |
Math.Abs (n) | Returns the absolute value of a number, n. |
Math.Exp (n) | Returns the exponential value of a number, n – i.e. e n. Note: e = 2.71828182 |
Fix (n) | Returns the integer part of a floating point number, n. |
Int (n) | Returns the integer part of a floating point number, n, for positive numbers, or the next smallest integer value for negative numbers. |
Math.Log (n) | Returns the natural logarithm of a number, n. |
Rnd () | Returns a randomly generated value between 0 and 1. |
Round (n,m) | Rounds a number, n, up (or down) to m decimal places. |
Formatting numbers
The Format() function allows you to specify precisely how numeric values are displayed. The function is used as follows:
Format (n, "styleArg")
The first argument (n) is the number to be formatted. The value selected for the second argument ("styleArg") will determine how the number is displayed. The possible values are described in the table below (note that a user-defined value for "styleArg" can also be used).
Style Argument | Description |
---|---|
"General Number" | Displays n with no separator between thousands. |
"Fixed" | Displays n with no separator between thousands, and rounds it up (or down) to two decimal places. |
"Standard" | Displays n with separators between thousands, and rounds it up (or down) to two decimal places. |
"Currency" | Displays n prefixed by a "£" sign (if the locale is set to United Kingdom) , with separators between thousands, and rounds it up (or down) to two decimal places. |
"Percent" | Displays n as a percentage, rounds it up (or down) to two decimal places, and adds a "%" symbol. |
Formatting the date and time
The Format() function can also be used to specify how date and time values are displayed. The function is used as follows:
Format (Date, "styleArg")
The first argument (Date) is the date to be formatted (note that if you are using the current date, the Now function can be used as the argument. The value selected for the second argument ("styleArg") will determine how the date value is displayed. The possible values are described in the table below (note that a user-defined value for "styleArg" can also be used).
Style Argument | Description |
---|---|
"General Date" | Displays the date and time in the format: dd/mm/yyyy hh:mm:ss |
"Long Date" | Displays the date in the format: 06 October 2009 |
"Short Date" | Displays the date in the format: dd/mm/yyyy |
"Long Time" | Displays the time in the format: h:mm:ss |
"Short Time" | Displays the time in the format: hh:mm |