Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
  HOME    TRACKER    Visual Basic

KeyPress(KeyAscii As Integer) - Problem

Hi guys been a whie since i been on as I have been moving to Australia from Scotland.
Finaly built a beast of a PC for development using japanese import parts - Uber cheap.

ANyways i am having this simple problem.

I use the bleow code to "protect" my users from crashing the software by inputting Text when its numbers only.

Private Sub txttime2_KeyPress(KeyAscii As Integer)
   If Len(txttime2.Text) = 0 And KeyAscii = 45 Then
      KeyAscii = 0
   End If
   If KeyAscii >= 58 Or (KeyAscii <= 47 And KeyAscii <> 45 And KeyAscii <> 8 And KeyAscii <> 13) Then
      KeyAscii = 0
   End If
End Sub

My problem is that the code prevents them from using the "-" symbol which is required later in the calculations, i know its gonna be something silly im missing but im at my wits end.

Other problem im having is i have a number lets call it X, now i want the calculation to gradually change X where the formulae would be (X+2). this would occur continuosl.

i.e 2 then 4 then 6.....

ANy help is always appreciated.



(my software gets its release in a few weeks time Version 1.0 Beta.)
already developing it for the xchange to (hard work)

View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
KeyPress And The Keyascii?
Is there a way I can replace one keyascii with (2) keyascii codes in the keypress evant?

Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 39 Then
KeyAscii = 51
KeyAscii = 52
End If
End Sub

This always overwrite the first character, I need it to be one after the other.

I am trying to capture a key press inside a text box, if the user hits the enter key after typing somthing in the block of code will execute
CODEPrivate Sub txtOldPass_KeyPress(KeyAscii As Integer)
    If (KeyAscii = 13) Then
        'DO STUFF
    End If
End Sub

Using KeyPress To Compare KeyAscii Values
Need assistance in using the KeyPress Sub to translate a Carriage Return and a Space into a different character.

What I am trying to accomplish is when a person hits Carriage Return in a text field, it converts it into
and when they hit space it converts it into _ .

I understand I need to use

Private Sub Form_KeyPress (KeyAscii as Integer)
If KeyAscii = Asc (13) (I assume this is right for a CR?)
Then KeyAscii = Asc (92) & Asc (110) (Hoping this produces a

But for some reason, it wont recognize Asc (13) as a carriage return. I even tried Asc ("13") but that doesnt seem to work either.

Any help would be appreciated.

When Use Form_KeyPress(KeyAscii As Integer) It Always Play Beep Sound.
When I use Form_KeyPress(KeyAscii As Integer), the system always play beep sound... how to disable the beep sound?

How To Convert From Integer/long Integer To Binary ?
How to convert from integer or long integer to binary in Visual Basic,
for example : 9568 to 10010101100000
and how to convert back from binary to integer or long integer,
for example : 10010101100000 to 9568.

Please help.

Integer Equals Value Of Another Integer

I am trying to get intSus be equal to the value of intDown in the following code and I don't know why it won't pass the value on .

Any help would be greatly appreciated


Dim intDown as integer
Dim intSus as integer
intDown = 1
intSus = 1
Do[indent] Sheets("Sheet3").Select
If Range("A" & intDown).Value = Name1 Then
intSus = intDown '-------- this line doesn't work
intDown = intDown + 1
End If[/indent]Loop Until Range("A" & intDown).Value = Name1

Integer Closest To A Integer
I have a array of 5 variables that are all positive even integers. The integers are anywhere between 0-9. How can I find out which one of those variables less than or equal to a number. The number is Dim'ed as Single.

Ok so in my program i need to detect wether the user presses left or right arrow keys.Heres the code:

if KeyAnscii = ? then
x = x + 5
if KeyAnscii = ? then
y = y + 5
end if
What do i put instead of the ? marks?
The top is left
bottom is right.Ignore the x + 5 or watever.

I'm wondering what the VB KeyAscii for the up/down/left/right arrows are. Thanx in advance.

Where can I find different keyascii values for the F keys?
Or how can I do this:
Either using ascii or another replacement for an F key?

Private Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii = bah Then
Call bloop
End If
End Sub

How can i get the keyAscii which people typed in to applications other than VB forms? anyone knows ?

a novice

Instead Of Keyascii
I am experiencing a little problem when with the KeyAscii command. I just wonder if there's another way of doing this.

I have a textbox (text1) that don't accept spaces.
When the space bar is pressed a message will appear...

This is how the code looks like.

I put this of course in the keypress event

Dim ABC As String
If KeyAscii = 32 Then
KeyAscii = 0
ABC = MsgBox("bla bla bla..........
end if

It works this way but if I copy a string with spaces for instance: " " (without quotes), and paste it into the textbox the message box wont pop up.

This is what I want instead....

When typing spaces in the textbox a message box shall NOT appear.
But when I press a button (command1) I want a message box to appear telling the user that the text box contains spaces.

is there a website that tells what each key is numbered, or how are they numbered (sorry im new to this stuff)

does anyone know the keyascii number for the up arrow key? e.g

13 is enter key? just so i can use code like

if keyascii = 13 then
do stuff here
end if

cheers guys

Hi Guys

I have this code

If (Shift And vbCtrlMask) = vbCtrlMask And KeyCode = vbKeyF3

Ho will i go on about doing this.

i want to hold down Ctrl, shift, F3, F6, F8 and then it must only execute the code.
How will i go on to do this.

Hi People.

How can i set the forms focus. I have a settings dialog but its a secret form.

So what i want to do is hold down CTRL SHIFT R on the form to bring up the secret form. I tried this code on a tex tbox and it works. When i try it on the form nothing happens.

Private Sub txtDistance_KeyPress(KeyAscii As Integer)
If KeyAscii = vbKeyA Then
MsgBox "test"
End Sub

ps: This is just a text code

hi all

is there a way to display the key the user presses onscreen as the ascii code for it

for example - the user presses "q" and the ascii code is displayed on screen

any ideas, thanks

Need Help In KeyAscii
I Want to now the Ascii keis for this bottoms:


Keyascii Value Of Tab
pls tell me keyascii value of TAb key as such keyascii value of Enter key is 13.


I want a text box to accept only Alpha(A-Z or a - z) and / ' . (KeyAscii 39,46,47) and only one space. what is the simplest coding?


1) what is the Keascii code for Back space and forward sapce

2) if Keyascii = 0 then nothing types in the text box
if backspace and forward space keys are pressed
i would like to see the cursor moving

