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

Enter Key Behavior in Text Box

P: 77
Hi,

I have a access form on which there is a tab control with 2 tabs.

each tab has a text box (lets say text1 & text2)

each tab also has a command button (button1 & button2)

What I am looking for is when the user presses the Enter key in a text box, then the button_click event to be called. (true in both the tabs)

I have tried making the buttons to default=true, also tried putting the code in the Exit property of the text box but no luck.

I don't want to change the Enter Key property from the form's design view or from Access Options as the other users who are using the app may not have changed it and would not be able to work.

Pls suggest.

Thanks.
Aug 28 '08 #1
Share this Question
Share on Google+
10 Replies


P: 77
To my surprise, the keypress/keydown event fails to track the Enter key's ascii code as well and does nothing. The code works well for other keys..
Aug 28 '08 #2

Expert Mod 2.5K+
P: 2,545
Are you sure the enter keycode is not being recognised, Yaaara? I placed the following in the keydown event of a textbox called Country to check and it worked as expected when the enter key was pressed:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Country_KeyDown(KeyCode As Integer, Shift As Integer)
  2.     If KeyCode = 13 Then
  3.         MsgBox ("Return Pressed")
  4.     End If
  5. End Sub
-Stewart
Aug 28 '08 #3

missinglinq
Expert 2.5K+
P: 3,532
Stewart's code is pretty standard stuff. Perhaps we need to look at the actual code you've tried.

You can, of course, only assign one command button as the default for a given form. One question does come to mind; does the appropriate event occur when you simply click on one of these command buttons, and what are these buttons supposed to do?

Welcome to Bytes!

Linq ;0)>
Aug 28 '08 #4

P: 77
Hi,

Although this code appears to be working (Strangely enough, it was not working yesterday), but I am now getting another issue..

Here is the code that I have:

Expand|Select|Wrap|Line Numbers
  1. Private Sub txtEmpIDATT_KeyDown(KeyCode As Integer, Shift As Integer)
  2.     If KeyCode = 13 Then
  3.         If txtEmpIDROD.Text = "" Then
  4.         'do nothing
  5.         Else
  6.             Call cmdHidden3_Click
  7.         End If
  8.     End If
  9. End Sub
  10.  
Whenever I press enter key in the text box, it gives me "error 2185: You can't reference a property or method for a control unless the control has focus"

Pls help...

Thanks.

Stewart's code is pretty standard stuff. Perhaps we need to look at the actual code you've tried.

You can, of course, only assign one command button as the default for a given form. One question does come to mind; does the appropriate event occur when you simply click on one of these command buttons, and what are these buttons supposed to do?

Welcome to Bytes!

Linq ;0)>
Aug 29 '08 #5

NeoPa
Expert Mod 15k+
P: 31,485
There are ways to get around this, but I would strongly recommend against using them.

Instead of that, consider moving your cmdHidden3_Click code out to a separate subroutine and calling THAT from both places.

IE.
Before :
Expand|Select|Wrap|Line Numbers
  1. ...
  2.   Call cmdHidden3_Click
  3. ...
  4. Private Sub cmdHidden3_Click
  5.   Some code
  6. End Sub
After :
Expand|Select|Wrap|Line Numbers
  1. ...
  2.   Call DoSomeCode
  3. ...
  4. Private Sub cmdHidden3_Click
  5.   Call DoSomeCode
  6. End Sub
  7.  
  8. Private Sub DoSomeCode
  9.   Some code
  10. End Sub
Does that make better sense?
Aug 29 '08 #6

P: 77
Actually, cmdHidden3_Click is not associated with any control and is just like any other routine.. I did have a command button named cmdHidden3 earlier, but i removed it.. Just didn't change the name of the function (call me lazy for this :-| but I'll change it anyways)

So, in this scenario, the below solution won't work out as it is one and the same thing.. isn't it?

What I am currently doing is using the "Exit" event of the text box for now which works fine with the TAB key, but not with the Enter key... What I want is the trapping of Enter key...

Dunno what to do :-(
Aug 29 '08 #7

Expert Mod 2.5K+
P: 2,545
Hi Yaaara. I think you are overlooking the context of your error message. It appears to relate to textbox txtempIDROD, whose text property is only available when the control has focus. If you set a breakpoint and step through your code you will no doubt find it stops here:

IF txtempIDROD.txt = "" THEN

To resolve this, check for the control being null (not its text property being empty)

IF IsNull(me.txtempIDROD) THEN

The help file entry for the Text property makes all this clear.

Note To set or return a control's Text property, the control must have the focus, or an error occurs. To move the focus to a control, you can use the SetFocus method or GoToControl action.
-Stewart
Aug 29 '08 #8

P: 77
I see... Hmmm... didn't check this...

I applied the IsNull function and it works............

Awesome Help Stewart... Many Thanks :-)
Aug 29 '08 #9

missinglinq
Expert 2.5K+
P: 3,532
Also note that Stewart didn't include the .Value property in his statement, even though the txtempIDROD textbox doesn't have focus. That's because the Default Property of a textbox is .Value and can be omitted.

IsNull(me.txtempIDROD)

and

IsNull(me.txtempIDROD.Value)

mean the same thing in Access VBA.

Linq ;0)>
Aug 29 '08 #10

P: 77
Thanks for your inputs as well missing :-)

Also note that Stewart didn't include the .Value property in his statement, even though the txtempIDROD textbox doesn't have focus. That's because the Default Property of a textbox is .Value and can be omitted.

IsNull(me.txtempIDROD)

and

IsNull(me.txtempIDROD.Value)

mean the same thing in Access VBA.

Linq ;0)>
Aug 30 '08 #11

Post your reply

Sign in to post your reply or Sign up for a free account.