article

The Daily Newbie - Using the Chr() function

Email
Submitted on: 1/16/2015 8:05:00 PM
By: Matthew Roberts (from psc cd)  
Level: Beginner
User Rating: By 2 Users
Compatibility: VB 3.0, VB 4.0 (16-bit), VB 4.0 (32-bit), VB 5.0, VB 6.0, VB Script, ASP (Active Server Pages) , VBA MS Access, VBA MS Excel
Views: 765
 
     To show the usage of the Chr() function.

 
				



Daily Newbie - 04/29/2001


The Daily Newbie

“To Start Things Off Right”

Fourth Edition April 28, 2001 Free

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.

Today’s Keyword: Chr()

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.

Used for Converting an ASCII character to a string character.

VB Help Description: Returns a String containing the character associated with the specified character code.

Plain English: Takes a ASCII Character code and converts it to a "normal" text character.

Syntax: Chr(ASCII Code)

Usage: strCharacter =Chr(65)

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

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()


Other 25 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.