Name
Asc, AscW Functions
Class
Microsoft.VisualBasic.Strings
Syntax
Asc(string) AscW(str)
-
string,str(required; String or Char) Any expression that evaluates to a nonempty string
Return Value
An Integer that represents the character code of the first character of the string. The range for the returned value is 0 - 255 on nonDBCS systems, and -32768 to 32767 on DBCS systems.
Description
Returns an Integer representing the character code for the first character of the string passed to it. All other characters in the string are ignored
Rules at a Glance
The string expression passed to the function must contain at least one character or a runtime error is generated.
Only the first character of the string is evaluated by Asc or AscW.
Example
Dim sChars As String
Dim iCharCode As Integer
sChars = TextBox1.Text
If Len(sChars) > 0 Then
iCharCode = Asc(sChars)
If iCharCode >= 97 And iChar <= 122 Then
MsgBox "The first character must be uppercase"
End If
End IfProgramming Tips and Gotchas
Check that the string you are passing to the function contains at least one character using the Len function, as the following example shows:
If Len(sMyString) > 0 Then iCharCode = Asc(sMyString) Else MsgBox("Cannot process a zero-length string") End IfUse Asc within your data-validation routines to determine such conditions as whether the first character is upper- or lowercase and whether it is alphabetic or numeric, as the following example demonstrates:
Private Sub Button1_Click(ByVal sender As System.Object, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access