specific character keypress help needed

E

efandango

I have some code that I want to action if i press a specific key in a text
box. The key I want to press is '/' (the one that shares its key with '?'.
How would I do this?

I tried this code:

Select Case KeyCode

Case vbKeyDivide
Me![txt_Run_point_Address].Visible = True
Me![Run_Point_Postcode].Visible = True

End Select

But it didn't work. It just let me type the '/' character into the text box.
 
A

Allen Browne

Suggestions:

1. What event?
a) Use the KeyDown event of the control if you want this to work in only one
control.
b) Use the KeyDown event of the form if you want this to work regardless of
what the active control is.
c) Use an AutoKeys macro if you want this to work regardless of what
form/report is active.

2. KeyPress will identify the slash; use KeyDown if you have a special
reason.

3. To destroy the keystroke, set KeyAscii (or keycode) to zero after taking
action:
KeyAscii = 0

4. You cannot hide a control if it has the focus.

5. You can hide a control (text box), but not a field in the form's
RecordSource that is not represented by a control on your form. (Not sure if
that's the difference between the one with the txt prefix and the one
without: txt_Run_point_Address and Run_Point_Postcode.)
 
G

Graham Mandeno

Hi Eric

vbKeyDivide is on the numeric keypad. The one on the main keyboard has a
code of 191, so to catch both you need:

Case vbKeyDivide, 191

BTW, you could have found this out by setting a breakpoint on Select Case
and examining the value of KeyCode.
 
E

efandango

Allen,

The event is: I have a combo text box that allows me to select an answer to
a question. This is a memory quiz. Sometimes I/user simply cannot think of
the answer and give up trying. So, I have a command button that if pressed
reveals the answer in another text box by un-hiding it; everything works just
nice. But... after an intensive session, it can get a bit tiring/sore
fingers, so rather than have the button only option, i wanted to offer a way
of just pressing the '/' key while in the answer response box which would
reveal the actual answer in the other hidded text box. I hope this makes
sense?, (I do wish Msoft would allow us to paste a screenshot or
something....)

anyway, I will try to get my head around your fullsome answer and let you
know how I got on.

thanks for responding Allen.

regards

Eric






Allen Browne said:
Suggestions:

1. What event?
a) Use the KeyDown event of the control if you want this to work in only one
control.
b) Use the KeyDown event of the form if you want this to work regardless of
what the active control is.
c) Use an AutoKeys macro if you want this to work regardless of what
form/report is active.

2. KeyPress will identify the slash; use KeyDown if you have a special
reason.

3. To destroy the keystroke, set KeyAscii (or keycode) to zero after taking
action:
KeyAscii = 0

4. You cannot hide a control if it has the focus.

5. You can hide a control (text box), but not a field in the form's
RecordSource that is not represented by a control on your form. (Not sure if
that's the difference between the one with the txt prefix and the one
without: txt_Run_point_Address and Run_Point_Postcode.)

--
Allen Browne - Microsoft MVP. Perth, Western Australia

Reply to group, rather than allenbrowne at mvps dot org.

efandango said:
I have some code that I want to action if i press a specific key in a text
box. The key I want to press is '/' (the one that shares its key with '?'.
How would I do this?

I tried this code:

Select Case KeyCode

Case vbKeyDivide
Me![txt_Run_point_Address].Visible = True
Me![Run_Point_Postcode].Visible = True

End Select

But it didn't work. It just let me type the '/' character into the text
box.
 
E

efandango

Graham,

I did wonder about that... bu couldn't find a seperate reference to the
keyboard '/' key, which is actually the one I want because it also has the
'?' key and users will not think to hit shift (and why should they I guess)

thanks for your advice on error trapping. That kind of thing doesn't come
instinctively and as I'm not a pro-coder, etc I don't get much practice with
that area of coding.

thanks

regards

Eric


Graham Mandeno said:
Hi Eric

vbKeyDivide is on the numeric keypad. The one on the main keyboard has a
code of 191, so to catch both you need:

Case vbKeyDivide, 191

BTW, you could have found this out by setting a breakpoint on Select Case
and examining the value of KeyCode.
--
Good Luck :)

