The Data Type Conversion Functions


Table 14.1 describes the data type conversion functions, denoted by their initial letter C (for convert). Each function converts its argument from one data type to another.

Table 14.1 The Data Type Conversion Functions

Function Name Description
CBool() Converts its argument to the Boolean data type
CByte() Converts its argument to the Byte data type
CCur() Converts its argument to the Currency data type
CDate() Converts its argument to the Date data type
CDbl() Converts its argument to the Double data type
CDec() Converts its argument to the Decimal data type
CInt() Converts its argument to the Integer data type
CLng() Converts its argument to the Long data type
CSng() Converts its argument to the Single data type
CStr() Converts its argument to the String data type
CVar() Converts its argument to the Variant data type

You must be able to convert the argument to the target data type. You can't convert the number 123456789 to Integer with CInt(), for example, because an Integer data type can't hold an integer that large

Unlike Int() and Fix(), CInt() returns the closest rounded integer to the argument. Look at the remarks to the right of each of the following statements to see what's stored in each variable:


cA1 = CInt(8.5)       ' Stores an 8 in cA2
cA2 = CInt(8.5001)    ' Stores a 9 in cA3

For negative numbers, CInt() also rounds to the closest whole integer.

The following code declares a variable of four different data types and then converts each argument to those data types. Remember that you also can pass these functions expressions that produce numeric results so that you can control the data types of your calculated results before storing them in a field or variable.


vVar1 = CCur(123)      ' Converts 123 to currency data type
vVar2 = CDbl(123)      ' Converts 123 to double-precision data type
vVar3 = CSng(123)      ' Converts 123 to single-precision data type
vVar4 = CVar(123)      ' Converts 123 to the variant data type

Top Home