473,388 Members | 1,524 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

KeyPress to prevent entry of certain characters

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
7 16589
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
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
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
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

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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Phil Amey | last post by:
I would like to prevent certain characters from being input to some form cells, e.g. ~ # ' and so on. I'm currently using this format to check the input data of cells - if...
4
by: phil_gg04 | last post by:
Dear Javascript Experts, I'm currently implementing Anyterm, a terminal emulator on a web page. It consists of an Apache module, some XmlHTTP and a bit of Javascript. The idea is to give you...
9
by: Jan Danielsson | last post by:
Hello, I'd like to encode the string that outputs: Hello World! to 'Hello\x0aWorld!', and the string that outputs: Hello\World!
2
by: MLH | last post by:
I have a form frmEditAdminData. Its AllowAdditions property is set to No. I thought this meant the form could not be opened in a manner that allowed the appending of new records to its source...
4
by: Jiri Fogl | last post by:
I'm trying to build a regular expression (POSIX extended). One part of it has to match any text not containing a slash except standalone two dots (..), but I can't find the way to say something...
4
by: jcsnippets.atspace.com | last post by:
Hi everyone, Recently I have posted a question regarding special characters in text files. I was trying to read the text file to process the text later on, but I was using the wrong encoding....
1
by: PW | last post by:
Hi, Is there anyway to format a text input box to prevent any punctuation marks from being entered? TIA, PW
8
by: Paul | last post by:
Hi, My VB is very rusty I'm afraid! What would be the most efficient way to remove the following ASCII characters from a string? à è ì ò ù À È Ì Ò Ù á é í ó ú ý Á É Í Ó Ú Ý â ê î ô û Â Ê Î Ô...
26
by: Brad | last post by:
I'm writing a function to remove certain characters from strings. For example, I often get strings with commas... they look like this: "12,384" I'd like to take that string, remove the comma...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.