Graham Mandeno [Access MVP]
Auckland, New Zealand

efandango said:
I have some code that I want to action if i press a specific key in a text
box. The key I want to press is '/' (the one that shares its key with '?'.
How would I do this?

I tried this code:

Select Case KeyCode

Case vbKeyDivide
Me![txt_Run_point_Address].Visible = True
Me![Run_Point_Postcode].Visible = True

End Select

But it didn't work. It just let me type the '/' character into the text
box.
 
E

efandango

Allen,

that worked, but.... now if I keep my finger on the '/' key for more than a
jiffy, it repeats a series of /////'s and if I hit it very quickly, it still
inputs and retains the single '/' character. So, how can I have it so that
the '/' character is never actually input, but still actions the procedure,
and how can I get it to stop repeating?

here is my code so far:

Key Down

Select Case KeyCode
Case vbKeyDivide, 191
' key = /
Me![txt_Run_point_Address].Visible = True
Me![Run_Point_Postcode].Visible = True
KeyAscii = 0
End Select

Key Up

Me![txt_Run_point_Address].Visible = False
Me![Run_Point_Postcode].Visible = False


regards

Eric



Is there a way of stopping

Allen Browne said:
Suggestions:

1. What event?
a) Use the KeyDown event of the control if you want this to work in only one
control.
b) Use the KeyDown event of the form if you want this to work regardless of
what the active control is.
c) Use an AutoKeys macro if you want this to work regardless of what
form/report is active.

2. KeyPress will identify the slash; use KeyDown if you have a special
reason.

3. To destroy the keystroke, set KeyAscii (or keycode) to zero after taking
action:
KeyAscii = 0

4. You cannot hide a control if it has the focus.

5. You can hide a control (text box), but not a field in the form's
RecordSource that is not represented by a control on your form. (Not sure if
that's the difference between the one with the txt prefix and the one
without: txt_Run_point_Address and Run_Point_Postcode.)

--
Allen Browne - Microsoft MVP. Perth, Western Australia

Reply to group, rather than allenbrowne at mvps dot org.

