What is meant by the "preview" property of a form in MS Access?

I am now programming in MS Access, and after going through the properties of the form, I found a property called "key preview". The MS Access documentation reports that it is used to trigger keyboard events for forms before keyboard events for controls.

I'm not sure what this means and it may be possible to use this property?

+4
source share
1 answer

If the KeyPreview property is set to true, the form will receive a keyboard event before the control does this, giving you the ability to do something with it at the form level.

Now imagine that you have a form with many controls. Imagine that you want the user to press the F2 key on the keyboard to perform some actions, for example, open another form, play some kind of music or something else.

In Access, as in most user interface programs, only one control can receive focus at any given time. Therefore, if the focus is in the text box, when you press F2, this keyboard keyboard event will fire.
You can catch this keystroke from text field events, but if the user hits F2 from another control, it will not be detected unless you also find that F2 has been removed from this control.

So to make things simpler, you can simply configure the form to receive keyboard events first, before they are transferred to the control that has focus, which will give you the opportunity to detect that the user pressed F2 in one place, instead of having to connect each element controls to detect this keystroke.

Example

Add one mytextbox text field to the new form. In the code for the form, add the following events to catch keyboard events for both the form and the text field:

 Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer) Debug.Print "Form_KeyDown(keycode:" & KeyCode & ", Shift:" & Shift & ")" End Sub Private Sub Form_KeyPress(KeyAscii As Integer) Debug.Print "Form_KeyPress(KeyAscii:" & KeyAscii & ")" End Sub Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer) Debug.Print "Form_KeyUp(keycode:" & KeyCode & ", Shift:" & Shift & ")" End Sub Private Sub mytextbox_KeyDown(KeyCode As Integer, Shift As Integer) Debug.Print "mytextbox_KeyDown(keycode:" & KeyCode & ", Shift:" & Shift & ")" End Sub Private Sub mytextbox_KeyPress(KeyAscii As Integer) Debug.Print "mytextbox_KeyPress(KeyAscii:" & KeyAscii & ")" End Sub Private Sub mytextbox_KeyUp(KeyCode As Integer, Shift As Integer) Debug.Print "mytextbox_KeyUp(keycode:" & KeyCode & ", Shift:" & Shift & ")" End Sub 

Now open the form and enter one key.
If you press the Q key, you will see something like this in the next window:

Control Keyboard events

Only the text field received events.

Now set the KeyPreview property to true, and when you open the form and press Q , you should see that the form first received Keyboard events.

Form and Control Keyboard events

+5
source

Source: https://habr.com/ru/post/1443580/


All Articles