Lost Focus/Last Key ENTER ?

D

Dave Ruhl

Sorry for the cross-post, but I didn't get an answer in
the Forms Programming forum...
==================================

Hello, I want to know how I can tell if the ENTER key was
hit to make a control lose focus (as opposed to TAB or a
mouse click). I've tried the OnKeyPress event, but it
doesn't get triggered by the ENTER key. I've also tried
the OnKeyDown event. It does catch the ENTER key, but
leaves my textbox with a NULL value, even if data appears
to be in it.

So, there doesn't seem to be a way to trap the ENTER key
without losing the data in my control. Can this be
done ? Thanks!
 
A

Albert D. Kallal

I've also tried
the OnKeyDown event. It does catch the ENTER key, but
leaves my textbox with a NULL value, even if data appears
to be in it.

The above is not my experience. Something is wrong, as you should be able
use the On Key Down event.

A few things:

You can change the focus by using the keydown event, but you need to
remember a few things:

The before update..and after update events still need to fire.

The Enter key, or the tab key is STILL GOING TO be processed. So, if you
move the focus some where else..then the Enter key will then fire..and try
to move. In other words..be careful..as the key you pressed is STILL going
to take action. So, if you move the focus with the key down ..then the enter
key still is firing. The solution usually is to send the keypress into a
black hole..and NOT let ms-access see it. Here some key down code that
should work:

Private Sub CutName_KeyDown(KeyCode As Integer, Shift As Integer)

Select Case KeyCode

Case vbKeyReturn
' if user hits return, lets move to customer field
KeyCode = 0 ' VERY imporant to kill the keypress!
Me.Customer.SetFocus

Case vbKeyTab
KeyCode = 0
Me.TakenBy.SetFocus
End If


End Sub


Of course...you don't copy the Sub name above. However, try killing the
keypress...as you MUST remember if you change the behaviour or set the
focus...access will STILL process the keypress. By using KeyCode = 0, then
access does NOT process the key.
 
D

Dave Ruhl

Thanks Albert, I will try that. I figured I must be
missing something. Thanks!
 

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