Keydown skips

G

Guest

Hi,

In the keydown event of a control (eg. Control A) there is code to set the
focus to the next control (eg. Control B) if the key that is pressed down is
either the Tab or Return key. What happens is that the focus is set to
Control B then automatically is set to the next control (eg. Control C).

Why does the focus seem to jump to Contol C instead of staying at Control B?
I don't have any code for Control B.

Thanks in advance.
 
D

Dirk Goldgar

juicegully said:
Hi,

In the keydown event of a control (eg. Control A) there is code to
set the focus to the next control (eg. Control B) if the key that is
pressed down is either the Tab or Return key. What happens is that
the focus is set to Control B then automatically is set to the next
control (eg. Control C).

Why does the focus seem to jump to Contol C instead of staying at
Control B? I don't have any code for Control B.

Thanks in advance.

Do you "eat" the keystroke that triggers the event, by setting the
KeyCode to 0? If you don't, you'll transfer the focus to Control B, and
then Control B will see the keypress and tab on over to Control C.
 
G

Guest

Hi Dirk,

I don't understand what you mean by "eat". Can you please clarify?

Here's my code:

Private Sub ControlA_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyReturn Or KeyCode = vbKeyTab Then
ControlB.SetFocus
End If
End Sub

After it executes this code, Control B gets the focus then Control C gets
the focus right away.

Thanks again.
 
D

Dirk Goldgar

juicegully said:
Hi Dirk,

I don't understand what you mean by "eat". Can you please clarify?

Here's my code:

Private Sub ControlA_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyReturn Or KeyCode = vbKeyTab Then
ControlB.SetFocus
End If
End Sub

After it executes this code, Control B gets the focus then Control C
gets the focus right away.

Revise it as follows:

'----- start of revised code -----
Private Sub ControlA_KeyDown(KeyCode As Integer, Shift As Integer)

If KeyCode = vbKeyReturn Or KeyCode = vbKeyTab Then

' "eat" this key, since we're going to respond to it here.
KeyCode = 0

ControlB.SetFocus

End If

End Sub
'----- end of revised code -----
 
G

Guest

Thank you!

Dirk Goldgar said:
Revise it as follows:

'----- start of revised code -----
Private Sub ControlA_KeyDown(KeyCode As Integer, Shift As Integer)

If KeyCode = vbKeyReturn Or KeyCode = vbKeyTab Then

' "eat" this key, since we're going to respond to it here.
KeyCode = 0

ControlB.SetFocus

End If

End Sub
'----- end of revised code -----

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 

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