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

How to use Enter key as command button?

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
> 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

P: n/a
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

P: n/a
> 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

P: n/a
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.