Asc AscB AscW Functions

Named Arguments No

Syntax

Asc( string) AscB( string) AscW( string) string

Use: Required

Data Type: String

Any expression that evaluates to a 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 non-DBCS systems, but -32768-32767 on DBCS systems.

Description

Returns the ANSI or Unicode character code that represents the first character of the string passed to it. All other characters in the string are ignored. Use AscB with Byte data and AscW on Unicode (DBCS) systems.

Rules at a Glance

• The string expression passed to the function must contain at least one character, or a runtime error (either "Invalid use of Null" or "Invalid procedure call or argument") is generated.

• Only the first character of the string is evaluated by Asc, AscB, and AscW.

• Use the AscW function to return the Unicode character of the first character of a string.

• Use the AscB function to return the first byte of a string containing byte data. Example

Dim sChars As String Dim iCharCode As Integer sChars = TextBox1.Text If Len(sChars) > 0 Then

Asc, AscB, AscW Functions 129

iCharCode = Asc(sChars)

MsgBox "The first character must be uppercase" End If End If

Programming Tips & Gotchas

• Always 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 If

• On platforms which don't support Unicode, the AscW function performs exactly the same as Asc.

• Surprisingly, although the VB Object Browser clearly shows that the data type of the parameter passed to the Asc function is String, it can actually be any data type. Evidently the Asc routine converts incoming values to strings before extracting their first character.

• Use Asc within your data validation routines to determine such conditions as whether the first character is upper- or lowercase and whether it's alphabetic or numeric, as the following example demonstrates:

Private Sub CommandButton1_Click()

Dim sTest As String Dim iChar As Integer sTest = TextBox1.Text

MsgBox "The first character is UPPERCASE" ElseIf iChar >= 97 And iChar <= 122 Then

MsgBox "The first character is lowercase" Else

MsgBox "The first character isn't alphabetical" End If Else

MsgBox "Please enter something in the text box" End If

End Sub

• Use the Asc function and its converse, Chr, to create rudimentary encryption methods. Once you have obtained the character code for a particular character, you can perform calculations on this code to come up with a different number and then convert this to a character using the Chr function. To decrypt your string, simply reverse the calculation. Be sure, though, that your

130 Chapter 7- The Language Reference calculation doesn't generate character codes less than 20, since these are special nonprinting characters, which, if displayed or printed, can cause undesirable effects.

Private Sub CommandButton2_Click()

Dim MyName As String, MyEncryptedString As String Dim MyDecryptedString As String Dim i As Integer

MyName = "Paul Lomax"

MyEncryptedString = MyEncryptedString & _

Next i

MsgBox "Hello, my name is " & MyEncryptedString

MyDecryptedString = MyDecryptedString & _ Chr(Asc(Mid(MyEncryptedString, i, 1)) - 25) Next i

MsgBox "Hello, my name is " & MyDecryptedString End Sub

Was this article helpful?

+4 0

Responses

  • SARAH
    What is the difference between "chrw()" and "ascw()" functions in access vba?
    5 years ago

Post a comment