thanks in advance


Sam F

I'm finding out where can i get the full list of AsciiKey code. Cos i nd to use it to do sum prompting where a user of the program accidentally key in sumthing tt r not suppose to be.

Does any1 hav it can send it to me or tell me the website... thks.

Getting The Keyascii Value
how to get thge keyascii value of the key pressed in a
message box ??????

Keyascii Q
how can i make it so that they can only hit key's a and b?

hi there,
I use this code on each text_KeyPress
i think it could be use only at once in the form
instead of rewrite this code every time ,

Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
End If
End Sub

could someone help pls.
thanks in advance

KeyAscii Help
Hi everyone, just a quick question which is puzzling me.

I have a text box, where each time the enter button is pressed I would like a <BR> to be placed on a new line underneeth. I am currently using this...

If KeyAscii = 13 Then
txtarticle = txtarticle + "<br>" + vbCrLf
End If

but the cursor is still at the start position, is there a way for me to move the cursor down to the end of the <br> tag, so typing may continue without interuption.


What is the KeyAscii for Tab?

How can I program the four directionnal arrow without holding the "shift" key


Ok I have it set that only numbers will work in the text box using the code

If keyascii < 48 or keyascii > 57 then
keyascii = 0
End if

That works fine, however I want them to be able to use the "Backspace", how can I do that while still having this code or a code that prevents All those keyascii's listed, except the backspace.


I have a Key event, which works, but I don't
understand why!

Would anyboy be able to tell me what the numbers 8,
46, and 48 to 57 represent?

Basically, the code disables the alphabetic characters
on a keyboard.

Thank you in anticipation.

Option Explicit

Function OnlyNumericKeys(KeyAscii As Integer) As

Select Case KeyAscii
Case 8, 46, 48 To 57 ' allow backspace, '.'
and digits
Case Else: KeyAscii = 0 ' reject everything
End Select
OnlyNumericKeys = KeyAscii

End Function

Private Sub Text1_KeyPress(KeyAscii As Integer)
KeyAscii = OnlyNumericKeys(KeyAscii)
End Sub

hello people

can anyone send me a copy of all ascii.


Hey there, what i want to do is have a text field that can only accept
numbers, full stops and the backspace key.
I currently have the below code to accept only numbers, but no full stops or
Im sure this is really simple, and i will be greatful for a respons..

Private Sub txtBal_KeyPress(KeyAscii As Integer)
If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then
KeyAscii = 0
MsgBox "Please enter only numbers"
End If

End Sub


Keyascii Help
Can someone please help. I'm trying to, instead having the user use the mouse to click the enter button, just hit the enter key, but don't know how to code it.
please help

hey im trying to write a program that when you hit any key on the keyboard, a form will popup....but im not too sure how to enter that into my code. can someone help.

this is what i have so far:

