Asc Function
Returns an Integer representing the character code corresponding to the first letter in a string.
Syntax
Asc(string)
Parameters
string- Required. Any valid string expression. If the string contains no characters, a run-time error occurs.
Return Value
Returns an Integer representing the ANSI character code of the first character in the string.
- For
ANSIcharacters (0-127), returns standardASCIIvalues - For extended
ANSIcharacters (128-255), returns extendedASCIIvalues - Unicode characters are converted to
ANSIbefore the code is returned
Remarks
The Asc function returns the numeric ANSI character code for the first character in a string.
This is useful for:
- Validating input characters
- Performing character-based operations
- Converting characters to their numeric representations
- Character range checking
Important Notes
- Only First Character: Only the first character of the string is examined
- Empty String Error: Passing an empty string results in a run-time error (Error 5: Invalid procedure call or argument)
- ANSI vs. Unicode: In VB6,
AscreturnsANSIcodes;AscWreturns Unicode values - Return Type: Returns
Integer(16-bit signed), range -32,768 to 32,767, but character codes are 0-255 - Case Sensitive: Upper and lowercase letters have different codes (e.g., "A" = 65, "a" = 97)
Character Code Ranges
- 0-31: Control characters (non-printable)
- 32: Space
- 48-57: Digits '0' through '9'
- 65-90: Uppercase letters 'A' through 'Z'
- 97-122: Lowercase letters 'a' through 'z'
- 128-255: Extended ANSI characters
Examples
Basic Usage
Dim code As Integer
code = Asc("A") ' Returns 65
code = Asc("Apple") ' Returns 65 (first character only)
code = Asc("a") ' Returns 97
code = Asc("0") ' Returns 48
code = Asc(" ") ' Returns 32 (space)
Character Validation
Function IsDigit(ch As String) As Boolean
Dim code As Integer
code = Asc(ch)
IsDigit = (code >= 48 And code <= 57)
End Function
Function IsUpperCase(ch As String) As Boolean
Dim code As Integer
code = Asc(ch)
IsUpperCase = (code >= 65 And code <= 90)
End Function
Case Conversion Offset
' Calculate offset between upper and lower case
Dim offset As Integer
offset = Asc("a") - Asc("A") ' Returns 32
Character Range Checking
Function IsPrintable(ch As String) As Boolean
Dim code As Integer
code = Asc(ch)
IsPrintable = (code >= 32 And code <= 126)
End Function
String Encoding
Function EncodeString(s As String) As String
Dim i As Integer
Dim result As String
For i = 1 To Len(s)
If result <> "" Then result = result & ","
result = result & CStr(Asc(Mid(s, i, 1)))
Next i
EncodeString = result
End Function
Common Patterns
1. Input Validation
If Asc(userInput) >= 48 And Asc(userInput) <= 57 Then
' First character is a digit
End If
2. Alphabetic Checking
Dim code As Integer
code = Asc(UCase(letter))
If code >= 65 And code <= 90 Then
' It's a letter
End If
3. CSV Parsing Helper
If Asc(field) = 34 Then ' 34 is double quote
' Handle quoted field
End If
4. Character Comparison
If Asc(char1) < Asc(char2) Then
' char1 comes before char2 in ASCII order
End If
5. Special Character Detection
Select Case Asc(ch)
Case 9 ' Tab
Case 10 ' Line feed
Case 13 ' Carriage return
Case 32 ' Space
End Select
6. Keyboard Input Processing
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then ' Enter key
' Process input
End If
End Sub
7. Character Class Testing
Function IsControl(ch As String) As Boolean
Dim code As Integer
code = Asc(ch)
IsControl = (code < 32 Or code = 127)
End Function
8. Simple Encryption
Function ROT13Char(ch As String) As String
Dim code As Integer
code = Asc(UCase(ch))
If code >= 65 And code <= 90 Then
code = ((code - 65 + 13) Mod 26) + 65
ROT13Char = Chr(code)
Else
ROT13Char = ch
End If
End Function
Common Character Codes
| Character | Code | Description |
|---|---|---|
| Null | 0 | Null character |
| Tab | 9 | Horizontal tab |
| LF | 10 | Line feed |
| CR | 13 | Carriage return |
| Space | 32 | Space |
| ! | 33 | Exclamation mark |
| " | 34 | Double quote |
| 0 | 48 | Digit zero |
| 9 | 57 | Digit nine |
| A | 65 | Uppercase A |
| Z | 90 | Uppercase Z |
| a | 97 | Lowercase a |
| z | 122 | Lowercase z |
| DEL | 127 | Delete |
Error Handling
On Error Resume Next
code = Asc(inputString)
If Err.Number = 5 Then
' Empty string error
MsgBox "String cannot be empty"
End If
Related Functions
AscB: Returns the first byte of a stringAscW: Returns the Unicode character codeChr: Returns the character for a given character code (inverse of Asc)ChrB: Returns a byte containing the characterChrW: Returns a Unicode characterInStr: Finds the position of a character in a stringMid: Extracts a substringLeft: Gets leftmost charactersRight: Gets rightmost characters
Performance Notes
- Asc is a very fast operation (direct character code lookup)
- More efficient than string comparison for single character checks
- Use Asc for character-based validation instead of multiple string comparisons
- In tight loops, cache Asc results if checking the same character repeatedly
Parsing Notes
The Asc function is not a reserved keyword in VB6. It is parsed as a regular
function call (CallExpression). This module exists primarily for documentation
purposes and to provide a comprehensive test suite that validates the parser
correctly handles Asc function calls in various contexts.