MS-Access / Getting Started

Conversion Functions

Conversion functions are used to convert a value from one format to another. An example would be converting a numeric value into a string or converting the string back into a numeric value. These functions are extremely useful for switching values between various formats. For example, you may have a four-figure number where you want the second digit on its own. One of the easiest ways to do this is to convert the number into a string and then use the Mid function to separate out that digit. You can then convert it back to a numeric for the purposes of performing further calculations.

CStr

Cstr converts a value to a string. The following example will produce the string "1234":

Cstr(1234)

CInt

CInt converts a value or a string to an integer (two bytes). No decimal places are shown. Both of the following examples will give the value 123:

CInt (123.45)
CInt("123.45")

CInt does not work like the Int function but instead rounds to the nearest whole number in lieu of truncating. If the expression has any nonnumerical characters, you will get a Type Mismatch error.

CLng

CLng converts a value or a string to a long integer (four bytes). No decimal places are shown. Both of the following examples will return the value 123456789:

CLng(123456789.45)
CLng("123456789.45")

Note that CLng does not work like the Int function but rounds to the nearest whole number instead of truncating. If the expression has any nonnumerical characters, you will get a Type Mismatch error.

CDbl

CDbl converts a value or a string to a double-precision floating point number (eight bytes) where decimal places are allowed:

CDbl("123.56789")

This will return the value 123.56789.
If the expression has any nonnumeric characters, you will get a Type Mismatch error.

Val

Val converts a string to a value. It is more forgiving than CInt or CLng because it will accept nonnumeric characters:

Val("123")

This will give the value 123. The following will give the value 123.45:

Val("123.45")

The next example will give the value 12:

Val("12martin")

The following will give the value 0, meaning there are no numeric characters to evaluate:

Val("martin")
[Previous] [Contents] [Next]