key entry trapping using On KeyDown (1 Viewer)

Zedster

Registered User.
Local time
Today, 17:25
Joined
Jul 2, 2019
Messages
168
I have a function to prevent users typing non numerics into field requiring pure numbers.

It uses the On KeyDown event to read the ASCII code for the key which has been pressed if the key entered has an ASCII code falling between 48 & 57 (corresponding to 0 through 9) then it is accepted, else it is rejected.

The problem is on one persons machine it will not accept the number 1 when it is pressed. On further investigation, when he presses the number 1 his machine returns ASCII 97 which should be letter "a". I don't need help on my code, but I have no idea what is going on with his machine and how I can get it to return ASCI 49 when he presses 1.

Any help appreciated.

Code:
10010    If (KeyCode >= 48 And KeyCode <= 57) Or KeyCode = 8 Or KeyCode = 9 Or KeyCode = 46 Then
        'do nothing
10030    Else
10040        strError = "The values entered must be pure numeric i.e characters 0 through to 9. The value you have entered has been cancelled"
10050    End If
 

CJ_London

Super Moderator
Staff member
Local time
Today, 17:25
Joined
Feb 19, 2013
Messages
16,553
don't think I have time to help right now - but so others have a bit more information. What character is displayed when the '1' is pressed (temporarily modify your code to accept keycode 97)
 

Zedster

Registered User.
Local time
Today, 17:25
Joined
Jul 2, 2019
Messages
168
I will write a little app that logs all codes and come back to you.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 17:25
Joined
Feb 19, 2013
Messages
16,553
Only thing I can suggest is the keyboard has been remapped in some way - so I would check the language and keyboard settings as well
 

Zedster

Registered User.
Local time
Today, 17:25
Joined
Jul 2, 2019
Messages
168
OK. I have now remapped all the main alpha-numeric key presses. What appears to be happening is when numbers are pressed on the numeric pad through "num lock", they give different and incorrect ASCII values, even though they themselves print fine on the screen!

I am confused!

Key typed = q; ASCII Code = 81 ; Converted Character = Q
Key typed = w; ASCII Code = 87 ; Converted Character = W
Key typed = e; ASCII Code = 69 ; Converted Character = E
Key typed = r; ASCII Code = 82 ; Converted Character = R
Key typed = t; ASCII Code = 84 ; Converted Character = T
Key typed = y; ASCII Code = 89 ; Converted Character = Y
Key typed = u; ASCII Code = 85 ; Converted Character = U
Key typed = i; ASCII Code = 73 ; Converted Character = I
Key typed = o; ASCII Code = 79 ; Converted Character = O
Key typed = p; ASCII Code = 80 ; Converted Character = P
Key typed = a; ASCII Code = 65 ; Converted Character = A
Key typed = s; ASCII Code = 83 ; Converted Character = S
Key typed = d; ASCII Code = 68 ; Converted Character = D
Key typed = f; ASCII Code = 70 ; Converted Character = F
Key typed = g; ASCII Code = 71 ; Converted Character = G
Key typed = h; ASCII Code = 72 ; Converted Character = H
Key typed = j; ASCII Code = 74 ; Converted Character = J
Key typed = k; ASCII Code = 75 ; Converted Character = K
Key typed = l; ASCII Code = 76 ; Converted Character = L
Key typed = z; ASCII Code = 90 ; Converted Character = Z
Key typed = x; ASCII Code = 88 ; Converted Character = X
Key typed = c; ASCII Code = 67 ; Converted Character = C
Key typed = v; ASCII Code = 86 ; Converted Character = V
Key typed = b; ASCII Code = 66 ; Converted Character = B
Key typed = n; ASCII Code = 78 ; Converted Character = N
Key typed = m; ASCII Code = 77 ; Converted Character = M
Key typed (on number pad) = 1; ASCII Code = 97 ; Converted Character = a
Key typed (on number pad) = 2; ASCII Code = 98 ; Converted Character = b
Key typed (on number pad) = 3; ASCII Code = 99 ; Converted Character = c
Key typed (on number pad) = 4; ASCII Code = 100 ; Converted Character = d
Key typed (on number pad) = 5; ASCII Code = 101 ; Converted Character = e
Key typed (on number pad) = 6; ASCII Code = 102 ; Converted Character = f
Key typed (on number pad) = 7; ASCII Code = 103 ; Converted Character = g
Key typed (on number pad) = 8; ASCII Code = 104 ; Converted Character = h
Key typed (on number pad) = 9; ASCII Code = 105 ; Converted Character = I

Key typed (on number pad) = 0; ASCII Code = 96 ; Converted Character = `
 

Micron

AWF VIP
Local time
Today, 13:25
Joined
Oct 20, 2018
Messages
3,476
Maybe just
Code:
Sub SomeControl_OnChange()
If Not IsNumeric(SomeControl) Then
  do something
End If
If it's unbound, then SomeControl.Text
If there are spaces between digits, then If Val(SomeControl) > 0
 
Last edited:

theDBguy

I’m here to help
Staff member
Local time
Today, 10:25
Joined
Oct 29, 2018
Messages
21,358
Hi. Pardon me for jumping in, but KeyCode and Ascii Code are two different things. In case it helps, take a look at the KeyCode values on a standard keyboard here. Cheers!

 

Zedster

Registered User.
Local time
Today, 17:25
Joined
Jul 2, 2019
Messages
168
I solved the problem by adding another Or clause as below:

Code:
If (KeyCode >= 48 And KeyCode <= 57) Or KeyCode = 8 Or KeyCode = 9 Or KeyCode = 46 Or (KeyCode >= 96 And KeyCode <= 105)Then
 

Users who are viewing this thread

Top Bottom