Type Conversion Functions

Applies To
Access for Microsoft 365 Access 2024 Access 2021 Access 2019 Access 2016

Each function converts an expression to a specific data type.

Syntax

  • CBool(expression)
  • CByte(expression)
  • CCur(expression)
  • CDate(expression)
  • CDbl(expression)
  • CDec(expression)
  • CInt(expression)
  • CLng(expression)
  • CSng(expression)
  • CStr(expression)
  • CVar(expression)

The required expression argument is any string or numeric expression.

Return types

The function name determines the return type, as shown in the following table:

Function Return type Range for expression argument
CBool Boolean Any valid string or numeric expression.
CByte Byte 0 to 255.
CCur Currency -922,337,203,685,477.5808 to 922,337,203,685,477.5807.
CDate Date Any valid date expression.
CDbl Double -1.79769313486231E308 to -4.94065645841247E-324 for negative values; 4.94065645841247E-324 to 1.79769313486232E308 for positive values.
CDec Decimal +/-79,228,162,514,264,337,593,543,950,335 for zero-scaled numbers, or numbers with no decimal places. For numbers with 28 decimal places, the range is +/-7.9228162514264337593543950335. The smallest possible nonzero number is 0.0000000000000000000000000001.
CInt Integer -32,768 to 32,767; fractions are rounded.
CLng Long -2,147,483,648 to 2,147,483,647; fractions are rounded.
CSng Single -3.402823E38 to -1.401298E-45 for negative values; 1.401298E-45 to 3.402823E38 for positive values.
CStr String The return value depends on the expression argument.
CVar Variant Same range as Double for numeric values. Same range as String for nonnumeric values.

Remarks

If the expression passed to the function is outside the range of the data type that you're converting to, an error occurs.

In general, you can document your code by using data type conversion functions to show that the result of an operation should use a particular data type instead of the default type. For example, use CCur to force currency arithmetic in cases where single-precision, double-precision, or integer arithmetic would normally occur.

Use the data type conversion functions instead of Val to provide locale-aware conversions from one data type to another. For example, when you use CCur, Access recognizes different decimal separators, thousand separators, and currency options based on your computer's locale setting.

When the fractional part is exactly 0.5, CInt and CLng always round it to the nearest even number. For example, 0.5 rounds to 0, and 1.5 rounds to 2. CInt and CLng differ from the Fix and Int functions, which truncate the fractional part of a number instead of rounding it. Fix and Int also always return a value of the same type that you pass in.

Use the IsDate function to determine whether date can be converted to a date or time. CDate recognizes date literals and time literals, as well as some numbers that fall within the range of acceptable dates. When you convert a number to a date, the whole number portion becomes a date. Any fractional part becomes a time of day that starts at midnight.

CDate recognizes date formats according to your system locale setting. Access might not determine the correct order of day, month, and year if you provide a format other than one of the recognized date settings. In addition, it doesn't recognize a long date format if it also contains the day-of-the-week string.

The CVDate function is also available for compatibility with earlier versions of Visual Basic. The syntax of CVDate is identical to the CDate function. However, CVDate returns a Variant whose subtype is Date instead of an actual Date type. Because Access now includes an intrinsic Date type, you no longer need CVDate. You can get the same effect by converting an expression to a Date and then assigning it to a Variant. This technique is consistent with the conversion of other intrinsic types to their equivalent Variant subtypes.

Note

The CDec function doesn't return a discrete data type. Instead, it always returns a Variant whose value has been converted to a Decimal subtype.

Query examples

Expression Results
SELECT SalePrice, FinalPrice, CBool(SalePrice > FinalPrice) AS Expr1 FROM productSales; Returns SalePrice and FinalPrice, and evaluates whether SalePrice is greater than FinalPrice. Returns -1 if it's true and 0 if it's false.
SELECT ProductID, CByte(Quantity) AS Expr1 FROM productSales; Returns ProductID, converts the values in the Quantity field to Byte, and displays them in the Expr1 column.
SELECT ProductID, CDate(DateofSale) AS Expr1 FROM productSales; Returns ProductID, converts the values in the DateofSale field to Date, and displays them in the Expr1 column.
SELECT ProductID, CDbl(Discount) AS Expr1 FROM productSales; Returns ProductID, converts the values in the Discount field to Double, and displays them in the Expr1 column.
SELECT ProductID, CInt(Discount) AS Expr1 FROM productSales; Returns ProductID, converts the values in the Discount field to Integer, and displays them in the Expr1 column.
SELECT ProductID, CLng(Discount) AS Expr1 FROM productSales; Returns ProductID, converts the values in the Discount field to Long, and displays them in the Expr1 column.
SELECT ProductID, CSng(Discount) AS Expr1 FROM productSales; Returns ProductID, converts the values in the Discount field to Single, and displays them in the Expr1 column.
SELECT ProductID, CStr(Discount) AS Expr1 FROM productSales; Returns ProductID, converts the values in the Discount field to String, and displays them in the Expr1 column.
SELECT ProductID, CVar(Discount) AS Expr1 FROM productSales; Returns ProductID, converts numeric Discount values to Double, and converts nonnumeric values to String.

