KeyPress and Enter key

B

Brian

I have this code for the KeyPress event of all my combo boxes:

Private Sub <ControlName>_KeyPress(KeyAscii As Integer)
If Len(ActiveControl.Text) = 0 And KeyAscii <> 9 And KeyAscii <> 27 Then
ActiveControl.Dropdown 'dropdown on first character unless it is TAB/ESC
End Sub

Combined with LimitToList & AutoExpand both set to True, the net effect is
that the control drops down on the first keystroke, allowing greater
visibility of the list for the user.

Here is the problem: when I type an entry in the prior combo box (i.e . per
the tab order) and press Tab, it correctly moves the focus to this empty
control without dropping down; however, when I type an entry in the prior
combo box and press Enter, it moves to this control and drops down,
presumably due to my code above.

It seems that 1) the Enter keystroke is somehow being applied to the
following control, not the current control and 2) I cannot intercept the
Enter keystroke to prevent this behavior.

Ideas? (I mean, besides telling the users to stick to the Tab key or just
living with the box dropping down when they didn't intend that to happen).
 
U

UpRider

Try this:
Private Sub Combo11_Enter()
If IsNull(Combo11.Column(0)) Then
SendKeys "{ESC}"
End If
End Sub

UpRider
 
D

Dirk Goldgar

In
Brian said:
It seems that 1) the Enter keystroke is somehow being applied to the
following control, not the current control and 2) I cannot intercept
the Enter keystroke to prevent this behavior.

The Enter key works just like the Tab key in this regard: it is handed
to the next control to process. That is, when you press Enter or Tab in
combo A, that KeyPress event is handled by combo B. You can amend your
code like this:

'----- start of modified code model -----
Private Sub <ControlName>_KeyPress(KeyAscii As Integer)

If Len(ActiveControl.Text) = 0 Then
Select Case KeyAscii
Case 9, 13, 27
' ignore TAB, ENTER, ESC
Case Else
ActiveControl.Dropdown
End Select
End If

End Sub

'----- end of modified code model -----
 
D

Dirk Goldgar

In
UpRider said:
Try this:
Private Sub Combo11_Enter()
If IsNull(Combo11.Column(0)) Then
SendKeys "{ESC}"
End If
End Sub

I don't think that's going to do what Brian wants. The Enter event
fires when the focus enters the control, not specifically when the Enter
key is pressed. I also recommend that you not use SendKeys unless
there's no other way around it. SendKeys sends the keys to whatever
window has the focus, and is not safe in a multithreaded environment
where any window may seize the focus at any time. Most of the time it
may work, but on rare occasions you'll be sending your keystrokes to the
wrong window.
 
B

Brian

That did the trick. Thank you, Dirk.

Dirk Goldgar said:
In

The Enter key works just like the Tab key in this regard: it is handed
to the next control to process. That is, when you press Enter or Tab in
combo A, that KeyPress event is handled by combo B. You can amend your
code like this:

'----- start of modified code model -----
Private Sub <ControlName>_KeyPress(KeyAscii As Integer)

If Len(ActiveControl.Text) = 0 Then
Select Case KeyAscii
Case 9, 13, 27
' ignore TAB, ENTER, ESC
Case Else
ActiveControl.Dropdown
End Select
End If

End Sub

'----- end of modified code model -----


--
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