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?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Eric FlammOwnerCommented:
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).
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.
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...

Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems AnalystCommented:
Can you post the code you have now ?
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.
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems AnalystCommented:
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 ...

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'
End If

End sub
Eric FlammOwnerCommented:
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.
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.
Eric FlammOwnerCommented:
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MCaliebeAuthor Commented:
What is the effect if there is more then one command button on a form?
Eric FlammOwnerCommented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.