Keypress event problem

giedrius

Registered User.
Local time
Today, 19:05
Joined
Dec 14, 2003
Messages
80
I have three controls (say A, B and C) on my form, which receive focus one after another sequentially. I need to track Enter or Tab key presses for the field B. However when I put code into On_Keypress event for control B, it fires when the user hits Enter in field A. In order to catch Enter key press in B I have to write code for the On_Keypress event for the field C.

Is this normal way of A2K operation or am I missing something here?

giedrius
 
>>>>receive focus one after another sequentially<<<<<<

I'm having a guess at this one , if the sequence is moved from one control to the next then I would imagine that when you finished working in control "A" the code moves you to control "B" then I would guess this could be causing your problem.
 
Tony Hine the code moves you to control "B" [/QUOTE said:
Not the code moves, but rather a user is pressing Enter or Tab keys in each control to move to the next one. I need to get hold when the user hits Enter or ab while on control B.

giedrius
 
Wrong event

Try using the keydown event - that fires while the control still has focus and the key that was pressed is in the argument list just like keypress.
 
Hi giedrius,
Looking at this,

>However when I put code into On_Keypress event for control B, it fires >when the user hits Enter in field A.

Did you put the code at the Form's KeyPress Event? Sounds more like the Form's event to me.

giedrius said:
I have three controls (say A, B and C) on my form, which receive focus one after another sequentially. I need to track Enter or Tab key presses for the field B. However when I put code into On_Keypress event for control B, it fires when the user hits Enter in field A. In order to catch Enter key press in B I have to write code for the On_Keypress event for the field C.

Is this normal way of A2K operation or am I missing something here?

giedrius
 
giedrus,

If the objective is to escort the user sequentially through three controls,
I would use the "On Lost Focus" event. It's far less code, and can't be
circumvented by just using the mouse to move to another control.

Each event simply needs: Me.NextControl.SetFocus

hth,
Wayne
 
I just wanted to add to this. The tab key is a little trickier to capture, but the Enter key behavior can be captured just like any other key.

In the control's Property window:
Goto the tab called "Other"
Change "Enter Key Behavior" from the default value to "New Line in Field"

Once this happens, Access will take the enter key as text and when you press it, you can capture it in the KeyPress for control B (KeyAscii will be 13).
If you dont want to insert a new line in control B, simply set KeyAscii = 0 in the KeyPress event.

Furthermore, if you want the enter key to go to the next control, set the focus as the next control in the keypress event as WayneRyan suggested (Me.NextControl.SetFocus)
 
The other thing to mention is that you need to set the form's KeyPreview property to Yes as it is, by default, No.
 
Yes and I forgot to mention that the setting is only need for the KeyPress event. You can capture both the Enter and Tab keys in the KeyDown event handler.
 
Hm... replying to a post dating from 04-20-2004 originally, and the latest addition replies to 12-07-2006...

Anyway - from the original issue WEBOn was closest.

Keystrokes that causes the focus to leave the control, such as TAB and Enter (without the "Enter Key Behaviour"), will not be received by this controls keypressed event, but the next controls keypressed (as described by the OP). Solution, if one want to trap the TAB and Enter keys within the current control, follow WEBOns advice - use the KeyDown event.
 

Users who are viewing this thread

Back
Top Bottom