TechnologyUK - Programming (VB6) Logo

Built-in Functions

The table below lists some of the common mathematical functions.


Common Mathematical Functions
FunctionDescription
Abs() Returns the absolute (unsigned) value of the argument.
Atn() Returns the arc tangent of the argument in radians
Cos() Returns the cosine value of the argument in radians
Exp() Returns the natral logarithm base of the argument
Len() Returns the number of characters in a string
Log() Returns the natural logarithm of the argument
Sin() Returns the sine value of the argument in radians
Sqr() Returns the square root value of the argument
Tan() Returns the tangent value of the argument in radians


String Functions

Visual Basic string functions return a string, and usually accept one or more strings as arguments. The table below lists some of the common string functions.


Common String Functions
FunctionDescription
Chr(int) Returns the ASCII character that matches the numeric argument
LCase(str) Returns the string argument in lower case letters (any character
that is already lower case is not affected)
Left(str, int) Returns the int leftmost characters from the string argument
Len(str) Returns the number of characters in the string (works on numeric
arguments as well)
LTrim(str) Removes leading spaces from the string argument
Mid(str, intStart [,intLen]) Returns a substring of a string argument, starting with the character
at intStart and ending at the character specified by intLen (optional),
or the last character of the string
Right(str, int) Returns the far-right int number of characters from the string argument
RTrim(str) Removes trailing spaces from the string argument
Str() Converts a numeric argument to a string consisting of numeric digits
UCase(str) Returns the string argument in upper case characters (any character
that is already upper case is not affected)


Time and Date Functions

Visual Basic includes the date and time functions described in the table below. Note that the functions Date(), Now(), and Time() do not accept arguments, so parentheses are not actually required.


Common Data and Time Functions
FunctionDescription
Date() Returns the current date.
DateSerial(intYr, intMo, intDay) Returns an internal date value for the three arguments.
DateAdd(strIntrvl, intN, dteDate) Adds the intN value to the date specified by dteDate for
the given strIntrvl.
DateDiff(strIntrvl, dte1, dte2) Returns the number of time intervals (specified by strIntrvl)
between the two dates.
DatePart(strIntrvl, dteDate) Returns the strIntrvl portion of the dteDate.
Now() Returns the current date and time in the date format.
Time() Returns the current time.
Timer() Returns the number of seconds since midnight.
TimeSerial(hour, min, sec) Returns the current date and time in the internal date
format for the time specified.


You can assign values returned from Date(), Time(), and Now() to variables declared as datatype Date. You can use the Format() function to put the date into the required format. DateSerial() enables you to convert a three-part date into a date that matches the internal Date datatype so that you can work with variables that hold dates. If the year falls within the 20th century, you can omit the 19 before the year. TimeSerial() also returns an internal Date datatype for three time parts, as shown below.

dteClockedIn = TimeSerial(16, 29, 25) 'Stores 4:29:25 P.M.

Time and date functions work on a 24-hour clock, so 16 represents 4:00 p.m. The DateAdd(), DateDiff(), and DatePart() functions require a special string interval value (see table below). The interval tells these date functions how to process the date argument.


Date and Time Interval
String Values
IntervalDescription
h Hour
d Day
m Month
n Minute
q Quarter
s Second
y Day of year
w Weekday
ww Week
yyyy Year


The DateDiff() function uses the interval string value to return the number of intervals between two dates. For example, the following expression returns the number of weeks between two date values:

dateDiff("ww", dteUser1, dteUser2)

You can use the interval value and the DatePart() function to obtain the integer number that represents the specified value. You can determine the day of the week (assuming that the week starts with Sunday as day 1) on which you were born using the following expression:

DatePart("d", dteUserBDay)

Visual Basic includes three additional functions that strip off the day, month, and year values from a Date datatype variable: Day(), Month(), and Year(). If you want to work with the current year, you can strip off the year from the current date like this:

intYear = Year(Date) ' Get this year

The Timer() function is useful for determining the amount of time that has elapsed between two events. It requires no arguments or parentheses, and the values returned can be stored in separate variables to enable the amount of time that has elapsed between the two events to be calculated.


Data-Testing Functions

The Is...() functions are called data inspection functions, since they inspect a data item and return information about its datatype. When you store a value in a variable declared as a Variant datatype, the data inspection functions can be used to determine the datatype of the stored value. They are especially useful for working with user-supplied data. The table below describes the data inspection functions.


