Rob
  • Rob
  • Newbie Topic Starter
2012-10-16T16:29:40Z
I am using the latest version of iGrid in MS Access 2007. I have setup the grid so that it is a large, read-only (.Editable = False) list that a user will select a row from (.RowMode = True). A detail form will be called whenever a user either double-clicks (which I have working) or hits the "enter" key (which is not working).

How do I capture the Enter key, keep the focus on the grid, and not let it go into cell edit mode?

-- Rob
Igor/10Tec
2012-10-17T06:54:23Z
Rob, that's a very good question so we even made it sticky and changed the topic title from 'Trapping the Enter Key on a Readonly Grid' to 'Trapping the Enter Key in MS Access'.

The problem does not relate to the read-only mode. The problem is in the way Access processes such significant interface keys as ENTER and TAB. MS Access analyzes them first, and only after that we can process them in other form controls - sure, if Access allows us to 'see' them. I mean if a key is an important interface key such ENTER which can affect the whole form (ENTER can be used to move to the next record or next field on the form), Access can simply 'swallow' it. These are two different layers of key processing: the first is the Access form layer, the second is the controls on the form. If the key is processed on the 1st layer, the 2nd won't see it.

You can find a lot of Internet resources explaining how to set the ENTER key behavior in MS Access. In fact, we need the following setting in the Options dialog:

MS Access Enter Key Behavior Setting.png

Click to View Image135 View(s)



To solve our problem, first, set the "Move after enter" radio button to the "Don't move" option.

Second, to process ENTER, we need to use the form's KeyDown event. To make it possible, we also need to set the KeyPreview property of the form to True and use code like this:

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
   If Screen.ActiveControl.Name = "iGrid0" Then
      If KeyCode = 13 Then
         MsgBox "ENTER"
      End If
   End If
End Sub
Rob
  • Rob
  • Newbie Topic Starter
2012-10-17T17:55:13Z
Igor, as your mentioned, there is an Advance Option in MS Access that allows you to specify what what happens when you hit the ENTER key - Don't move, Next Field and Next Record. The downside to this is that it is a universal setting and applies to all databases and forms. I came across a post on the internet with another suggestion that seems to work.

1. On the Form, set Key Preview to True
2. Capture the Keystroke using the "On Key Down" event of the form
3. Change the value of KeyCode to whatever value represents the key you would like the system to process. In my case, I want the system to do nothing, so I set the value of KeyCode to -1.

So the code looks like this:

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    If Screen.ActiveControl.Name = "ilstList" Then
        If KeyCode = 13 Then
            MsgBox "ENTER"
            KeyCode = -1  'changes the KeyCode to a value ignored by the system
        End If
    End If
   
End Sub