Disallow the dash/minus key

J

JayTee

Hi all,
I would like to know how I stop users putting a dash between a number
I have a big database that requires users to enter a ID no such as 123896521
and some cannot help them selves and enter 123-563-985 etc. The filed is a
text field because sometimes it has a letter at the star.
Thanks to anyone that can help
 
D

Dennis

Don't know if this is the best way or right way to do it but I have used this
in the past and it seems OK. My field is called txtTest and I put this code
in the On Key Up event

Private Sub txtTest_KeyUp(KeyCode As Integer, Shift As Integer)
On Error Resume Next
If KeyCode = 189 Then
txtTest.Text = Left(txtTest.Text, Len(txtTest.Text) - 1)
End If
End Sub
 
S

Stuart McCall

JayTee said:
Hi all,
I would like to know how I stop users putting a dash between a number
I have a big database that requires users to enter a ID no such as
123896521 and some cannot help them selves and enter 123-563-985 etc. The
filed is a text field because sometimes it has a letter at the star.
Thanks to anyone that can help

Go and grab the function KeyFilter from:

http://www.smccall.demon.co.uk/Keyboard.htm#KeyFilter

Then it's simply a matter of pasting this:

KeyFilter KeyAscii, "-"

in the textbox's Keypress event, and the user will not be able to type a
hyphen, but instead will hear a beep. If you want to disallow certain other
characters, add them to the parameter string.
 
T

TeeSee

Go and grab the function KeyFilter from:

http://www.smccall.demon.co.uk/Keyboard.htm#KeyFilter

Then it's simply a matter of pasting this:

KeyFilter KeyAscii, "-"

in the textbox's Keypress event, and the user will not be able to type a
hyphen, but instead will hear a beep. If you want to disallow certain other
characters, add them to the parameter string.

