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

Trap for enter...

P: n/a
In a form I'm trying to make a search field to be able to quickly find a
record on ID-number. User enters a number and presses enter to move to that
record. How do I trap for the enter-keystroke and where should I put the
code?
I found this:
You can set the form's KeyPreview property to YES, then in the Form's
KeyPress event, add the following pseudo-code:
Select Case KeyAscii
Case 13
Select Case Screen.PreviousControl.Name
Case "cboMyCombo"
cmdMyButton_Click
End Select
End Select
but I wondered if there is a way to code this inside of the searchfield?
Also, if I have to use the above code where can I find the KeyPreview
property?
Thanks in advance,
john
Oct 21 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
john wrote:
In a form I'm trying to make a search field to be able to quickly
find a record on ID-number. User enters a number and presses enter to
move to that record. How do I trap for the enter-keystroke and where
should I put the code?
I found this:
You can set the form's KeyPreview property to YES, then in the Form's
KeyPress event, add the following pseudo-code:
Select Case KeyAscii
Case 13
Select Case Screen.PreviousControl.Name
Case "cboMyCombo"
cmdMyButton_Click
End Select
End Select
but I wondered if there is a way to code this inside of the
searchfield? Also, if I have to use the above code where can I find
the KeyPreview property?
Thanks in advance,
john
Put your code in the click event of a button and set the Default property of the
button to true. That will make the click event of the button run when you press
<Enter>. You can make the button transparent if you don't want it to be
visible.

--
Rick Brandt, Microsoft Access MVP
Email (as appropriate) to...
RBrandt at Hunter dot com
Oct 21 '06 #2

P: n/a
"Rick Brandt" <ri*********@hotmail.comschreef in bericht
news:JE*****************@newssvr29.news.prodigy.ne t...
john wrote:
>In a form I'm trying to make a search field to be able to quickly
find a record on ID-number. User enters a number and presses enter to
move to that record. How do I trap for the enter-keystroke and where
should I put the code?
I found this:
You can set the form's KeyPreview property to YES, then in the Form's
KeyPress event, add the following pseudo-code:
Select Case KeyAscii
Case 13
Select Case Screen.PreviousControl.Name
Case "cboMyCombo"
cmdMyButton_Click
End Select
End Select
but I wondered if there is a way to code this inside of the
searchfield? Also, if I have to use the above code where can I find
the KeyPreview property?
Thanks in advance,
john

Put your code in the click event of a button and set the Default property
of the button to true. That will make the click event of the button run
when you press <Enter>. You can make the button transparent if you don't
want it to be visible.
Thanks Rick. I partly got it working. The enter is 'recognized' but I have
to press enter twice to execute the button. With the first enter the button
gets focus (coming from the searchfield) and with the second enter the
button is actually pressed. Any idea to resolve that?
john
Oct 21 '06 #3

P: n/a
john wrote:
Thanks Rick. I partly got it working. The enter is 'recognized' but I
have to press enter twice to execute the button. With the first enter
the button gets focus (coming from the searchfield) and with the
second enter the button is actually pressed. Any idea to resolve that?
john
Then you still have something wrong. The <Enterkey will run ANY button when
that button has focus. The whole point of the Default property is to make a
button run from anywhere in the form by pressing <Enter>.

I'll run a few tests and post back.

--
Rick Brandt, Microsoft Access MVP
Email (as appropriate) to...
RBrandt at Hunter dot com
Oct 21 '06 #4

P: n/a
Rick Brandt wrote:
john wrote:
Thanks Rick. I partly got it working. The enter is 'recognized' but
I have to press enter twice to execute the button. With the first
enter the button gets focus (coming from the searchfield) and with
the second enter the button is actually pressed. Any idea to
resolve that? john

Then you still have something wrong. The <Enterkey will run ANY
button when that button has focus. The whole point of the Default
property is to make a button run from anywhere in the form by
pressing <Enter>.
I'll run a few tests and post back.
In my tests the button ran on the first <Enterpress no matter where I was in
the form and afterwards the button had focus. This was on a form with many
controls. I then built one like yours that had only a TextBox and a Button and
it still worked with a single <Enterpress.

--
Rick Brandt, Microsoft Access MVP
Email (as appropriate) to...
RBrandt at Hunter dot com
Oct 21 '06 #5

P: n/a
"Rick Brandt" <ri*********@hotmail.comschreef in bericht
news:Ir******************@newssvr21.news.prodigy.c om...
Rick Brandt wrote:
>john wrote:
Thanks Rick. I partly got it working. The enter is 'recognized' but
I have to press enter twice to execute the button. With the first
enter the button gets focus (coming from the searchfield) and with
the second enter the button is actually pressed. Any idea to
resolve that? john

Then you still have something wrong. The <Enterkey will run ANY
button when that button has focus. The whole point of the Default
property is to make a button run from anywhere in the form by
pressing <Enter>.
I'll run a few tests and post back.

In my tests the button ran on the first <Enterpress no matter where I
was in the form and afterwards the button had focus. This was on a form
with many controls. I then built one like yours that had only a TextBox
and a Button and it still worked with a single <Enterpress.
Thanks Rick. I got it working by putting the code in the button's OnFocus
event. Apart from that I can't figure out why the click event is only
triggered when I hit the enter key for the second time, which is different
from what you describe.
john
Oct 21 '06 #6

