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

Getting cursor at beginning of new record on pressing ENTER key

P: 44
Hello.
I need the cursor to go at the beginning of a new record in a data sheet form when pressing the ENTER key.
I tried to change the default settings of the key enter and all I get is the cursor in a new record but not at the beginning of the record itself.
Any suggestion or help wiill be much appreciated.
David
Aug 11 '17 #1

✓ answered by NeoPa

I had to amend the code once I remembered that the keys to check for were simply <ENTER>.
Expand|Select|Wrap|Line Numbers
  1. 'Form_KeyDown only runs if the form's .KeyPreview property is True.
  2. Private Sub Form_KeyDown(ByRef KeyCode As Integer, ByRef Shift As Integer)
  3.     If (Not Shift) _
  4.     And (KeyCode = vbKeyEnter) Then
  5.         KeyCode = vbKeyAdd
  6.         Shift = (Shift Or acCtrlMast)
  7.         Exit Sub
  8.     End If
  9. End Sub
Be aware that hitting of the <ENTER> key won't get passed through if you have a CommandButton Control on the form set as Default = True.

Share this Question
Share on Google+
25 Replies


PhilOfWalton
Expert 100+
P: 1,430
David.
What do you mean by "at the beginning". Do you mean the first field?

If so, on the Design Tab there is a "Tab Order" button. Probably "Auto Order" will do what you want.

Phil
Aug 11 '17 #2

P: 44
Hello Phil.
Sorry. Yes I mean the first field. I have tried to set the tab order in Auto Order and change the default enter in “New Record”.Unfortunately when I press the enter key the cursor remains in the same field and the same record.
Am I doing something wrong?
David.
Aug 11 '17 #3

PhilOfWalton
Expert 100+
P: 1,430
Plan B (Always have a plan B)
Try this code on the On Current of the form

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Current()
  2.  
  3.     MyField.SetFocus      ' Where MyField is the name of that first field
  4.  
  5. End Sub
  6.  

Phil
Aug 11 '17 #4

NeoPa
Expert Mod 15k+
P: 31,409
Unfortunately, the option only stipulates that it goes to the next record on <Enter>. Not which field or control (It covers continuous forms as well as datasheet view.) of that record :-(

Phil's idea is probably the best available, however, I expect it will only work on controls on a form rather than fields in datasheet view. If I remember correctly (most) events don't fire in datasheet view.
Aug 11 '17 #5

P: 44
Hello Phil.
Tried your code but the cursor keeps staying in the same field and same record after pressing the Enter key. A plan C seems nneeded...
I'll be off for 5 days and sure enough I'll be thinking for a possible solution. I do hope you can help with some idea to get around the problem. See you soon.
David
Aug 12 '17 #6

PhilOfWalton
Expert 100+
P: 1,430
Have a good holiday.

Don't forget that after every entry in each field, you are going to press the "Enter" Key.

My code works for a datasheet on the On Current (which also means when you go to a new record)

I am assuming you have no other code that could be confusing the issue

Personally, I never use datasheets. I much prefer a continuous form that seems to have much more formatting potential.

Phil
Aug 12 '17 #7

NeoPa
Expert Mod 15k+
P: 31,409
I tried Phil's solution and it worked for me both in Continuous Form view and as a Datasheet. Do be aware that it forces to the first control or field whether you use <Enter> or <Down Arrow>, or even clicking on a specific control or field of another record.

Plan C would appear to be unnecessary. Plan B has not been properly explored yet. One can't be too precipitate in this game. Too many gotchas to catch you out unless you're precise and fastidious.
Aug 12 '17 #8

P: 44
I'm sure Phil 's B plan is correct though, in my case, it doesn 't seem to work.I Cannot guess what is conflicting with his code.
David
Aug 13 '17 #9

P: 44
When I tried your code I wrote it within the form events. Is it possible that it was meant to be written for the subform events, that is where I enter data?
Hope you understand my poor English
David
Aug 13 '17 #10

PhilOfWalton
Expert 100+
P: 1,430
Yes, David, It's in whichever form you are entering the data.

This is the first mention of a Subform, but that certainly is where the code should go

Phil
Aug 13 '17 #11

P: 44
Sorry, Phil, I didn't mention it before.
I will try again your code when I'm back home.
David
Aug 13 '17 #12

NeoPa
Expert Mod 15k+
P: 31,409
David:
Hope you understand my poor English
I didn't even know you had poor English till you mentioned it. So, doing well enough I would suggest :-)
Aug 13 '17 #13

P: 44
Hello Phil,
Your B plan is absolutely correct. In fact, it works fine when the cursor (previous to pressing the enter key) is set in any field except one which contains text data type.
In this case, after pressing the enter key I get the following result:
1) the cursor remains in the same field and record; 2) any data just typed in this field is deleted.
Can the text data type for this field affect the behavior of the enter key? All the other fields have numeric type data.
Aug 20 '17 #14

