Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent moving to next record when hitting Enter?

Tags:

vba

ms-access

I have a form in Access 2003 that should only be working with a single record. I can set the Cycle property to Current Record, but the form still jumps to the next record when I press Enter. My first thought was a KeyPreview property, but I'm not seeing one. My other thought is maybe the KeyPress or KeyUp event, but I thought I'd ask in case of unintended consequences. Any ideas?

like image 522
Michael Itzoe Avatar asked Feb 12 '09 20:02

Michael Itzoe


3 Answers

The Cycle property only affects the TAB key.

To control the behaviour of the Enter Key that's a global property.

Go to Tools/Options - Keyboard tab and on "Move After Enter" select "Next Field"

There are KeyPress and KeyDown events that you can use to trap the Enter key too but that's more work.

like image 110
DJ. Avatar answered Oct 13 '22 17:10

DJ.


This can also be done in vba.

Application.GetOption "Move After Enter" 'get current setting
Application.SetOption "Move After Enter", 0 'don't move
Application.SetOption "Move After Enter", 1 'Next Field
Application.SetOption "Move After Enter", 2 'Next Record

http://www.pcreview.co.uk/forums/enter-key-moving-next-field-t3454281.html

like image 39
RubberDuck Avatar answered Oct 13 '22 16:10

RubberDuck


The moment keys like TAB, Alt, PgUP, PgDn, Enter are pressed at the end of adding the record (after the last field mostly), the database auto saves all the info you entered on the form and wipes out the fields so that you can enter the next value. So what happens is that the data is saved but the form looks empty.

3 things to do:

  1. Form Cycle Property set to Current Record.
  2. Form Key Preview Property set to Yes.
  3. Add following code to the KeyDown Event of the form:

    '33 - PgUp; 34 - PgDown; 9 - Tab; 18=Alt; 13=Enter
    
    Select Case KeyCode
    Case 33, 34, 18, 9, 13
    KeyCode = 0    
    Case Else
    'Debug.Print KeyCode, Shift
    End Select
    

    I found this while scouring the web and don't take credit/responsibility for the code but I don't know where I found it. Works for me!

like image 35
Yusuf N. Avatar answered Oct 13 '22 18:10

Yusuf N.