Private Sub Form_keypress(keyascii As Integer)
    If keyascii = __________ Then
        frmLogin.Visible = True

    End If
End Sub


Hello.If somebody know's the KeyAscii for PageUp and PageDown please mail me to:, thanks.

Please Help With This Keyascii
This is the code that creates a file that makes the output look like this in HexViewer:
000001Michael Jackson.000001.123456.445112
The above line has a certain records...Serial number,name,id number...all assumptions...and in the same file the second record is like this
000002Bill Clinton.001201.5343432.456789 and in the third record will have the first field as 000003.
What I want is that the first record will have
000001Michael Jacson.000001.123456.445112 and then I don't want this number to increment till the 4 the record so the 4th record should look like this:
000002Bill Clinton.....
and then the 8th Record will look like
000003William Gates.....
I want this number to increment after pressing enter 4 times, but am unable to do it, could anyone help....and in the last line I'm removing 0Ah by CHR$(13), I also wanna remove 0Dh, as the final output file will be viewed in HexViewer.

If KeyAscii = 13 Then
lMyNumber = CLng(sMyNumber)
lMyNumber = lMyNumber + 1
sMyNumber = Format(CStr(lMyNumber), "000000")
Print #1, sMyNumber & Trim(txtInput(0).Text); Chr$(13)
Print #1, Trim(txtInput(1).Text); Chr$(13);
Print #1, Trim(txtInput(2) + txtInput(3)); Chr$(13);
Print #1, Trim(txtInput(4).Text); Chr$(13);
Print #1, Trim(txtInput(2).Text) + Trim(txtInput(3))+ Trim(txtInput(4)) Chr$(13);

End If

when i enter the code in List1.Keypress

if keyascii = 68 then list1.removeitem (list1.listindex);

if the user hits D then the selected item is removed.
when I do it with delete, however:

if keyascii = 46 then list1.removeitem (list1.listindex)

when they press DELETE nothing happens! What is the code&gt;?

Keyascii For ,
Can somebody tell me the keyascii number for , ?


Enter Keyascii
i have been working on a piano on the computer because my sister ih have used beeps from "private Declare Function Beep Lib "kernel32" (ByVal dwFreq As Long, ByVal dwDuration As Long) As Long" and i have gott almost every thing and i was wandering if there was a keyascii code for enter tab and backspace

Problem With Keyascii
for a mud client i am creating i have set up some macros where they can set it so that a key on the number pad can have its own value(like press 2 and it sends the south command to the mud ect.ect.)any ways, once i start the program the numberpad does nothing, all of the triggers are set off by other keys on the keyboard(commented below) anyone have this problem before, no of a fix.....

Private Sub Text2_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case vbKeyNumpad0:
macro0 = GetSetting(App.Title, "settings", "macro0")
Winsock1.SendData macro0 & vbCrLf
Case vbKeyNumpad1:
macro = GetSetting(App.Title, "settings", "macro1")
Winsock1.SendData macro1 & vbCrLf
Case vbKeyNumpad2:'triggered when the letter b is pressed
macro2 = GetSetting(App.Title, "settings", "macro2")
Winsock1.SendData macro2 & vbCrLf
Case vbKeyNumpad3:
macro3 = GetSetting(App.Title, "settings", "macro3")
Winsock1.SendData macro3 & vbCrLf
Case vbKeyNumpad4:
macro4 = GetSetting(App.Title, "settings", "macro4")
Winsock1.SendData macro4 & vbCrLf
Case vbKeyNumpad5:'triggered when the letter e is pressed
macro5 = GetSetting(App.Title, "settings", "macro5")
Winsock1.SendData macro5 & vbCrLf
Case vbKeyNumpad6:
macro6 = GetSetting(App.Title, "settings", "macro6")
Winsock1.SendData macro6 & vbCrLf
Case vbKeyNumpad7:'triggered when the letter g is pressed
macro7 = GetSetting(App.Title, "settings", "macro7")
Winsock1.SendData macro7 & vbCrLf
Case vbKeyNumpad8:'triggered when the letter h is pressed
macro8 = GetSetting(App.Title, "settings", "macro8")
Winsock1.SendData macro8 & vbCrLf
Case vbKeyNumpad9:'triggered when the letter o is pressed
macro9 = GetSetting(App.Title, "settings", "macro9")
Winsock1.SendData macro9 & vbCrLf
End Select
End Sub

How To Return The KeyAscii?
I have a label that I coded to show the KeyAscii value of the keyboard button at the Form KeyPress event, as follows:


Private Sub Form_KeyPress(KeyAscii As Integer)
Label1.Caption = KeyAscii
End Sub

