Keydown skips

  • Thread starter Thread starter Guest
  • Start date Start date
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.
 
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.
 
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.
 
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 -----
 
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)
 
Back
Top