Sorry (I'm a learner) but I don't get it! Believe me it's not he only
thing I don't get. I was intrigued with/by this wee function and I
tried to set it up on an unbound control in a form as suggested. (I
think). The part I don't get so far is where is the defined content of
string "Excludes". The string seems to me to be at the end of the line
that starts with the word "Syntax:".
Could someone please expound? Code is below .....

Public Function KeyFilter(KeyAscii As Integer, Excludes As String)
' Called from keypress event of a textbox
' Disallows all characters found in Excludes
'
'Syntax: KeyFilter KeyAscii, "!£$%^&*()\/|?'#~@;:[]{}-_=+"

Select Case KeyAscii
Case 8, 9, 13, 27 'Backspace, Tab, Enter, Esc
Exit Function
End Select
If InStr(1, Excludes, Chr$(KeyAscii)) Then
Beep
KeyAscii = 0
End If
End Function

I placed the (KeyFilter KeyAscii, "-" ) in the OnKeyPress event and I
get the error message (no number) ....
MOA cannot find the macro (KeyFilter KeyAscii, "-" )No brackets on
error message.
I have also tried removing the ('Syntax:) from the line above but I
still don't get it. Please help ... I love to learn!
 
S

Stuart McCall

Go and grab the function KeyFilter from:

http://www.smccall.demon.co.uk/Keyboard.htm#KeyFilter

Then it's simply a matter of pasting this:

KeyFilter KeyAscii, "-"

in the textbox's Keypress event, and the user will not be able to type a
hyphen, but instead will hear a beep. If you want to disallow certain
other
characters, add them to the parameter string.
Sorry (I'm a learner) but I don't get it! Believe me it's not he only
thing I don't get. I was intrigued with/by this wee function and I
tried to set it up on an unbound control in a form as suggested. (I
think). The part I don't get so far is where is the defined content of
string "Excludes". The string seems to me to be at the end of the line
that starts with the word "Syntax:".
Could someone please expound? Code is below .....
Public Function KeyFilter(KeyAscii As Integer, Excludes As String)
' Called from keypress event of a textbox
' Disallows all characters found in Excludes
'
'Syntax: KeyFilter KeyAscii, "!£$%^&*()\/|?'#~@;:[]{}-_=+"
Select Case KeyAscii
Case 8, 9, 13, 27 'Backspace, Tab, Enter, Esc
Exit Function
End Select
If InStr(1, Excludes, Chr$(KeyAscii)) Then
Beep
KeyAscii = 0
End If
End Function
I placed the (KeyFilter KeyAscii, "-" ) in the OnKeyPress event and I
get the error message (no number) ....
MOA cannot find the macro (KeyFilter KeyAscii, "-" )No brackets on
error message.
I have also tried removing the ('Syntax:) from the line above but I
still don't get it. Please help ... I love to learn!

Ok, first of all we need to resolve the 'cannot find the macro' problem. If
you wish to use this function from more than one form, it must reside in a
Standard Module. Switch to the VB editor (press Alt-F11) and, from the menu
bar, select Insert | Module. Put the text cursor at the top of the module,
underneath any 'Option' lines. Paste the function into the Module. From the
menu bar, select Debug | Compile. If all is ok, you won't receive an error
message.

Now, remove the function from wherever you put it.

Then open your form in design view and, from the menu bar, select View |
Code. Find your Keypress event for the control and make sure it says:

KeyFilter KeyAscii, "-"

The first parameter to the function (KeyAscii) is passed to the event
procedure by Access. It is passed 'by reference' which basically means it
works 'both ways', ie you get the keypress ascii code from Access and, if
you then say:

KeyAscii = 0

you have effectively 'thrown away' the keypress. This is what the function
does when the incoming keypress is contained in the 'Excludes' string (the
second parameter).

Hope that gets you going.
 
T

TeeSee

Go and grab the function KeyFilter from:

Then it's simply a matter of pasting this:
KeyFilter KeyAscii, "-"
in the textbox's Keypress event, and the user will not be able to type a
hyphen, but instead will hear a beep. If you want to disallow certain
other
characters, add them to the parameter string.
Sorry (I'm a learner) but I don't get it! Believe me it's not he only
thing I don't get. I was intrigued with/by this wee function and I
tried to set it up on an unbound control in a form as suggested. (I
think). The part I don't get so far is where is the defined content of
string "Excludes". The string seems to me to be at the end of the line
that starts with the word "Syntax:".
Could someone please expound? Code is below .....
Public Function KeyFilter(KeyAscii As Integer, Excludes As String)
' Called from keypress event of a textbox
' Disallows all characters found in Excludes
'
'Syntax: KeyFilter KeyAscii, "!£$%^&*()\/|?'#~@;:[]{}-_=+"
    Select Case KeyAscii
        Case 8, 9, 13, 27  'Backspace, Tab, Enter, Esc
            Exit Function
    End Select
    If InStr(1, Excludes, Chr$(KeyAscii)) Then
        Beep
        KeyAscii = 0
    End If
End Function
 I placed the (KeyFilter KeyAscii, "-" ) in the OnKeyPress event and I
get the error message (no number) ....
MOA cannot find the macro (KeyFilter KeyAscii, "-" )No brackets on
error message.
I have also tried removing the ('Syntax:) from the line above but I
still don't get it. Please help ... I love to learn!

Ok, first of all we need to resolve the 'cannot find the macro' problem. If
you wish to use this function from more than one form, it must reside in a
Standard Module. Switch to the VB editor (press Alt-F11) and, from the menu
bar, select Insert | Module. Put the text cursor at the top of the module,
underneath any 'Option' lines. Paste the function into the Module. From the
menu bar, select Debug | Compile. If all is ok, you won't receive an error
message.

Now, remove the function from wherever you put it.

Then open your form in design view and, from the menu bar, select View |
Code. Find your Keypress event for the control and make sure it says:

KeyFilter KeyAscii, "-"

The first parameter to the function (KeyAscii) is passed to the event
procedure by Access. It is passed 'by reference' which basically means it
works 'both ways', ie you get the keypress ascii code from Access and, if
you then say:

KeyAscii = 0

you have effectively 'thrown away' the keypress. This is what the function
does when the incoming keypress is contained in the 'Excludes' string (the
second parameter).

Hope that gets you going.- Hide quoted text -

- Show quoted text -

Stuart (a great Scottish name) Thanks for the explanation. I believe I
had the code in the correct "spot" but something wasn't quite correct.
Following your most recent instruction I have it functioning properly.
Sorry but I don't fully understand the logic. If you could, (i know it
will probably have to be a litle more verbose) try to help me
undersatnd what is actually going on.

Slange
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top