KeyAscii

Be.gif (24359 bytes)

 

Menu

KeyAscii

KeyAscii is important to a project if you are making a calculator or somthing that requires specific text. If you use a calculator, you should notice that you cant type A through to Z, this is done by useing KeyAscii.
Im not sure how to go into alot of detail, hopefully you know a bit of visual Basic already so this should make sense. Add a Textbox and open the codeing area for it, then change it from Text1_Change to Text1_KeyPress.

Private Sub Text1_KeyPress(KeyAscii As Integer)
'Only allow number keys, or backspace
If (KeyAscii >= vbKey0 And KeyAscii <= vbKey9) Or KeyAscii = vbKeyBack Then
    Exit Sub
Else
    KeyAscii = 0
    Beep
End If
End Sub

In this example above when you type into Text1 you can only type numbers 0 to 9 and can only press the BackSpace key. If you was to use this as a calculator you would soon find a major problem, the Decimal point has been filterd with the KeyAscii. There is no Decimal Point in Visual Basics Variables, so we will have to make one. Place the below in your General Declarations area.

Option Explicit
Const vbKeyDecPt = 46

This is simply declaring the Decimal Point, if you know change the Text1_KeyPress to ....

Private Sub Text1_KeyPress(KeyAscii As Integer)
'Only allow number keys, decimal point, or backspace
If (KeyAscii >= vbKey0 And KeyAscii <= vbKey9) Or KeyAscii = vbKeyDecPt Or KeyAscii = vbKeyBack Then
    Exit Sub
Else
    KeyAscii = 0
    Beep
End If
End Sub

This should allow only numbers the BackSpace and the Decimal Point to be pressed. If the user trys to enter any other letter, a Beep will be heard.

If you have any problems, broken links or just infomation or a query then please E-Mail me Here.
All pictures and files have been created by us.
Created on April 24th 2001.