P: n/a
john wrote:
"Rick Brandt" <ri*********@hotmail.comschreef in bericht
news:Ir******************@newssvr21.news.prodigy.c om...
Rick Brandt wrote:
john wrote:
Thanks Rick. I partly got it working. The enter is 'recognized'
but I have to press enter twice to execute the button. With the
first enter the button gets focus (coming from the searchfield)
and with the second enter the button is actually pressed. Any
idea to resolve that? john
>
Then you still have something wrong. The <Enterkey will run ANY
button when that button has focus. The whole point of the Default
property is to make a button run from anywhere in the form by
pressing <Enter>.
I'll run a few tests and post back.
In my tests the button ran on the first <Enterpress no matter
where I was in the form and afterwards the button had focus. This
was on a form with many controls. I then built one like yours that
had only a TextBox and a Button and it still worked with a single
<Enterpress.

Thanks Rick. I got it working by putting the code in the button's
OnFocus event. Apart from that I can't figure out why the click event
is only triggered when I hit the enter key for the second time, which
is different from what you describe.
john
You did set the Default property of the button to True correct? This property
is found on the { Other } tab of the property sheet.

--
Rick Brandt, Microsoft Access MVP
Email (as appropriate) to...
RBrandt at Hunter dot com
Oct 21 '06 #7

P: n/a
"Rick Brandt" <ri*********@hotmail.comschreef in bericht
news:cc******************@newssvr21.news.prodigy.c om...
john wrote:
>"Rick Brandt" <ri*********@hotmail.comschreef in bericht
news:Ir******************@newssvr21.news.prodigy. com...
Rick Brandt wrote:
john wrote:
Thanks Rick. I partly got it working. The enter is 'recognized'
but I have to press enter twice to execute the button. With the
first enter the button gets focus (coming from the searchfield)
and with the second enter the button is actually pressed. Any
idea to resolve that? john

Then you still have something wrong. The <Enterkey will run ANY
button when that button has focus. The whole point of the Default
property is to make a button run from anywhere in the form by
pressing <Enter>.
I'll run a few tests and post back.

In my tests the button ran on the first <Enterpress no matter
where I was in the form and afterwards the button had focus. This
was on a form with many controls. I then built one like yours that
had only a TextBox and a Button and it still worked with a single
<Enterpress.

Thanks Rick. I got it working by putting the code in the button's
OnFocus event. Apart from that I can't figure out why the click event
is only triggered when I hit the enter key for the second time, which
is different from what you describe.
john

You did set the Default property of the button to True correct? This
property is found on the { Other } tab of the property sheet.
Sorry. I was so sure it was set to True but I must have switched it back to
False while testing. Now it's working like you described.
Thanks for your time...
john
Oct 21 '06 #8

P: n/a
have you tried just running the Find code directly from the "search"
control's AfterUpdate event procedure?

hth
"john" <jo**@test.comwrote in message
news:f_******************************@casema.nl...
In a form I'm trying to make a search field to be able to quickly find a
record on ID-number. User enters a number and presses enter to move to
that
record. How do I trap for the enter-keystroke and where should I put the
code?
I found this:
You can set the form's KeyPreview property to YES, then in the Form's
KeyPress event, add the following pseudo-code:
Select Case KeyAscii
Case 13
Select Case Screen.PreviousControl.Name
Case "cboMyCombo"
cmdMyButton_Click
End Select
End Select
but I wondered if there is a way to code this inside of the searchfield?
Also, if I have to use the above code where can I find the KeyPreview
property?
Thanks in advance,
john


Oct 21 '06 #9

P: n/a
Thanks, that works, but when user enters a wrong number and hits enter the
focus goes to the next field while I would like it to remain focused on the
search field. The same accounts for when a user doesn't update the field and
hits enter.
john

tina" <no****@address.comschreef in bericht
news:61*******************@bgtnsc04-news.ops.worldnet.att.net...
have you tried just running the Find code directly from the "search"
control's AfterUpdate event procedure?

hth
"john" <jo**@test.comwrote in message
news:f_******************************@casema.nl...
>In a form I'm trying to make a search field to be able to quickly find a
record on ID-number. User enters a number and presses enter to move to
that
>record. How do I trap for the enter-keystroke and where should I put the
code?
I found this:
You can set the form's KeyPreview property to YES, then in the Form's
KeyPress event, add the following pseudo-code:
Select Case KeyAscii
Case 13
Select Case Screen.PreviousControl.Name
Case "cboMyCombo"
cmdMyButton_Click
End Select
End Select
but I wondered if there is a way to code this inside of the searchfield?
Also, if I have to use the above code where can I find the KeyPreview
property?
Thanks in advance,
john



Oct 22 '06 #10

P: n/a
if you put the search control in the form's Header section, and the "record
data" controls in the Detail section, the focus should remain on the search
control unless/until it is programmatically or manually shifted to a control
in the Detail section.

hth
"john" <jo**@test.comwrote in message
news:vq******************************@casema.nl...
Thanks, that works, but when user enters a wrong number and hits enter the
focus goes to the next field while I would like it to remain focused on
the
search field. The same accounts for when a user doesn't update the field
and
hits enter.
john

tina" <no****@address.comschreef in bericht
news:61*******************@bgtnsc04-news.ops.worldnet.att.net...
have you tried just running the Find code directly from the "search"
control's AfterUpdate event procedure?

hth
"john" <jo**@test.comwrote in message
news:f_******************************@casema.nl...
In a form I'm trying to make a search field to be able to quickly find
a
record on ID-number. User enters a number and presses enter to move to
that
record. How do I trap for the enter-keystroke and where should I put
the
code?
I found this:
You can set the form's KeyPreview property to YES, then in the Form's
KeyPress event, add the following pseudo-code:
Select Case KeyAscii
Case 13
Select Case Screen.PreviousControl.Name
Case "cboMyCombo"
cmdMyButton_Click
End Select
End Select
but I wondered if there is a way to code this inside of the
searchfield?
Also, if I have to use the above code where can I find the KeyPreview
property?
Thanks in advance,
john



Oct 22 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.