472,146 Members | 1,291 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,146 software developers and data experts.

How to use Enter key as command button?

I have a several forms that accept user input in a textbox and then take
some action based on that input after a command button is clicked. Is it
possible issue the command when the user presses Enter rather than clicking
the command button?

I've tried this, but for some reason it's not working:

Private Sub txtFind_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then Call cmdFind_Click
End Sub

Thanks in advance.
Nov 13 '05 #1
6 58573
Use the KeyDown event instead.

Private Sub txtFind_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyReturn Then cmdFind_Click
End Sub

BTW, the Call keyword is not required.

- Jim

On Sun, 30 May 2004 15:55:55 GMT, "deko" <no****@hotmail.com> wrote:
I have a several forms that accept user input in a textbox and then take
some action based on that input after a command button is clicked. Is it
possible issue the command when the user presses Enter rather than clicking
the command button?

I've tried this, but for some reason it's not working:

Private Sub txtFind_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then Call cmdFind_Click
End Sub

Thanks in advance.


Nov 13 '05 #2
Set the "Default" property of the command button to true, this will work if
no other button has the focus when return is pressed.

--
Michael Hopwood
"deko" <no****@hotmail.com> wrote in message
news:fu*******************@newssvr29.news.prodigy. com...
I have a several forms that accept user input in a textbox and then take
some action based on that input after a command button is clicked. Is it
possible issue the command when the user presses Enter rather than clicking the command button?

I've tried this, but for some reason it's not working:

Private Sub txtFind_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then Call cmdFind_Click
End Sub

Thanks in advance.

Nov 13 '05 #3
> Set the "Default" property of the command button to true, this will work
if
no other button has the focus when return is pressed.


That did the trick - thanks. The tab order was set so that the command
button was next (after the text box), which may have been the problem since
the button took the focus when the Enter key was pressed. I also set the
"Default" property of the command button to "Yes", but I don't know what
this does. What difference does it make when a command button's "Default"
property is set to "Yes"?
Nov 13 '05 #4
On Sun, 30 May 2004 16:52:52 GMT, "deko" <no****@hotmail.com> wrote:
Set the "Default" property of the command button to true, this will work

if
no other button has the focus when return is pressed.


That did the trick - thanks. The tab order was set so that the command
button was next (after the text box), which may have been the problem since
the button took the focus when the Enter key was pressed. I also set the
"Default" property of the command button to "Yes", but I don't know what
this does. What difference does it make when a command button's "Default"
property is set to "Yes"?

Keep in mind that the default command button will fire *whenever*
Enter is pressed (excluding other buttons). So if you press Enter in
another text box the default button fires. That's what setting it as
the default button does.

Did you try using KeyDown instead of KeyPress? That will give you what
you want without the side affects of using a default button.

- Jim
Nov 13 '05 #5
> Keep in mind that the default command button will fire *whenever*
Enter is pressed (excluding other buttons). So if you press Enter in
another text box the default button fires. That's what setting it as
the default button does.


I see. You are correct - the command button does fire whenever Enter is
pressed. I set the command button Default property back to "No" and
modified the code:

Private Sub txtFind_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 13 Then
DoCmd.RunCommand acCmdSaveRecord
Call cmdFind_Click
End If
End Sub

For some reason I have to save the record. The text box is unbound, so I'm
not sure why. If I just try to requery the text box like this:

Private Sub txtFind_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 13 Then
Me!txtFind.Requery
Call cmdFind_Click
End If
End Sub

I get an error:

Error Number 2118: You must save the current field before you run the
Requery action.

PS. I always use the Call keyword for readability, but yes, it is not
necessary.
Nov 13 '05 #6
On Sun, 30 May 2004 18:26:15 GMT, "deko" <no****@hotmail.com> wrote:

Private Sub txtFind_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 13 Then
DoCmd.RunCommand acCmdSaveRecord
Call cmdFind_Click
End If
End Sub

For some reason I have to save the record. The text box is unbound, so I'm
not sure why. If I just try to requery the text box like this:
I suspect that whatever is pushing the save is in another procedure.
What is the cmdFind_Click code doing? You might put a break in code
and see where that leads.
Private Sub txtFind_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 13 Then
Me!txtFind.Requery
Call cmdFind_Click
End If
End Sub

I get an error:
You don't want to do a requery of a control while it has the focus.
And, there is no need to with an unbound control.

Error Number 2118: You must save the current field before you run the
Requery action.

PS. I always use the Call keyword for readability, but yes, it is not
necessary.

Yeah, that's why I prefer to use named constants instead of numbers;
i.e., vbKeyReturn in place of 13

- Jim

Nov 13 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by pushrodengine via AccessMonster.com | last post: by
reply views Thread by Saiars | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.