By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,701 Members | 1,928 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,701 IT Pros & Developers. It's quick & easy.

KeyPress to prevent entry of certain characters

P: n/a
Hello --

In a form text field for entering stock symbols, I want to allow only:
alpha
integer
decimal
backspace
left and right arrows
delete
shift

The code below does not prevent
apostrophe
"%"
"-" (subtract).

Can someone tell me what to add to the code to prevent these 3 symbols?

Private Sub txtSym_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 48 To 57 'integer
Case 65 To 90, 97 To 122 'alpha
Case vbKeyDecimal
Case vbKeyBack
Case vbKeyRight
Case vbKeyLeft
Case vbKeyInsert
Case vbKeyDelete
Case vbKeyShift
Case Else
KeyAscii = 0
Exit Sub
End Select
End Sub

Thanks for any help.

L Mehl

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.525 / Virus Database: 322 - Release Date: 10/10/2003
Nov 12 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
As a general rule use the KeyPress event to control printable keyboard
characters and the KeyUp/KeyDown to control non-printable keyboard
characters.

So the code you are loking for is something like

Private Sub txtSym_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 48 To 57 'integer
Case 65 To 90, 97 To 122 'alpha
Case vbKeyDecimal
Case vbKeyBack
Case Else
KeyAscii = 0
Exit Sub
End Select
End Sub
Terry
"L Mehl" <me*********@cyvest.com> wrote in message
news:8n*****************@newsread4.news.pas.earthl ink.net...
Hello --

In a form text field for entering stock symbols, I want to allow only:
alpha
integer
decimal
backspace
left and right arrows
delete
shift

The code below does not prevent
apostrophe
"%"
"-" (subtract).

Can someone tell me what to add to the code to prevent these 3 symbols?

Private Sub txtSym_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 48 To 57 'integer
Case 65 To 90, 97 To 122 'alpha
Case vbKeyDecimal
Case vbKeyBack
Case vbKeyRight
Case vbKeyLeft
Case vbKeyInsert
Case vbKeyDelete
Case vbKeyShift
Case Else
KeyAscii = 0
Exit Sub
End Select
End Sub

Thanks for any help.

L Mehl

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.525 / Virus Database: 322 - Release Date: 10/10/2003

Nov 12 '05 #2

P: n/a
Terry --

(using Access 2000)

Thank you for explaining the difference.

Your suggestion works. I don't understand why vbKeyBack (non-printable) is
included in this group, but, it works, so I will leave that question alone.

The only challenge that remains is to prevent screen blinking or anything
from displaying if F1-F12 keys are pressed.

The following code does not accomplish that. Pressing F1, I see the
Microsoft Access Help bar (only the bar) appear and disappear quickly, and
prefer not to see even that.

Private Sub txtSym_KeyDown(KeyCode As Integer, Shift As Integer)
'disallow function keys
Dim intF1 As Integer
intF1 = (vbKeyF1 And acShiftMask) > 0
If intF1 Then
'MsgBox "You pressed the F1 key."
Exit Sub
End If
End Sub

Putting the same code on
Private Sub txtSym_KeyUp(KeyCode As Integer, Shift As Integer)
or
Private Sub txtSym_BeforeUpdate(Cancel As Integer)
or
Private Sub txtSym_AfterUpdate()
or
Private Sub txtSym_Change()
has the same result.

Can you help me get on the right track?

Thanks for any additional help.

Larry

"Terry Kreft" <te*********@mps.co.uk> wrote in message
news:bm**********@newsreaderm1.core.theplanet.net. ..
As a general rule use the KeyPress event to control printable keyboard
characters and the KeyUp/KeyDown to control non-printable keyboard
characters.

So the code you are loking for is something like

Private Sub txtSym_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 48 To 57 'integer
Case 65 To 90, 97 To 122 'alpha
Case vbKeyDecimal
Case vbKeyBack
Case Else
KeyAscii = 0
Exit Sub
End Select
End Sub
Terry
"L Mehl" <me*********@cyvest.com> wrote in message
news:8n*****************@newsread4.news.pas.earthl ink.net...
Hello --

In a form text field for entering stock symbols, I want to allow only:
alpha
integer
decimal
backspace
left and right arrows
delete
shift

The code below does not prevent
apostrophe
"%"
"-" (subtract).

Can someone tell me what to add to the code to prevent these 3 symbols?

Private Sub txtSym_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 48 To 57 'integer
Case 65 To 90, 97 To 122 'alpha
Case vbKeyDecimal
Case vbKeyBack
Case vbKeyRight
Case vbKeyLeft
Case vbKeyInsert
Case vbKeyDelete
Case vbKeyShift
Case Else
KeyAscii = 0
Exit Sub
End Select
End Sub

