fbpx
Logo
Mail us
ben@accessdatabasetutorial.com
accessdatabasetutorial
Home » Database Theory » Microsoft Access Forms: Working With The KeyPreview Property

Microsoft Access Forms: Working With The KeyPreview Property

Microsoft Access Forms: Working With The KeyPreview Property


Working and managing data via Ms Access forms is only as good as the design and if the user becomes frustrated then don’t expect your database (and you) to receive any credit.

One element overlooked when form designing which is easily missed to be fair, is the lack of keyboard short-cuts should users wish to throw away that pesky ‘rodent’ mouse! After all, we end up enter and managing the data via the keyboard in any case.

Ms Access forms

As you may have gathered by now, I’m a keyboard fan and only use the mouse for the obvious interactions and even though  Microsoft Access provides the basic and essential keystrokes including navigation and general Window actions (like copy and paste), there is a need to take full control when managing these forms and is some cases, need to disable the general actions too.

Microsoft Access Forms: Working With The KeyPreview Property

There is a property in the form’s design environment which needs to be enabled (set as True) called ‘Key Preview’ and it controls when your keyboard actions are executed.

For example, when this property is set to the default False value and the use presses the ‘F2’ function key in a field text box control, the cursor enters edit mode ready for data value updates since ‘F2’ means edit data.

However, setting the ‘Key Preview’ to True causes the form and not the control’s event to be called first giving you the opportunity to first add your own Access VBA (or macro) procedures before the active control takes over. In my example, I may want to validate the contents of the text box field first by adding code to the ‘On Key Down’ event as this simple piece of code below shows:

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
   If KeyCode = vbKeyF2 Then MsgBox "F2 key called & _
                                      "- validate data entry?"
End Sub

There are three form events that are released over to you to add interactive control to your form designs:

  1. On Key Down – When you press any key on the keyboard while a control or form has the focus.
  2. On Key Press – When you press and release a key or key combination that produces a standard ANSI character while a control or form has the focus.
  3. On Key Up – When you release a pressed key while a control or form has the focus.

Specially designed Ms Access Forms therefore are typically controlled using this approach when wanting to custom build the keyboard short-cuts should you for example want to really tighten down the general Windows key strokes as in some of my designs where I have disabled the standard Cut, Copy and Paste commands (Ctrl + X, C and V).

At a higher level, I have created a library of modules which are plugged into any database that is going to utilise my keyboard functionality instead so it becomes a modular tool with my designs.

I welcome your comments on what you use the ‘Key Preview’ property for – please reply in the comments box below.

Tags: , , , , ,

2 Responses so far.

  1. Giorgio Rovelli says:

    Very interesting article. So if I wanted to associate a particular action to a combination of keys, how would I go about doing it?