VB6Parse / Library / String / chrw_dollar

VB6 Library Reference

ChrW$ Function

Returns a String containing the Unicode character associated with the specified character code. The dollar sign suffix ($) explicitly indicates that this function returns a String type (not a Variant), and the "W" suffix indicates this is the wide (Unicode) version.

Syntax

ChrW$(charcode)

Parameters

Return Value

Returns a String containing the single Unicode character corresponding to the specified character code. The return value is always a String type (never Variant), and represents a Unicode character (2 bytes).

Remarks

Common Character Codes

Code Character Constant Description
0 (null) vbNullChar Null character
9 \t vbTab Horizontal tab
10 \n vbLf Line feed
13 \r vbCr Carriage return
32 (space) - Space character
34 " - Double quote
39 ' - Single quote
65 A - Uppercase A
97 a - Lowercase a
169 © - Copyright symbol
8364 - Euro sign

Typical Uses

  1. Unicode characters - Access characters beyond the ANSI range
  2. International text - Work with non-English characters
  3. Special symbols - Insert mathematical, currency, and other Unicode symbols
  4. Line breaks - Insert carriage returns and line feeds
  5. Emoji and symbols - Access Unicode symbols (within BMP range)
  6. Cross-platform text - Generate Unicode text for better compatibility
  7. Web content - Create Unicode strings for web applications

Basic Examples

' Example 1: Get character from code
Dim ch As String
ch = ChrW$(65)  ' Returns "A"
' Example 2: Unicode character beyond ANSI
Dim euro As String
euro = ChrW$(8364)  ' Returns "€"
' Example 3: Copyright symbol
Dim copyright As String
copyright = ChrW$(169)  ' Returns "©"
' Example 4: Line break
Dim msg As String
msg = "Line 1" & ChrW$(13) & ChrW$(10) & "Line 2"

Common Patterns

Multi-line Strings

Function CreateMultiLine() As String
Dim result As String
result = "First Line" & ChrW$(13) & ChrW$(10)
result = result & "Second Line" & ChrW$(13) & ChrW$(10)
result = result & "Third Line"
CreateMultiLine = result
End Function

Unicode Symbols

Function CreateCopyrightNotice(year As String, company As String) As String
CreateCopyrightNotice = "Copyright " & ChrW$(169) & " " & year & " " & company
End Function

Currency Symbols

Function FormatPrice(amount As Double, currency As String) As String
Dim symbol As String
Select Case currency
Case "EUR"
symbol = ChrW$(8364)  ' €
Case "GBP"
symbol = ChrW$(163)   ' £
Case "YEN"
symbol = ChrW$(165)   ' ¥
Case Else
symbol = "$"
End Select
FormatPrice = symbol & Format(amount, "0.00")
End Function

International Characters

Function GetGreeting(language As String) As String
Select Case language
Case "German"
GetGreeting = "Gr" & ChrW$(252) & ChrW$(223) & " Gott"  ' Grüß Gott
Case "French"
GetGreeting = "Fran" & ChrW$(231) & "ais"  ' Français
Case "Spanish"
GetGreeting = "Espa" & ChrW$(241) & "ol"   ' Español
Case Else
GetGreeting = "Hello"
End Select
End Function

Mathematical Symbols

Function CreateMathExpression() As String
Dim result As String
result = "x " & ChrW$(8804) & " 10"  ' x ≤ 10
result = result & " " & ChrW$(8743) & " "  ' ∧ (and)
result = result & "x " & ChrW$(8805) & " 0"  ' x ≥ 0
CreateMathExpression = result
End Function

Tab-Separated Values

Function CreateTSV(col1 As String, col2 As String, col3 As String) As String
CreateTSV = col1 & ChrW$(9) & col2 & ChrW$(9) & col3
End Function

Quote in String

Function AddQuotes(text As String) As String
AddQuotes = ChrW$(34) & text & ChrW$(34)
End Function

Bullet Points

Function CreateBulletList() As String
Dim result As String
Dim bullet As String
bullet = ChrW$(8226)  ' •
result = bullet & " First item" & ChrW$(13) & ChrW$(10)
result = result & bullet & " Second item" & ChrW$(13) & ChrW$(10)
result = result & bullet & " Third item"
CreateBulletList = result
End Function

Null-Terminated String

Function CreateNullTerminated(text As String) As String
CreateNullTerminated = text & ChrW$(0)
End Function

Generate Alphabet

Function GenerateAlphabet() As String
Dim i As Integer
Dim result As String
For i = 65 To 90
result = result & ChrW$(i)
Next i
GenerateAlphabet = result  ' Returns "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
End Function

Error Handling

Function SafeChrW(code As Long) As String
On Error Resume Next
SafeChrW = ChrW$(code)
If Err.Number <> 0 Then
SafeChrW = ""
Err.Clear
End If
End Function

Performance Considerations

Best Practices

  1. Use named constants for common characters instead of magic numbers
  2. Use ChrW$ for Unicode characters, Chr$ for ANSI-only characters
  3. Document character codes with comments showing the actual character
  4. Validate character codes are in valid range before calling ChrW$
  5. Use vbCrLf constant instead of ChrW$(13) & ChrW$(10) when possible
  6. Prefer ChrW$ over ChrW when you need a String result
  7. Consider internationalization when working with Unicode characters

Unicode Ranges

Limitations

← Back to String | View all functions