Thanks for any help.

L Mehl

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.525 / Virus Database: 322 - Release Date: 10/10/2003


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.525 / Virus Database: 322 - Release Date: 10/9/2003
Nov 12 '05 #3

P: n/a
Terry --

I realized my logic was wrong in
intF1 = (vbKeyF1 And acShiftMask) > 0
If intF1 Then ....

I think the If ... Then
in
Private Sub txtSym_KeyDown(KeyCode As Integer, Shift As Integer)
should be

If KeyCode >= 112 And KeyCode <= 128 Then
MsgBox "You pressed a function key."
Me.txtSym.SetFocus
Exit Sub
End If

That traps the keycode, but the brief flicker of the Menu bar still occurs.
Can you help me eliminate the flicker?

Thanks,

Larry

"L Mehl" <me*********@cyvest.com> wrote in message
news:xR****************@newsread4.news.pas.earthli nk.net... Terry --

(using Access 2000)

Thank you for explaining the difference.

Your suggestion works. I don't understand why vbKeyBack (non-printable) is included in this group, but, it works, so I will leave that question alone.
The only challenge that remains is to prevent screen blinking or anything
from displaying if F1-F12 keys are pressed.

The following code does not accomplish that. Pressing F1, I see the
Microsoft Access Help bar (only the bar) appear and disappear quickly, and
prefer not to see even that.

Private Sub txtSym_KeyDown(KeyCode As Integer, Shift As Integer)
'disallow function keys
Dim intF1 As Integer
intF1 = (vbKeyF1 And acShiftMask) > 0
If intF1 Then
'MsgBox "You pressed the F1 key."
Exit Sub
End If
End Sub

Putting the same code on
Private Sub txtSym_KeyUp(KeyCode As Integer, Shift As Integer)
or
Private Sub txtSym_BeforeUpdate(Cancel As Integer)
or
Private Sub txtSym_AfterUpdate()
or
Private Sub txtSym_Change()
has the same result.

Can you help me get on the right track?

Thanks for any additional help.

Larry

"Terry Kreft" <te*********@mps.co.uk> wrote in message
news:bm**********@newsreaderm1.core.theplanet.net. ..
As a general rule use the KeyPress event to control printable keyboard
characters and the KeyUp/KeyDown to control non-printable keyboard
characters.

So the code you are loking for is something like

Private Sub txtSym_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 48 To 57 'integer
Case 65 To 90, 97 To 122 'alpha
Case vbKeyDecimal
Case vbKeyBack
Case Else
KeyAscii = 0
Exit Sub
End Select
End Sub
Terry
"L Mehl" <me*********@cyvest.com> wrote in message
news:8n*****************@newsread4.news.pas.earthl ink.net...
Hello --

In a form text field for entering stock symbols, I want to allow only:
alpha
integer
decimal
backspace
left and right arrows
delete
shift

The code below does not prevent
apostrophe
"%"
"-" (subtract).

Can someone tell me what to add to the code to prevent these 3 symbols?
Private Sub txtSym_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 48 To 57 'integer
Case 65 To 90, 97 To 122 'alpha
Case vbKeyDecimal
Case vbKeyBack
Case vbKeyRight
Case vbKeyLeft
Case vbKeyInsert
Case vbKeyDelete
Case vbKeyShift
Case Else
KeyAscii = 0
Exit Sub
End Select
End Sub

Thanks for any help.

L Mehl

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.525 / Virus Database: 322 - Release Date: 10/10/2003


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.525 / Virus Database: 322 - Release Date: 10/9/2003

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.525 / Virus Database: 322 - Release Date: 10/9/2003
Nov 12 '05 #4

P: n/a
Well

Private Sub txtSym_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode >= 112 And KeyCode <= 128 Then
Me.txtSym.SetFocus
KeyCode = 0
Exit Sub
End If
End Sub
should work
Terry

"L Mehl" <me*********@cyvest.com> wrote in message
news:62*****************@newsread4.news.pas.earthl ink.net...
Terry --

I realized my logic was wrong in
intF1 = (vbKeyF1 And acShiftMask) > 0
If intF1 Then ...

I think the If ... Then
in
Private Sub txtSym_KeyDown(KeyCode As Integer, Shift As Integer)
should be

If KeyCode >= 112 And KeyCode <= 128 Then
MsgBox "You pressed a function key."
Me.txtSym.SetFocus
Exit Sub
End If

That traps the keycode, but the brief flicker of the Menu bar still

occurs. Can you help me eliminate the flicker?

Thanks,

Larry

"L Mehl" <me*********@cyvest.com> wrote in message
news:xR****************@newsread4.news.pas.earthli nk.net...
Terry --

