By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,574 Members | 3,021 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,574 IT Pros & Developers. It's quick & easy.

How to change Enter Key behavior

P: n/a
I have a textbox in a form into which users enter a string to execute a
search. After the user presses Enter, I'd like the focus to go back to the
textbox, but the default behavior of the Enter Key moves the focus to the
next control.

Here's the code:

Private Sub txtFind_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyReturn Then
Call cmdFind_Click
Me!txtFind.SetFocus ' this does not work
End If
End Sub

Any suggestions welcome.

Thanks in advance!
Nov 13 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
You say that you want the focus to "go back to the textbox". Would you
prefer it never leave the textbox? Should the Enter key do something to the
textbox? Have you looked at Tools|Options|Keyboard Tab|Move after Enter?

--
Wayne Morgan
MS Access MVP
"deko" <ww*******************************@nospam.com> wrote in message
news:QW******************@newssvr21.news.prodigy.c om...
I have a textbox in a form into which users enter a string to execute a
search. After the user presses Enter, I'd like the focus to go back to
the
textbox, but the default behavior of the Enter Key moves the focus to the
next control.

Here's the code:

Private Sub txtFind_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyReturn Then
Call cmdFind_Click
Me!txtFind.SetFocus ' this does not work
End If
End Sub

Any suggestions welcome.

Thanks in advance!

Nov 13 '05 #2

P: n/a
To change the enter key behavior for the whole database application, from
the Database Window, on the menu Tools | Options | Keyboard, and set the
appropriate action in "Move after Enter". (Note: these are the exact words
in Access 2002, but there may be minor differences in the wording between
Access' versions.)

To change the enter key behavior on a specific control on a form,
right-click, choose Properties, then select Other, and use the property
"Enter Key Behavior".

Larry Linson
Microsoft Access MVP
"deko" <ww*******************************@nospam.com> wrote in message
news:QW******************@newssvr21.news.prodigy.c om...
I have a textbox in a form into which users enter a string to execute a
search. After the user presses Enter, I'd like the focus to go back to the textbox, but the default behavior of the Enter Key moves the focus to the
next control.

Here's the code:

Private Sub txtFind_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyReturn Then
Call cmdFind_Click
Me!txtFind.SetFocus ' this does not work
End If
End Sub

Any suggestions welcome.

Thanks in advance!

Nov 13 '05 #3

P: n/a
Hi and thanks for the reply.
To change the enter key behavior for the whole database application, from
the Database Window, on the menu Tools | Options | Keyboard, and set the
appropriate action in "Move after Enter". (Note: these are the exact words
in Access 2002, but there may be minor differences in the wording between
Access' versions.)
If I make a change globally, my guess I'll be chasing all kinds of other
issues, so I was hoping to find a way to do it with code.
To change the enter key behavior on a specific control on a form,
right-click, choose Properties, then select Other, and use the property
"Enter Key Behavior".


I tried that, but the only other selection for that property is "New Line in
Field", which causes other problems.

The only other thing I can think of is adding some code to the control that
takes the focus after Enter is pressed - and have that code send the focus
back to the text box. But I may just have to make the global change...

Thanks again for the help.
Nov 13 '05 #4

P: n/a
Thanks for the reply.
You say that you want the focus to "go back to the textbox". Would you
prefer it never leave the textbox? Should the Enter key do something to the textbox? Have you looked at Tools|Options|Keyboard Tab|Move after Enter?


Yes, I know I can make the change globally by changing the "Move after
Enter" option. I was hoping to find a way to resolve this without a global
change (perhaps I should have mentioned that).
Nov 13 '05 #5

P: n/a
On Sat, 30 Oct 2004 21:44:16 GMT, "deko" <ww*******************************@nospam.com> wrote:
I have a textbox in a form into which users enter a string to execute a
search. After the user presses Enter, I'd like the focus to go back to the
textbox, but the default behavior of the Enter Key moves the focus to the
next control.

Here's the code:

Private Sub txtFind_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyReturn Then
Call cmdFind_Click
Me!txtFind.SetFocus ' this does not work
End If
End Sub

Any suggestions welcome.

Thanks in advance!


If you want the focus to go "back" to the text box, you first have to leave the text box.
Set the focus to another control and then bring it back.

Private Sub txtFind_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyReturn Then
Call cmdFind_Click
Me!SomeOtherControl.Setfocus
Me!txtFind.SetFocus ' this does not work
End If
End Sub
Wayne Gillespie
Gosford NSW Australia
Nov 13 '05 #6

P: n/a
Give this a try:

Private Sub Text0_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyReturn Then KeyCode = 0
End Sub

--
Wayne Morgan
MS Access MVP
"deko" <ww*******************************@nospam.com> wrote in message
news:nk******************@newssvr21.news.prodigy.c om...
Thanks for the reply.
You say that you want the focus to "go back to the textbox". Would you
prefer it never leave the textbox? Should the Enter key do something to

the
textbox? Have you looked at Tools|Options|Keyboard Tab|Move after Enter?


Yes, I know I can make the change globally by changing the "Move after
Enter" option. I was hoping to find a way to resolve this without a
global
change (perhaps I should have mentioned that).

Nov 13 '05 #7

P: n/a
> Give this a try:
Private Sub Text0_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyReturn Then KeyCode = 0
End Sub


Thanks - that seems to do the trick.

Here's the complete code:

Private Sub txtFind_KeyDown(KeyCode As Integer, Shift As Integer)
On Error GoTo HandleErr
Dim lngEid As Long
If KeyCode = vbKeyReturn Then
Application.Echo False
lngEid = Me.Entity_ID
Me.Requery 'this appears to be nec'y - otherwise Me!txtFind is
sometimes Null
If IsNull(Me!txtFind) Then GoTo Exit_Here
KeyCode = 0 'not sure if this is best place for this, but it seems
to work
lngCurEid = Me!Entity_ID
Call cmdFind_Click 'go to new entity
If lngCurEid = Me!Entity_ID Then Call GoToEid(lngEid) 'if cd not
find entity, go back to where user was (because Me!Requery has moved form to
first record in database)
End If
Me!txtFind.SetFocus
Exit_Here:
Application.Echo True
Exit Sub
HandleErr:
Select Case Err.Number
Case Else
modHandler.LogErr ("frm0"), ("txtFind_KeyDown")
End Select
Resume Exit_Here
End Sub

I need both "KeyCode = 0" and "Me!txtFind.SetFocus" because GoToEid has a
SetFocus statement - which is required when GoToEid is called from other
code in the project.
Nov 13 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.