Limit Keys

G

Gazza

Can someone tell me if it is possible to limit the keys that can be entered
into a textbox.

By this i mean i have a textbox at the end of a form and i want the user to
only be able to enter either Y, N or d.

At the moment i can enter anything into the textbox and it will save the
record but i only want the record to be added if the Y key is pressed. The N
and D keys are for other functions and they work to but i can`t figure out
how to stop the record from being added if i press say the K key or if i just
hit the enter key with nothing in the textbox atall.i have tried the code in
the afterupdate, beforeupdate, onkeydown, onkeyup, on exit and lost focus
events all with different results (and problems).

Any help will be much appreciated

Thanks
Gareth
 
G

Graham R Seach

Gazza,

If you want to limit the users' input to just three possible values, then
use either a Combo Box, or an Option Group.

To use a Combo Box, add one to the form, set its [RowSource Type] property
to 'Value List' and add the following to the its [RowSource] property:
"Y";"N";"d"
Then set the control's [Limit To List] property to True.

To use an Option Group, use the wizard to put the group onto the form, and
add three labels, Y, N and d. Select the default value, set the value for
each selection, set the control type to 'option button', give it a name, and
that's it. With this option, the bound table field has to be changed from
Text to Integer. The integer value then reflects the users' choice.

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
 
G

Gazza

Thanks for the reply Graham.

Although my first option was to put a combo box on the form, after speaking
to the people who use it they would prefer to just enter a letter and then
press the enter key to continue.

At the moment i have tried the following code in the before and afterupdate
event of the text box :

Dim response As Integer

Select Case Me.Select1

Case "y"
MsgBox "New record added", vbOKOnly
DoCmd.RunCommand acCmdSave
DoCmd.GoToRecord , , acNewRec
Me.Select1 = Null

Case "n"
response = MsgBox("Are you sure you want to delete this record", vbYesNo)

If response = vbYes Then
Cancel = True
DoCmd.RunCommand acCmdDeleteRecord
MsgBox "Record Deleted", vbOKOnly
Me.Select1 = Null

End If
If response = vbNo Then
Cancel = True
Me.Select1 = Null
Me.Accountcode.SetFocus
End If

Case "D"
DoCmd.OpenForm "FrmDeliveryAddress"

Case Else
MsgBox "Please enter Y or N only", vbOKOnly


End Select

but i cant get it to do what i want.

Thanks
Gareth
 
J

John W. Vinson

Although my first option was to put a combo box on the form, after speaking
to the people who use it they would prefer to just enter a letter and then
press the enter key to continue.

Well? You can tab into a combo box, type the first letter of any value in its
rowsource (the first displayed field), and hit enter or tab out to accept it.
You don't *need* to use the mouse or the dropdown arrow.
 
G

Graham R Seach

Gazza,

OK, it's up to you, but if a user said to me that they wanted to type a
specific letter into a textbox, I'd be asking why. From a usability
perspective (and at this point, its a purely academic exercise), it's not
terribly intuitive for a new user, whereas a combo or option group is. If it
were me, I'd stick to my guns and make them come up with a valid reason why
they wanted to enter a single character into a textbox in preference to any
other data data entry model. If they come up with a real good reason, then
fine, but if they say "because that's the way we've always done it", I'd say
phhfffttt - get over it - here's the new way. They *will* come to prefer it.

I recommend the option group.

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
 
G

Gazza

Personally i prefer to just type something into a text box and press the
enter key and if the users cant answer a simple Y or N question then they
shouldnt be allowed out.

Also when the person thats paying your wages tells you to do something
personally i do it although most people tend to want to do it there way.

Thanks
Gareth
 
G

Graham R Seach

Gareth,

If you get paid to do as your told, or if you really don't know any better,
then fine...do it. I truly mean no insult by that comment. But a
professional developer's job is to provide the customer with a quality
system based on *our* expertise. That's what we're paid exhorbitant amounts
of money for, and if we cave in and blindly do as we're told without
considering the impact of that decision, or better ways of doing things,
then we're not doing right by our customer. I call that a breach of ethics.

Customers are not (normally) qualified developers, and so do not (normally)
understand the intricasies of software design or human-computer interaction.
Of course, I'm not there, and I can't see the specifics of your application,
but when someone says they're doing something a particular way simply
because they're told to, then I have to question the wisdom and ethics of
what follows.

I know that highly regarded developers, some of whom frequent this very
group, will argue with me about this very point, but none of those arguments
invalidate my statements. You should also understand that I wouldn't be
doing right by you if I didn't offer you my considered opinion. Which way
you do it is entirely up to you.
You said nothing about asking, much less answering, a question. All you said
was "...they would prefer to just enter a letter...".

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
 
M

Mike Painter

Graham said:
Gareth,

If you get paid to do as your told, or if you really don't know any
better, then fine...do it. I truly mean no insult by that comment.
But a professional developer's job is to provide the customer with a
quality system based on *our* expertise. That's what we're paid
exhorbitant amounts of money for, and if we cave in and blindly do as
we're told without considering the impact of that decision, or better
ways of doing things, then we're not doing right by our customer. I
call that a breach of ethics.
Customers are not (normally) qualified developers, and so do not
(normally) understand the intricasies of software design or
human-computer interaction. Of course, I'm not there, and I can't see
the specifics of your application, but when someone says they're
doing something a particular way simply because they're told to, then
I have to question the wisdom and ethics of what follows.

Agreed. I was once fired from a job because I did *exactly* as the customer
demanded.
I had been ordered to by my boss and told to get everything in writing.
I had it in writing and the client signed off on it.
It was not what they wanted and I got the blame.
I lost a job.
 
G

Gazza

Ok sorry if i came across rude in my last post but it was not my intention
and i totally understand where you are coming from.

This database im creating is also a learning experience for me.

Basically what i want to do is to try and sort of recreate the database we
currently use (not access) and then make some changes (e.g. using a combo box
instead of a textbox as in my question, plus other changes)to make it better.

I know this sounds extremely stupid but i find i learn better by doing
something rather than reading about it so hopefully by doing this i will
learn a few tips and tricks along the way.Also if i know how they both work i
can then say to my boss that although he wants it this way it works easier
and better this way.

i have sort of got it working by adding the nullor zls statement to the
beforeupdate event and having a case else statement in the afterupdate event
but when i enter the record it gives me both msgbox`s.

so sorry for being a pain but is it possible.

Thanks
Gareth

P.S. unfortunately i always do as im told at work, it was just the way i was
brought up.
 
G

Graham R Seach

Hi Gareth,

Assuming you want uppercase 'Y' or 'N' and lowercase 'd', try this:

Private Sub Text0_Change()
Select Case LCase(Me.Text0.Text)
Case "", "y", "n"
Me.Text0.Text = UCase(Me.Text0.Text)
Case "d"
Me.Text0.Text = LCase(Me.Text0.Text)
Case Else
DoCmd.Beep
MsgBox "You can only enter 'Y', 'N', or 'd'."
Me.Text0 = Null
End Select
End Sub

If case doesn't matter, then this'll do it:

Private Sub Text0_Change()
Select Case LCase(Me.Text0.Text)
Case "", "y", "n", "d"
Case Else
DoCmd.Beep
MsgBox "You can only enter 'Y', 'N', or 'd'."
Me.Text0 = Null
End Select
End Sub

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
 

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