Now I need another label to return the KeyAscii value to the relevant keyboard button value or text. For example the (97) KeyAscii is the letter (a).
So how can I do this without using text box?


Calculator + Keyascii
hi having probs with the key ascii, i am trying to make the calculator so that wen i press the + button on the number pad to automaticly continue with the process of a calculator. At the moment if i click it or press it, it just adds + which i dont want


If keyascii = 43 then
End If

WORK? if soo where would i put this??


Public add As Double
Public num2 As Double
Public res As Double
Public operation As String

Private Sub cmd_Click()
add = txtdisp.Text
txtdisp.Text = add * add
End Sub

Private Sub cmd0_Click()
txtdisp = txtdisp & 0
End Sub

Private Sub cmd1_Click()
txtdisp = txtdisp & 1

End Sub

Private Sub cmd2_Click()
txtdisp = txtdisp & 2

End Sub

Private Sub cmd3_Click()
txtdisp = txtdisp & 3

End Sub

Private Sub cmd4_Click()
txtdisp = txtdisp & 4

End Sub

Private Sub cmd5_Click()
txtdisp = txtdisp & 5

End Sub

Private Sub cmd6_Click()
txtdisp = txtdisp & 6

End Sub

Private Sub cmd7_Click()
txtdisp = txtdisp & 7

End Sub

Private Sub cmd8_Click()
txtdisp = txtdisp & 8

End Sub

Private Sub cmd9_Click()
txtdisp = txtdisp & 9

End Sub

Private Sub cmdadd_Click()
add = txtdisp.Text
txtdisp.Text = ""
operation = "Plus"

End Sub

Private Sub cmddiv_Click()
operation = "divide"
add = txtdisp.Text
txtdisp.Text = ""
End Sub

Sub cmdeq_Click()
num2 = txtdisp.Text
txtdisp.Text = ""

If operation = "plus" Then
res = num2 + add
End If

If operation = "times" Then
res = add * num2
End If

If operation = "divide" Then
res = add / num2
End If

If operation = "minus" Then
res = add - num2
End If

txtdisp.Text = (res)

End Sub

Private Sub cmdmin_Click()
operation = "minus"
add = txtdisp.Text
txtdisp.Text = ""
End Sub

Private Sub cmdtim_Click()
operation = "times"
add = txtdisp.Text
txtdisp.Text = ""
End Sub

Private Sub txtdisp_KeyPress(KeyAscii As Integer)

If KeyAscii = 13 Then
End If

End Sub

Keyascii Problem

Public Sub validate_entry(KeyAscii)
Select Case KeyAscii
Case Is >= 97, Is <= 122, vbKeyA To vbKeyZ, vbKeyBack, vbKeySpace
'do nothing
Case Else
KeyAscii = 0
End Select
End Sub

my aim was to allow only lettters, backspace, space in a txtbox.

since lowercase a to z, do not have symbolic constants , i am using their ascii but in the above code, i can't have eg: case is >= 97 and <= 122
also, if i leaves it like above it is allowing numbers......

Can anyone find the solution as it is ?

KeyAscii Chart
I'm looking for the KeyAscii numbers for the up, down, left, and right arrow keys. Does anyone know what they are or where I can find a chart with a list of all of them?

Values For Keyascii
Isthere a list for all the keyascii values available?


Keycode And Keyascii
What is the major difference between the keyascii that is passed in keypress event and the keycode which is passed in keydown and keyup events??

Keyascii Values
does anyone know the key ascii values in Vb for the
- (negative sign) and . (decimal place)


Keyascii Converting
Ok folks I am making a form the will function as the passwrod enter screen. I am using this code

Private Sub txtpassword_KeyPress(Index As Integer, KeyAscii As Integer)
If len(txtpassword(index)) = 4 Then
txtpassword(index + 1).setfocus
End If
End Sub
This work sfine to change the text box when each of their lengths get to 4 chracters. The only problem is that I loose the character that is entered when the text box is changed. Say that teh password was 1234-5678-9123. When you type in 1234 it works fine, but when you type in 5 it automatically changes text box and looses that key input.

I was wondering if there was some way to convert the Keyascii back into a charcter, something along these lines:

Private Sub txtpassword_KeyPress(Index As Integer, KeyAscii As Integer)
If len(txtpassword(index)) = 4 Then
txtpassword(index + 1).setfocus
txtpassword(index + 1).text = 'converted keysacii key
End If
End Sub

List For Keyascii
Can show me the whole list of keyascii?

Cus I read this:

Private Sub cmdRecord_KeyPress(keyascii As Integer)
If keyascii = 70 Then MsgBox "bleh"
End Sub

Copyright 2005-08, All rights reserved