PhilOfWalton
Expert 100+
P: 1,430
Are you running any code to do with keys like Key Press, Key Down or Mouse events?

Phil
Aug 20 '17 #15

P: 44
No, definetely.
David.
Aug 20 '17 #16

P: 44
I have seen that if I press the combination Ctrl+PlusKey instead of the enter key, the cursor is correctly sent, in any case, to the first field of a new record.
So it would be fine if I could pre-set through an option: enter = Ctrl+PlusKey or something like that.

David
Aug 20 '17 #17

PhilOfWalton
Expert 100+
P: 1,430
Well I am stumped. Can you send me the DB stripped of all sensitive information so that I can see the problem
If you send me a Private message, I will let you have my Email Address

Phil
Aug 20 '17 #18

NeoPa
Expert Mod 15k+
P: 31,409
The following code is an example of what you might use :
Expand|Select|Wrap|Line Numbers
  1. 'Form_KeyDown only runs if the form's .KeyPreview property is True.
  2. Private Sub Form_KeyDown(ByRef KeyCode As Integer, ByRef Shift As Integer)
  3.     If (Shift And acCtrlMask) _
  4.     And (KeyCode = vbKeyEnter) Then
  5.         KeyCode = vbKeyAdd
  6.         Exit Sub
  7.     End If
  8. End Sub
Be aware that hitting of the <ENTER> key won't get passed through if you have a CommandButton Control on the form set as Default = True.
Aug 20 '17 #19

ADezii
Expert 5K+
P: 8,619
I haven't tested the Code extensively, but the following Code, in the KeyPress() Event of a Form, appears to work quite well. Being on any Record except the last and pressing the ENTER Key will automatically advance to the first Field ([ID] in Demo) in the Next Record. The Form's KeyPreview Property must be set to True in order for this approach to work.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_KeyPress(KeyAscii As Integer)
  2. On Error GoTo Err_Form_KeyPress
  3. Const conENTER_Key As Byte = 13
  4.  
  5. If KeyAscii = conENTER_Key Then         'ENTER Key pressed
  6.   With Me
  7.     .RecordsetClone.MoveNext
  8.       If Not .RecordsetClone.EOF Then   'NOT the EOF
  9.         .Recordset.MoveNext             'Advance to Next Record
  10.         ![ID].SetFocus                  'Set Focus to 1st Field
  11.       End If
  12.   End With
  13. End If
  14.  
  15. Err_Form_KeyPress:
  16.   Exit Sub
  17. End Sub
Aug 20 '17 #20

NeoPa
Expert Mod 15k+
P: 31,409
I had to amend the code once I remembered that the keys to check for were simply <ENTER>.
Expand|Select|Wrap|Line Numbers
  1. 'Form_KeyDown only runs if the form's .KeyPreview property is True.
  2. Private Sub Form_KeyDown(ByRef KeyCode As Integer, ByRef Shift As Integer)
  3.     If (Not Shift) _
  4.     And (KeyCode = vbKeyEnter) Then
  5.         KeyCode = vbKeyAdd
  6.         Shift = (Shift Or acCtrlMast)
  7.         Exit Sub
  8.     End If
  9. End Sub
Be aware that hitting of the <ENTER> key won't get passed through if you have a CommandButton Control on the form set as Default = True.
Aug 20 '17 #21

P: 44
Thank you very much NeoPa, your code works perfectly.
I also wish to thank Phil who, in this matter has been so helpful.
David
Aug 23 '17 #22

NeoPa
Expert Mod 15k+
P: 31,409
Hi David.

I understand your point completely. However, the Best Answer is not simply a Thank you. It is also the closest thing to an answer to the original question that any searcher will see quickest when they look. For that reason I've reset it (Sorry Phil). Perhaps you'd be good enough to select another from your own perspective that helped most and, more fundamentally, would be most helpful to another seeking the same solution.

Normally I'd do that myself but I'm not sure in this case so thought - You know, this David seems a pretty with-it chap. I'll leave it to him.
Aug 24 '17 #23

P: 44
I've just selected the answer that from my own perspective helped most.
Sorry I didn't do it before.
David
Aug 24 '17 #24

NeoPa
Expert Mod 15k+
P: 31,409
David57:
Sorry I didn't do it before.
You had selected a post as Best Answer, so have nothing to be sorry for. It was I that reset that one, not because Phil didn't deserve your thanks by any means, but simply based on what the main reason for the Best Answer is.

I would say you've behaved impeccably throughout.
Aug 24 '17 #25

P: 44
Thanks, NeoPa.
David
Aug 25 '17 #26

Post your reply

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