(using Access 2000)

Thank you for explaining the difference.

Your suggestion works. I don't understand why vbKeyBack (non-printable)

is
included in this group, but, it works, so I will leave that question

alone.

The only challenge that remains is to prevent screen blinking or anything
from displaying if F1-F12 keys are pressed.

The following code does not accomplish that. Pressing F1, I see the
Microsoft Access Help bar (only the bar) appear and disappear quickly, and prefer not to see even that.

Private Sub txtSym_KeyDown(KeyCode As Integer, Shift As Integer)
'disallow function keys
Dim intF1 As Integer
intF1 = (vbKeyF1 And acShiftMask) > 0
If intF1 Then
'MsgBox "You pressed the F1 key."
Exit Sub
End If
End Sub

Putting the same code on
Private Sub txtSym_KeyUp(KeyCode As Integer, Shift As Integer)
or
Private Sub txtSym_BeforeUpdate(Cancel As Integer)
or
Private Sub txtSym_AfterUpdate()
or
Private Sub txtSym_Change()
has the same result.

Can you help me get on the right track?

Thanks for any additional help.

Larry

"Terry Kreft" <te*********@mps.co.uk> wrote in message
news:bm**********@newsreaderm1.core.theplanet.net. ..
As a general rule use the KeyPress event to control printable keyboard
characters and the KeyUp/KeyDown to control non-printable keyboard
characters.

So the code you are loking for is something like

Private Sub txtSym_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 48 To 57 'integer
Case 65 To 90, 97 To 122 'alpha
Case vbKeyDecimal
Case vbKeyBack
Case Else
KeyAscii = 0
Exit Sub
End Select
End Sub
Terry
"L Mehl" <me*********@cyvest.com> wrote in message
news:8n*****************@newsread4.news.pas.earthl ink.net...
> Hello --
>
> In a form text field for entering stock symbols, I want to allow only: > alpha
> integer
> decimal
> backspace
> left and right arrows
> delete
> shift
>
> The code below does not prevent
> apostrophe
> "%"
> "-" (subtract).
>
> Can someone tell me what to add to the code to prevent these 3

symbols? >
> Private Sub txtSym_KeyPress(KeyAscii As Integer)
> Select Case KeyAscii
> Case 48 To 57 'integer
> Case 65 To 90, 97 To 122 'alpha
> Case vbKeyDecimal
> Case vbKeyBack
> Case vbKeyRight
> Case vbKeyLeft
> Case vbKeyInsert
> Case vbKeyDelete
> Case vbKeyShift
> Case Else
> KeyAscii = 0
> Exit Sub
> End Select
> End Sub
>
> Thanks for any help.
>
> L Mehl
>
>
>
> ---
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.525 / Virus Database: 322 - Release Date: 10/10/2003
>
>

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.525 / Virus Database: 322 - Release Date: 10/9/2003

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.525 / Virus Database: 322 - Release Date: 10/9/2003

Nov 12 '05 #5

P: n/a

Which is why I said "As a general rule ... "