VBA examples

Note

The following examples show how to use this function in a Visual Basic for Applications (VBA) module. For more information about working with VBA, see the Access VBA reference.

CBool function

This example uses the CBool function to convert an expression to a Boolean. If the expression evaluates to a nonzero value, CBool returns True; otherwise, it returns False.

Dim A, B, Check
A = 5: B = 5    ' Initialize variables.
Check = CBool(A = B)    ' Check contains True.
A = 0    ' Define variable.
Check = CBool(A)    ' Check contains False.

CByte function

This example uses the CByte function to convert an expression to a Byte.

Dim MyDouble, MyByte
MyDouble = 125.5678    ' MyDouble is a Double.
MyByte = CByte(MyDouble)    ' MyByte contains 126.

CCur function

This example uses the CCur function to convert an expression to a Currency.

Dim MyDouble, MyCurr
MyDouble = 543.214588    ' MyDouble is a Double.
MyCurr = CCur(MyDouble * 2)
' Convert result of MyDouble * 2 (1086.429176) to a
' Currency (1086.4292).

CDate function

This example uses the CDate function to convert a string to a Date. In general, hard-coding dates and times as strings, as shown in this example, isn't recommended. Use date literals and time literals, such as #2/12/1969# and #4:45:23 PM#, instead.

Dim MyDate, MyShortDate, MyTime, MyShortTime
MyDate = "February 12, 1969"
' Convert to Date data type.
MyShortDate = CDate(MyDate)
MyTime = "4:35:47 PM"
' Convert to Date data type.
MyShortTime = CDate(MyTime)

CDbl function

This example uses the CDbl function to convert an expression to a Double.

Dim MyCurr, MyDouble
MyCurr = CCur(234.456784)
' Convert result to a Double.
MyDouble = CDbl(MyCurr * 8.2 * 0.01)

CDec function

This example uses the CDec function to convert a numeric value to a Decimal.

Dim MyDecimal, MyCurr
MyCurr = 10000000.0587    ' MyCurr is a Currency.
MyDecimal = CDec(MyCurr)     ' MyDecimal is a Decimal.

CInt function

This example uses the CInt function to convert a value to an Integer.

Dim MyDouble, MyInt
MyDouble = 2345.5678    ' MyDouble is a Double.
MyInt = CInt(MyDouble)    ' MyInt contains 2346.

CLng function

This example uses the CLng function to convert a value to a Long.

Dim MyVal1, MyVal2, MyLong1, MyLong2
MyVal1 = 25427.45
MyVal2 = 25427.55  ' MyVal1, MyVal2 are Doubles.
MyLong1 = CLng(MyVal1)
' MyLong1 contains 25427.
MyLong2 = CLng(MyVal2)
' MyLong2 contains 25428.

CSng function

This example uses the CSng function to convert a value to a Single.

Dim MyDouble1, MyDouble2, MySingle1, MySingle2
' MyDouble1, MyDouble2 are Doubles.
MyDouble1 = 75.3421115: MyDouble2 = 75.3421555
MySingle1 = CSng(MyDouble1)
' MySingle1 contains 75.34211.
MySingle2 = CSng(MyDouble2)
' MySingle2 contains 75.34216.

CStr function

This example uses the CStr function to convert a numeric value to a String.

Dim MyDouble, MyString
MyDouble = 437.324    ' MyDouble is a Double.
MyString = CStr(MyDouble)
' MyString contains "437.324".

CVar function

This example uses the CVar function to convert an expression to a Variant.

Dim MyInt, MyVar
MyInt = 4534    ' MyInt is an Integer.
MyVar = CVar(MyInt & "000")
' MyVar contains the string 4534000.