Access VBA Macro Programming

(Joao Candeias) #1

Because this deals with the entire character set, it also includes nonprintable characters.
For example, ASCII code 13 is a carriage return, which can be useful if you want to force a
carriage return on something like a message box:


MsgBox "This is " & Chr( 13 ) & "a carriage return"


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 theMidfunction to separate out that digit. You can then convert it back to a numeric for
the purposes of performing further calculations.


CStr


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


Cstr( 1234 )


CInt


CIntconverts 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")


CIntdoes not work like theIntfunction 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


CLngconverts 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( 12345678 9.45)
CLng(" 12345678 9.45")


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


Chapter 5: Strings, Functions, and Message Boxes 51

Free download pdf