Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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

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!

The Cycle property only works with the Tab key.

There are two options you could pursue.

You could trap the Enter key in KeyDown/KeyUp/KeyPressed
- OR -
You could filter the data source to the one record you want them editing, and disable adding new records through that form.

You can add below code to your form 'BeforeUpdate' event. If use want to move to next record, it will ask user to save then close the form before they can move to another recorde.

  Private Sub Form_BeforeUpdate(Cancel As Integer)
    Select Case MsgBox("Save?", vbYesNo)
      Case vbYes
        DoCmd.Close
      Case vbNo
        Cancel = True
    End Select
  End Sub

If you go into Access Options on the file page, go to Client Settings and the first setting will let you choose where your focus changes to when you press enter. At least in Access 2013.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top