Name

Chr, ChrB, ChrW Functions

Syntax

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

Use: Required

Data Subtype: 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 .

Description

Returns the character represented by charactercode.

Rules at a Glance

  • Chr returns the character associated with an ASCII or ANSI character code.

  • ChrB returns 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 Chr.

Programming Tips & Gotchas

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

    sSQL = "SELECT * from myTable where myColumn = " & Chr(34) & _
                  sValue & Chr(34)
  • You can use the ChrB function to assign binary values to String variables. Try this little demonstration of outputting a Unicode character (Unicode characters are two bytes in length; for example, “F” is represented by binary 70 and binary 0):

    Dim sBinVar
    sBinVar = ChrB(70) & ChrB(0)
    MsgBox BinVar
  • You can use the ChrW function to return Unicode characters. Try this code to produce a Unicode “G”:

    Dim sBinVar
    sBinVar = ChrW(AscW("G"))
    MsfBox sBinVar

    Well, wasn’t that exciting? A “G” was displayed in the message dialog! The difference is that the character displayed is a Unicode “G”.

  • The following ...

Get VBScript in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.