efandango said:
I have some code that I want to action if i press a specific key in a text
box. The key I want to press is '/' (the one that shares its key with '?'.
How would I do this?

I tried this code:

Select Case KeyCode

Case vbKeyDivide
Me![txt_Run_point_Address].Visible = True
Me![Run_Point_Postcode].Visible = True

End Select

But it didn't work. It just let me type the '/' character into the text
box.
 
E

efandango

Sorry Allen, I don't really follow?, I tried the following:

Key Down

Select Case KeyCode
Case vbKeyDivide, 191
Me![txt_Run_point_Address].Visible = True
Me![Run_Point_Postcode].Visible = True
KeyAscii = 0
End Select


Key Up:

Select Case KeyCode
Case vbKeyDivide, 191
Me![txt_Run_point_Address].Visible = False
Me![Run_Point_Postcode].Visible = False
KeyAscii = 0

End Select

This doesn't make the '/' stop being used or repeating, and I can't figure
out what other code to use on the Keyup event.

regards
Eric
 
S

Stuart McCall

efandango said:
Sorry Allen, I don't really follow?, I tried the following:

Key Down

Select Case KeyCode
Case vbKeyDivide, 191
Me![txt_Run_point_Address].Visible = True
Me![Run_Point_Postcode].Visible = True
KeyAscii = 0
End Select


Key Up:

Select Case KeyCode
Case vbKeyDivide, 191
Me![txt_Run_point_Address].Visible = False
Me![Run_Point_Postcode].Visible = False
KeyAscii = 0

End Select

This doesn't make the '/' stop being used or repeating, and I can't figure
out what other code to use on the Keyup event.

regards
Eric


Allen Browne said:
Try the KeyUp event.

Try using KeyCode = 0 rather than KeyAscii = 0, as KeyAscii only works in
the KeyPress event. For KeyDown and KeyUP, use KeyCode.
 
E

efandango

Stuart,

that seemed to work on the repeating key issue, but for some reason the
KeyUp command doesn't work, in so much as the following code no longer makes
the text box non-visible, until I move to a neww record?. Can it be made such
that the KeyDown button makes the textbox visible, and the KeyUp makes it
invisible, just like it did on the command button that I mentoned on my first
posting.

This is what I have so far:

KeyDown:

Select Case KeyCode
Case vbKeyDivide, 191
Me![txt_Run_point_Address].Visible = True
Me![Run_Point_Postcode].Visible = True
KeyCode = 0
End Select

KeyUp:

Me![txt_Run_point_Address].Visible = False
Me![Run_Point_Postcode].Visible = False



Stuart McCall said:
efandango said:
Sorry Allen, I don't really follow?, I tried the following:

Key Down

Select Case KeyCode
Case vbKeyDivide, 191
Me![txt_Run_point_Address].Visible = True
Me![Run_Point_Postcode].Visible = True
KeyAscii = 0
End Select


Key Up:

Select Case KeyCode
Case vbKeyDivide, 191
Me![txt_Run_point_Address].Visible = False
Me![Run_Point_Postcode].Visible = False
KeyAscii = 0

End Select

This doesn't make the '/' stop being used or repeating, and I can't figure
out what other code to use on the Keyup event.

regards
Eric


Allen Browne said:
Try the KeyUp event.

--
Allen Browne - Microsoft MVP. Perth, Western Australia

Reply to group, rather than allenbrowne at mvps dot org.

that worked, but.... now if I keep my finger on the '/' key for more
than
a
jiffy, it repeats a series of /////'s and if I hit it very quickly, it
still
inputs and retains the single '/' character. So, how can I have it so
that
the '/' character is never actually input, but still actions the
procedure,
and how can I get it to stop repeating?

Try using KeyCode = 0 rather than KeyAscii = 0, as KeyAscii only works in
the KeyPress event. For KeyDown and KeyUP, use KeyCode.
 
A

Allen Browne

I just tried this in an unbound text box (Text27), and it toggled the
Surname text box's visible property when you release the key:

Private Sub Text27_KeyUp(KeyCode As Integer, Shift As Integer)
If (KeyCode = 191) Or (KeyCode = 111) Then
Me.Surname.Visible = Not Me.Surname.Visible
KeyCode = 0
End If
End Sub

Private Sub Text27_KeyPress(KeyAscii As Integer)
If KeyAscii = 47 Then
KeyAscii = 0
End If
End Sub

Using KeyPress destroys the keystroke, and the KeyUp means it doesn't
repeat.
 
E

efandango

Allen,

I tried it, and it works as you describe; but the problem is that I don't
want to have to manually toggle it on/off; instead I want this sequence to
occur.

1. press the / key and hold it (makes the text visible)
2. digest the answer and eventually release the key (makes text box invisible)

that's it.


but with your current code:

1. I press the / key (reveals the text)
2. release the key (still reveals text)
3. press key again (hides text)

this requires two hits of the '/' to toggle on/off, when I really need the
toggle off to be automatic, by simply releasing the '/' key. can this be done?



Allen Browne said:
I just tried this in an unbound text box (Text27), and it toggled the
Surname text box's visible property when you release the key:

Private Sub Text27_KeyUp(KeyCode As Integer, Shift As Integer)
If (KeyCode = 191) Or (KeyCode = 111) Then
Me.Surname.Visible = Not Me.Surname.Visible
KeyCode = 0
End If
End Sub

Private Sub Text27_KeyPress(KeyAscii As Integer)
If KeyAscii = 47 Then
KeyAscii = 0
End If
End Sub

Using KeyPress destroys the keystroke, and the KeyUp means it doesn't
repeat.

--
Allen Browne - Microsoft MVP. Perth, Western Australia

Reply to group, rather than allenbrowne at mvps dot org.

efandango said:
Stuart,

that seemed to work on the repeating key issue, but for some reason the
KeyUp command doesn't work, in so much as the following code no longer
makes
the text box non-visible, until I move to a neww record?. Can it be made
such
that the KeyDown button makes the textbox visible, and the KeyUp makes it
invisible, just like it did on the command button that I mentoned on my
first
posting.

This is what I have so far:

KeyDown:

Select Case KeyCode
Case vbKeyDivide, 191
Me![txt_Run_point_Address].Visible = True
Me![Run_Point_Postcode].Visible = True
KeyCode = 0
End Select

KeyUp:

Me![txt_Run_point_Address].Visible = False
Me![Run_Point_Postcode].Visible = False
 
M

Marshall Barton

Try using the MouseDown event to make it visible and the
MouseUp event to hide it.
--
Marsh
MVP [MS Access]

Allen,

I tried it, and it works as you describe; but the problem is that I don't
want to have to manually toggle it on/off; instead I want this sequence to
occur.

1. press the / key and hold it (makes the text visible)
2. digest the answer and eventually release the key (makes text box invisible)

that's it.


but with your current code:

1. I press the / key (reveals the text)
2. release the key (still reveals text)
3. press key again (hides text)

this requires two hits of the '/' to toggle on/off, when I really need the
toggle off to be automatic, by simply releasing the '/' key. can this be done?



Allen Browne said:
I just tried this in an unbound text box (Text27), and it toggled the
Surname text box's visible property when you release the key:

Private Sub Text27_KeyUp(KeyCode As Integer, Shift As Integer)
If (KeyCode = 191) Or (KeyCode = 111) Then
Me.Surname.Visible = Not Me.Surname.Visible
KeyCode = 0
End If
End Sub

Private Sub Text27_KeyPress(KeyAscii As Integer)
If KeyAscii = 47 Then
KeyAscii = 0
End If
End Sub

Using KeyPress destroys the keystroke, and the KeyUp means it doesn't
repeat.

--
Allen Browne - Microsoft MVP. Perth, Western Australia

Reply to group, rather than allenbrowne at mvps dot org.

efandango said:
Stuart,

that seemed to work on the repeating key issue, but for some reason the
KeyUp command doesn't work, in so much as the following code no longer
makes
the text box non-visible, until I move to a neww record?. Can it be made
such
that the KeyDown button makes the textbox visible, and the KeyUp makes it
invisible, just like it did on the command button that I mentoned on my
first
posting.

This is what I have so far:

KeyDown:

Select Case KeyCode
Case vbKeyDivide, 191
Me![txt_Run_point_Address].Visible = True
Me![Run_Point_Postcode].Visible = True
KeyCode = 0
End Select

KeyUp:

Me![txt_Run_point_Address].Visible = False
Me![Run_Point_Postcode].Visible = False
 
E

efandango

Marsh,

I want to use the keys and not the mouse; it's easier on my fingers which
get quite sore after too much mouse clicking, which is why i wanted to change
from a command button to the '/' key.

regards

Eric


Marshall Barton said:
Try using the MouseDown event to make it visible and the
MouseUp event to hide it.
--
Marsh
MVP [MS Access]

Allen,

I tried it, and it works as you describe; but the problem is that I don't
want to have to manually toggle it on/off; instead I want this sequence to
occur.

1. press the / key and hold it (makes the text visible)
2. digest the answer and eventually release the key (makes text box invisible)

that's it.


but with your current code:

1. I press the / key (reveals the text)
2. release the key (still reveals text)
3. press key again (hides text)

this requires two hits of the '/' to toggle on/off, when I really need the
toggle off to be automatic, by simply releasing the '/' key. can this be done?



Allen Browne said:
I just tried this in an unbound text box (Text27), and it toggled the
Surname text box's visible property when you release the key:

Private Sub Text27_KeyUp(KeyCode As Integer, Shift As Integer)
If (KeyCode = 191) Or (KeyCode = 111) Then
Me.Surname.Visible = Not Me.Surname.Visible
KeyCode = 0
End If
End Sub

Private Sub Text27_KeyPress(KeyAscii As Integer)
If KeyAscii = 47 Then
KeyAscii = 0
End If
End Sub

Using KeyPress destroys the keystroke, and the KeyUp means it doesn't
repeat.

--
Allen Browne - Microsoft MVP. Perth, Western Australia

Reply to group, rather than allenbrowne at mvps dot org.

Stuart,

that seemed to work on the repeating key issue, but for some reason the
KeyUp command doesn't work, in so much as the following code no longer
makes
the text box non-visible, until I move to a neww record?. Can it be made
such
that the KeyDown button makes the textbox visible, and the KeyUp makes it
invisible, just like it did on the command button that I mentoned on my
first
posting.

This is what I have so far:

KeyDown:

Select Case KeyCode
Case vbKeyDivide, 191
Me![txt_Run_point_Address].Visible = True
Me![Run_Point_Postcode].Visible = True
KeyCode = 0
End Select

KeyUp:

Me![txt_Run_point_Address].Visible = False
Me![Run_Point_Postcode].Visible = False
 
A

Allen Browne

I'm sure you can take what I suggested, and adapt it so that the KeyPress
shows and the KeyUp hides the text box.

I'm not sure this is a great way to design an app in a multi-tasking
environment.

--
Allen Browne - Microsoft MVP. Perth, Western Australia

Reply to group, rather than allenbrowne at mvps dot org.

efandango said:
Allen,

I tried it, and it works as you describe; but the problem is that I don't
want to have to manually toggle it on/off; instead I want this sequence to
occur.

1. press the / key and hold it (makes the text visible)
2. digest the answer and eventually release the key (makes text box
invisible)

that's it.


but with your current code:

1. I press the / key (reveals the text)
2. release the key (still reveals text)
3. press key again (hides text)

this requires two hits of the '/' to toggle on/off, when I really need the
toggle off to be automatic, by simply releasing the '/' key. can this be
done?



Allen Browne said:
I just tried this in an unbound text box (Text27), and it toggled the
Surname text box's visible property when you release the key:

Private Sub Text27_KeyUp(KeyCode As Integer, Shift As Integer)
If (KeyCode = 191) Or (KeyCode = 111) Then
Me.Surname.Visible = Not Me.Surname.Visible
KeyCode = 0
End If
End Sub

Private Sub Text27_KeyPress(KeyAscii As Integer)
If KeyAscii = 47 Then
KeyAscii = 0
End If
End Sub

Using KeyPress destroys the keystroke, and the KeyUp means it doesn't
repeat.

efandango said:
Stuart,

that seemed to work on the repeating key issue, but for some reason the
KeyUp command doesn't work, in so much as the following code no longer
makes
the text box non-visible, until I move to a neww record?. Can it be
made
such
that the KeyDown button makes the textbox visible, and the KeyUp makes
it
invisible, just like it did on the command button that I mentoned on my
first
posting.

This is what I have so far:

KeyDown:

Select Case KeyCode
Case vbKeyDivide, 191
Me![txt_Run_point_Address].Visible = True
Me![Run_Point_Postcode].Visible = True
KeyCode = 0
End Select

KeyUp:

Me![txt_Run_point_Address].Visible = False
Me![Run_Point_Postcode].Visible = False
 
E

efandango

I'm sure you're correct, if only I knew how (I have tried all kinds of
variations on your code theme), or what was going wrong with this code, but
this:

Private Sub Combo_Answer_KeyUp(KeyCode As Integer, Shift As Integer)
If (KeyCode = 191) Or (KeyCode = 111) Then

Me.txt_Run_point_Address.Visible = Not Me.txt_Run_point_Address.Visible
Me.Run_Point_Postcode.Visible = Not Me.Run_Point_Postcode.Visible
KeyCode = 0
End If
End Sub


Together with this:

Private Sub Combo_Answer_KeyPress(KeyAscii As Integer)
If KeyAscii = 47 Then
KeyAscii = 0
End If

End Sub

will show the text box, but when I release the key, it will not hide it
until i press the key again.

However, If use this as well (when I have read the visible answer box and
want to move to the next question/record):

Private Sub Combo_Answer_LostFocus()
Me.txt_Run_point_Address.Visible = False
Me.Run_Point_Postcode.Visible = False
End Sub

Then the text box once again becomes invisible (without having to hit/toggle
the '/' key again)

It's not ideal, but I just can't figure out why something so simple as, If I
press this key, show the box, and when I release the key, just hide it.

I didn't really understand what you were getting at when you said "I'm not
sure this is a great way to design an app in a multi-tasking environment." I
am guessing that there is some relevenance to the 'multi-tasking
environment', but I don't really understand the context in which you say it?

regards

Eric




Allen Browne said:
I'm sure you can take what I suggested, and adapt it so that the KeyPress
shows and the KeyUp hides the text box.

I'm not sure this is a great way to design an app in a multi-tasking
environment.

--
Allen Browne - Microsoft MVP. Perth, Western Australia

Reply to group, rather than allenbrowne at mvps dot org.

efandango said:
Allen,

I tried it, and it works as you describe; but the problem is that I don't
want to have to manually toggle it on/off; instead I want this sequence to
occur.

1. press the / key and hold it (makes the text visible)
2. digest the answer and eventually release the key (makes text box
invisible)

that's it.


but with your current code:

1. I press the / key (reveals the text)
2. release the key (still reveals text)
3. press key again (hides text)

this requires two hits of the '/' to toggle on/off, when I really need the
toggle off to be automatic, by simply releasing the '/' key. can this be
done?



Allen Browne said:
I just tried this in an unbound text box (Text27), and it toggled the
Surname text box's visible property when you release the key:

Private Sub Text27_KeyUp(KeyCode As Integer, Shift As Integer)
If (KeyCode = 191) Or (KeyCode = 111) Then
Me.Surname.Visible = Not Me.Surname.Visible
KeyCode = 0
End If
End Sub

Private Sub Text27_KeyPress(KeyAscii As Integer)
If KeyAscii = 47 Then
KeyAscii = 0
End If
End Sub

Using KeyPress destroys the keystroke, and the KeyUp means it doesn't
repeat.

Stuart,

that seemed to work on the repeating key issue, but for some reason the
KeyUp command doesn't work, in so much as the following code no longer
makes
the text box non-visible, until I move to a neww record?. Can it be
made
such
that the KeyDown button makes the textbox visible, and the KeyUp makes
it
invisible, just like it did on the command button that I mentoned on my
first
posting.

This is what I have so far:

KeyDown:

Select Case KeyCode
Case vbKeyDivide, 191
Me![txt_Run_point_Address].Visible = True
Me![Run_Point_Postcode].Visible = True
KeyCode = 0
End Select

KeyUp:

Me![txt_Run_point_Address].Visible = False
Me![Run_Point_Postcode].Visible = False
 
A

AccessVandal via AccessMonster.com

Just using these two event will do fine. You don't need to alternate the
boolean value in each events. Remove other events when you try these.

Private Sub Combo_Answer_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode = 191 Or KeyCode = 111 Then
Me.txt_Run_point_Address.Visible = True
Me.Run_Point_Postcode.Visible = True
KeyCode = 0
End If
End Sub

Private Sub Combo_Answer_KeyPress(KeyAscii As Integer)
If KeyAscii = 47 Then
Me.txt_Run_point_Address.Visible = False
Me.Run_Point_Postcode.Visible = False
KeyAscii = 0
End If
End Sub
 
A

AccessVandal via AccessMonster.com

Edit:
'Hide the textbox
Private Sub Combo_Answer_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode = 191 Or KeyCode = 111 Then
Me.txt_Run_point_Address.Visible = False
Me.Run_Point_Postcode.Visible = False
KeyCode = 0
End If
End Sub

'Unhide the textbox if key is press
Private Sub Combo_Answer_KeyPress(KeyAscii As Integer)
If KeyAscii = 47 Then
Me.txt_Run_point_Address.Visible = True
Me.Run_Point_Postcode.Visible = True
KeyAscii = 0
End If
End Sub
 

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