Chr Chr ChrB ChrB ChrW Functions

Named Arguments

Syntax

Chr( charactercode) Chr$( charactercode) ChrB( charactercode) ChrB$( charactercode) ChrW( charactercode) charactercode

Use: Required

Data Type: Long

An expression that evaluates to either an ASCII or DBCS character code.

Return Value

Chr, ChrB, and ChrW return a variant of the string subtype that contains the character represented by charactercode.

Chr$ and ChrB$ return a string containing the character represented by char-

actercode.

Description

Returns the character represented by charactercode.

Rules at a Glance

• Chr and Chr$ return the character associated with an ASCII or ANSI character code.

• ChrB and ChrB$ return a one-byte string variant or a one-byte string, respectively.

• ChrW returns a Unicode character; however, on systems that don't support the Unicode character set, the function behaves identically to the Chr function.

Programming Tips & Gotchas

• Use Chr(34) to embed quotation marks inside a string, as shown in the following example:

where myColumn = " & Chr(34) & sValue & Chr(34)

• It's up to you as the programmer to decide which variation of the function to use—that is, whether to use the string or variant version of the function. The String versions, Chr$ and ChrB$ use less memory than their variant counterparts; however, you may find the variant versions more flexible, since they convert data types automatically and handle Null values more cleanly.

• You can use the ChrB function to assign binary values to String variables. Try this little demonstration of outputting a Unicode character (Unicode charac-

Chr, Chr$, ChrB, ChrB$, ChrW Functions 159

ters are two bytes in length; for example, "F" is represented by binary 70 and binary 0):

Dim sBinVar As String sBinVar = ChrB(70) & ChrB(0) Debug.Print sBinVar

• You can use the ChrW function to return Unicode characters, but for the most part these are difficult to see in VB, as the immediate window, label, and textbox know how to display only the Unicode equivalent of ANSI characters! However, try this code to produce a Unicode "G":

Dim sBinVar As String sBinVar = ChrW(AscW("G")) Debug.Print sBinVar

Well, wasn't that exciting: a "G" was displayed in the immediate window! The difference is that the character displayed is a Unicode "G". I wouldn't, however, recommend that you try to convert all ANSI characters in this way; it's better to use the StrConv function.

• The following table lists some of the more commonly used character codes that are supplied in the call to the Chr function:

Code

Value

Description

0

NULL

For C/C++ string functions, the null character required to terminate standard strings; equivalent to the vbNullChar constant.

8

BS

Equivalent to the vbBack constant.

9

TAB

Equivalent to the vbTab constant.

10

CR

Equivalent to the vbCr and vbCrLf constants.

13

LF

Equivalent to the vbLf and vbCrLf constants.

34

Quotation mark. Useful to embed quotation marks within a literal string, especially when forming SQL query strings.

See Also

Asc Function, CStr Function, Str Function

Was this article helpful?

+6 -3

Responses

  • agenore
    How to convert Chr() to ChrW()?
    8 years ago
  • KATRI AUNULA
    How to use the ChrW function in vba word macro?
    8 years ago
  • basso
    How to express hex numbers in visual studio chrw function?
    3 months ago

Post a comment