Data Inspection Functions for Testing Datatypes
FunctionDescription
IsDate() True if the argument can convert to a Date datatype.
IsEmpty() True if the argument has not been initialised since it was declared.
IsEmpty() works with variable arguments only, not controls.
IsNull() True if the argument holds Null (such as an empty string) and works
for controls as well as variables.
IsNumeric() True if the argument can convert to a Numeric datatype.


Note that Visual Basic does not have an IsString() function. If you want to test for a String value, use the VarType() function. This function returns a value that indicates the datatype of the supplied argument. If you want a user to enter an integer value, for example, you can use VarType() to test whether the input actually supplied byn the user is a valid integer. The table below shows the values returned by VarType() for the various datatypes.


The VarType() Return Values
ValueNameDescription
0 vbEmpty Empty and uninitialised argument
1 vbNull Invalid data or a null string argument
2 vbInteger Integer argument
3 vbLong Long argument
4 vbSingle Single argument
5 vbDouble Double argument
6 vbCurrency Currency argument
7 vbDate Date argument
8 vbString String argument
9 vbObject Object argument
10 vbError Error argument
11 vbBoolean Boolean argument
12 vbVariant Variant argument
13 vbDataObject Data Access Object (DAO) argument
14 vbDecimal Decimal argument
17 vbByte Byte argument
8192+ vbArray Array argument (the value returned = 8192
+ the VarType() value of the array elements)


Data Conversion Functions

Once you have determined what kind of value a Variant type variable or a control holds, you can convert the value to the required datatype using one of the conversion functions described in the following table.


The Data Conversion Functions
FunctionDescription
Asc() Converts a string argument to the ASCII code of the first character in the string
CCur() Converts the argument to an equivalent Currency datatype
CDbl() Converts the argument to an equivalent Double datatype
CInt() Rounds a floating-point number up to the next whole number (integer)
CLng() Converts the argument to an equivalent Long datatype
CSng() Converts the argument to an equivalent Single datatype
CStr() Converts the argument to an equivalent String datatype
CVar() Converts the argument to an equivalent Variant datatype
Fix() Truncates the fractional portion of a floating-point number
Int() Rounds a floating-point number down to the next whole number (integer)
Hex() Converts a numeric argument to a hexadecimal value
Oct() Converts a numeric argument to an octal value


Normally, the following assignment would store 0.1428571 in a label named myValue:

myValue.Caption = (1 / 7)

The following statement, however, increases the precision used for the calculation, assigning the value 0.142857142857143 to the label:

myValue.Caption = CDbl(1 / 7)

The conversion functions are useful when you need to specify the exact datatype for a calculation or for a control.


The format function

Format() returns a Variant datatype (easily coverted to a String datatype) in the specified format. The syntax of the Format() function is shown below.

Format(Expression, strFormat)

The result of Format() can be assigned to a variable or a control. Expression can be a variable, an expression, or a constant, while strFormat must be one of the values given in the table below.


The strFormat Values
strFormatDescription
"Currency" Ensures that a currency symbol (e.g. £ or $) appears before the formatted value, and
that appropriate separators are used for values over 999 (a decimal point or comma).
The regional settings on the computer will determine which characters are used.
Values will always be displayed with two decimal places. Negative values are
displayed in parentheses.
"Fixed" Displays at least one digit before, and two digits after the decimal point, with no
thousands separator.
"General Number" Displays the number with no thousands separator.
"Medium Time" Displays the time in 12-hour format, with the a.m. or p.m. indicator.
"On/Off" Displays "On" if the value contains a non-zero or True value and "Off" if the value
contains zero or a False value.
"Percent" Displays the number as a percentage (i.e. multiplied by 100) and adds the percent
sign (%) to the right of the number.
"Scientific" Displays numbers in scientific notation.
"Short Time" Displays the time in 24-hour format.
"True/False" Displays "True" if the value contains a non-zero or True value, and "False" if the
value contains zero or a False value.
"Yes/No" Displays "Yes" if the value contains a non-zero or True value, and "No" if the value
contains zero or a Falsevalue.


You will probably not need to use format codes often. If none of the predefined formats match the requirements of your program, you can define your own edit mask using custom formatting codes. These are usually a combination of the hash and zero characters to represent a digit (#) or a leading or trailing zero (0). The following assignment displays the value of Weight to three decimal places:

GrossWeight.Caption = Format(Weight, "######.000")

The value can also be formatted to appear without the decimal point as a rounded value. The following assignment displays the value of Weight in this way:

GrossWeight.Caption = Format(Weight, "######")

An edit mask is a format string, such as "#,###.##", that specifies how you want numeric and string data to appear.