Search Tools Links Login

The Daily Newbie - Using the Chr() function


Visual Basic 6, or VB Classic

To show the usage of the Chr() function.

Original Author: Matt Roberts

Code



content="text/html; charset=iso-8859-1">

Daily Newbie - 04/29/2001



v:shapes="_x0000_s1027">


The
Daily Newbie


“To Start Things
Off Right”


Fourth
Edition?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á
?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á
April 28,
2001?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á
?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á
Free


v:shapes="_x0000_s1027">




Today's command, Chr() is almost a must-know for a lot of string manipulation and should be one of the fundimental tricks in your VB coding bags. If you have read the previous Newbie articles, you already know about the Asc() function. The Chr() function is a compliment of it. While the Asc() Function returns an ASCII code for a character, the Chr() function returns a character for an ASCII character.



face="Arial">Today’s Keyword:
?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á
size="4" face="Arial"> Chr()


style="margin-left:135.0pt;text-indent:-135.0pt">face="Arial">Name Derived
From:?á?á?á?á?á?á?á?á

Character - a symbol (as a letter or number) that represents information; also : a representation of such a character that may be accepted by a computer - Webster's online
  dictionary.


style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;
margin-left:135.0pt;text-indent:-135.0pt">size="2" face="Arial">Used for?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á
Converting an ASCII character to a string character.


style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;
margin-left:135.0pt;text-indent:-135.0pt">size="2" face="Arial">VB Help Description:?á?á?á?á?á?á?á?á?á?á?á?á?áReturns a String containing the character associated with the specified character code.


style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;
margin-left:135.0pt;text-indent:-135.0pt">size="2" face="Arial">Plain
English:?á?á?á?á
?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?áTakes a ASCII Character code and converts it to a "normal" text character.


style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;
margin-left:135.0pt;text-indent:-135.0pt">size="2" face="Arial">Syntax:?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?áChr(ASCII Code)


style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;
margin-left:135.0pt;text-indent:-135.0pt">size="2" face="Arial">Usage:?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?ástrCharacter =Chr(65)


style="margin-left:135.35pt;text-indent:-135.35pt">face="Arial">Copy & Paste Code:






Today's code snippet will print a list of ACII codes and their equivilent character values in the debug window.





Dim intASCII As Integer

For intASCII = 49 To 122
Debug.Print Chr(intASCII)
Next intASCII

style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;
margin-left:135.0pt;text-indent:-135.0pt">?á


style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;
margin-left:135.0pt;text-indent:-135.0pt">size="2" face="Arial">Notes:?á


The reason that the Chr() function is so important is that a lot of things in Visual Basic as based on
ASCII values. For example, in the KeyPress() event of an object, the value that is passed in as the pressed
key is an ASCII value. If you are wanting to display each character on the keypress event, you can do it with this code:

Private Sub Form_KeyPress(KeyAscii As Integer)
MsgBox Chr(KeyAscii)
End Sub

Since the KeyAscii is a VB-defined parameter, the ability to convert it to a character value is pretty important. Chr() Makes this simple. I used Chr() in a simple "word scrambling" project that you can view
by clicking here.






Tomorrow's Keyword: Command()




About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 122 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.