You could argue that vbKeyBack affects what is shown because it backspaces
over characters but then that would imply that you should trap for Delete in
the keypress event as well, but you don't; you trap for this in the keydown
event (if we didn't have anomalies it would be too easy).
Terry

"L Mehl" <me*********@cyvest.com> wrote in message
news:xR****************@newsread4.news.pas.earthli nk.net...
Terry --
<SNIP>
Your suggestion works. I don't understand why vbKeyBack (non-printable) is included in this group, but, it works, so I will leave that question alone.
<SNIP> ---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.525 / Virus Database: 322 - Release Date: 10/9/2003

Nov 12 '05 #6

P: n/a
Terry --

Thanks. I see that I was not doing anything to change the KeyCode.

Larry

"Terry Kreft" <te*********@mps.co.uk> wrote in message
news:bm**********@newsreaderm1.core.theplanet.net. ..
Well

Private Sub txtSym_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode >= 112 And KeyCode <= 128 Then
Me.txtSym.SetFocus
KeyCode = 0
Exit Sub
End If
End Sub
should work
Terry

"L Mehl" <me*********@cyvest.com> wrote in message
news:62*****************@newsread4.news.pas.earthl ink.net...
Terry --

I realized my logic was wrong in
intF1 = (vbKeyF1 And acShiftMask) > 0
If intF1 Then

...

I think the If ... Then
in
Private Sub txtSym_KeyDown(KeyCode As Integer, Shift As Integer)
should be

If KeyCode >= 112 And KeyCode <= 128 Then
MsgBox "You pressed a function key."
Me.txtSym.SetFocus
Exit Sub
End If

That traps the keycode, but the brief flicker of the Menu bar still

occurs.
Can you help me eliminate the flicker?

Thanks,

Larry

"L Mehl" <me*********@cyvest.com> wrote in message
news:xR****************@newsread4.news.pas.earthli nk.net...
Terry --

(using Access 2000)

Thank you for explaining the difference.

Your suggestion works. I don't understand why vbKeyBack (non-printable)
is
included in this group, but, it works, so I will leave that question

alone.

The only challenge that remains is to prevent screen blinking or anything from displaying if F1-F12 keys are pressed.

The following code does not accomplish that. Pressing F1, I see the
Microsoft Access Help bar (only the bar) appear and disappear quickly, and prefer not to see even that.

Private Sub txtSym_KeyDown(KeyCode As Integer, Shift As Integer)
'disallow function keys
Dim intF1 As Integer
intF1 = (vbKeyF1 And acShiftMask) > 0
If intF1 Then
'MsgBox "You pressed the F1 key."
Exit Sub
End If
End Sub

Putting the same code on
Private Sub txtSym_KeyUp(KeyCode As Integer, Shift As Integer)
or
Private Sub txtSym_BeforeUpdate(Cancel As Integer)
or
Private Sub txtSym_AfterUpdate()
or
Private Sub txtSym_Change()
has the same result.

Can you help me get on the right track?

Thanks for any additional help.

Larry

"Terry Kreft" <te*********@mps.co.uk> wrote in message
news:bm**********@newsreaderm1.core.theplanet.net. ..
> As a general rule use the KeyPress event to control printable
keyboard > characters and the KeyUp/KeyDown to control non-printable keyboard
> characters.
>
> So the code you are loking for is something like
>
> Private Sub txtSym_KeyPress(KeyAscii As Integer)
> Select Case KeyAscii
> Case 48 To 57 'integer
> Case 65 To 90, 97 To 122 'alpha
> Case vbKeyDecimal
> Case vbKeyBack
> Case Else
> KeyAscii = 0
> Exit Sub
> End Select
> End Sub
>
>
> Terry
> "L Mehl" <me*********@cyvest.com> wrote in message
> news:8n*****************@newsread4.news.pas.earthl ink.net...
> > Hello --
> >
> > In a form text field for entering stock symbols, I want to allow

only: > > alpha
> > integer
> > decimal
> > backspace
> > left and right arrows
> > delete
> > shift
> >
> > The code below does not prevent
> > apostrophe
> > "%"
> > "-" (subtract).
> >
> > Can someone tell me what to add to the code to prevent these 3

symbols?
> >
> > Private Sub txtSym_KeyPress(KeyAscii As Integer)
> > Select Case KeyAscii
> > Case 48 To 57 'integer
> > Case 65 To 90, 97 To 122 'alpha
> > Case vbKeyDecimal
> > Case vbKeyBack
> > Case vbKeyRight
> > Case vbKeyLeft
> > Case vbKeyInsert
> > Case vbKeyDelete
> > Case vbKeyShift
> > Case Else
> > KeyAscii = 0
> > Exit Sub
> > End Select
> > End Sub
> >
> > Thanks for any help.
> >
> > L Mehl
> >
> >
> >
> > ---
> > Outgoing mail is certified Virus Free.
> > Checked by AVG anti-virus system (http://www.grisoft.com).
> > Version: 6.0.525 / Virus Database: 322 - Release Date: 10/10/2003
> >
> >
>
>
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.525 / Virus Database: 322 - Release Date: 10/9/2003

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.525 / Virus Database: 322 - Release Date: 10/9/2003


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.525 / Virus Database: 322 - Release Date: 10/9/2003
Nov 12 '05 #7

P: n/a
Terry --

I should have read the "fine print ... As a general rule ..." !

Thank you again for the help.

Larry

"Terry Kreft" <te*********@mps.co.uk> wrote in message
news:bm**********@newsreaderm1.core.theplanet.net. ..

Which is why I said "As a general rule ... "

You could argue that vbKeyBack affects what is shown because it backspaces
over characters but then that would imply that you should trap for Delete in the keypress event as well, but you don't; you trap for this in the keydown event (if we didn't have anomalies it would be too easy).
Terry

"L Mehl" <me*********@cyvest.com> wrote in message
news:xR****************@newsread4.news.pas.earthli nk.net...
Terry --

<SNIP>

Your suggestion works. I don't understand why vbKeyBack (non-printable)

is
included in this group, but, it works, so I will leave that question

alone.
<SNIP>
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.525 / Virus Database: 322 - Release Date: 10/9/2003


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.525 / Virus Database: 322 - Release Date: 10/9/2003
Nov 12 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.