Capturing the "Return" key and the text in the associated text box

I have a form which I am entering text into [txt_hours].  I capture the return key in this text box to fire off the code for cmd_add_line.  I also have a command button to fire off this code should I desire.

One of the checks before initiating a table update query is that [txt_hours] contain data.  It appears that when I changed the "enter Key Behavior" property to "new line", the enter key is captured but the text entered in the text box is not updated in the field so my code believes that [txt_hours] = "" when in actuality I just entred 40 into the field.

Any thoughts on how i still get te text box updated and fire off the cmd_add_line code?
MCaliebeAsked:
Who is Participating?
 
Eric FlammConnect With a Mentor Senior ConsultantCommented:
Right - and if you set the Command button's Default property (on the Other tab in the Property sheet) to True, and set the Enter Key behavior for the text box to Default - when you hit Enter in the text box, the code for the command button WILL FIRE - just like you said. You have to set the default property of the command button to true, or you get exactly the behavior you describe.
0
 
Eric FlammSenior ConsultantCommented:
Why did you change the enter key behavior to "new line" - can the input be multi-line? If not, just change the enter key behavior back to Default. The default behavior is to trigger the control marked as default - usually a command button; I'm not sure what other controls have the Default behavior property. If the text box can be multi-line, you'll need to the Enter Key to enter new lines, and you won't want to "capture" it (which, as noted, is the default behavior without any coding).
0
 
MCaliebeAuthor Commented:
What I noticed is that the default behavior for the enter key is to move onto the next item in the tab stop index so the enter key is never captured as a means of firing off an event.  When I changed hte property to new line, the enter key was captured however my data is not updated before the key is captured.

I tried to requery the text box but i get an error that the field must be saved before I can requery it.  However I can't find a "save" function for a text box.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
Jeffrey CoachmanMIS LiasonCommented:
Please be clear:
Is this a Single view form or a datasheet/continuous form?

"What I noticed is that the default behavior for the enter key is to move onto the next item in the tab stop index"

As far as I can tell this is *not* the default behavior on forms.
If you create a new database, table and form, ...upon opening the form and pressing the "Enter" key, no movement should occur.

The "Default" for the "enter Key Behavior" is always set to "Don't Move", in the database options

So it appears that in your situation someone changed this setting.

Like eflamm, I try not to change this setting unless absolutely necessary.

Perhaps it would clear things up if you first took a step back and explained what your ultimate goal is here, in case there is a more efficient design approach...


JeffCoachman
0
 
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
Can you post the code you have now ?
0
 
MCaliebeAuthor Commented:
I changed the text box default for the "enter key behavior" back to default.

From what I can tell, the tab key moves through the tab stops.  The enter key also moves through the tab stops howver, if the tab stop is a command button, the enter key will initiate the code behind the button.

My intent is to mimic a command button when the "enter" key is pressed in the text box.

This is a payroll sheet so there is an unbound text box txt_hours that is the last data entry fireld.
When the hours are added, "40",  I want the enter key to fire off the same code that lies behind the "add record" command button.

Now that I have switched the enter key back to default, the curser moves out of the text field onto the next tab stop so the enter key isn't captured under the "key pressed" event.
0
 
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
Possible you should look at the On Change event of the text box, and the subsequent .Text property. The On Change triggers for *each* character typed, and the .Text property (as opposed to the .Value - default) is up to date with the typing ...

mx
0
 
MCaliebeAuthor Commented:
Not sure how you are suggeting I use this.  for the text box being typed into, use the On Change property and call for the text? something like

Private Sub txt_hours_on_change ()

txt_hours.text  'what does this do?

End Sub

****then capture the enter key

Private Sub txt_hours_On_Keypress ()

If KeyAscii = vbKeyReturn Then  'capture the enter key (enter key behavior has tobe set to 'new line'
    Me.txt_hours
    Cmd_Add_Click
End If

End sub
0
 
Eric FlammSenior ConsultantCommented:
Actually - what you want to do
mimic a command button when the "enter" key is pressed
is what the "Default" setting on a button does. Only one button can have Default=True on a form - once you set Default=True, when you hit Enter, the click event of the button will fire.
0
 
MCaliebeAuthor Commented:
Actually, what I want to do is run the code attached to a command button.  However this could be any code.  My issue is when I enter into the text box and type my number ie. "40, and hit the enter key, the curser moves onto the next tab stop.  I could easily capture any other ASCII key and fire code from it, however I need to capture the "enter" or return key.
0
 
MCaliebeAuthor Commented:
What is the effect if there is more then one command button on a form?
0
 
Eric FlammSenior ConsultantCommented:
Only 1 can be the Default (one button to rule them all). Try it - setting one button's Default to Yes sets the other to No.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.