Solved F Keys Functions for each object in form (1 Viewer)

ahmedjamalaboelez

Ahmed J. Aboelez
Local time
Today, 06:23
Joined
Feb 25, 2015
Messages
79
Hello
i have database with buttons ( new -Save - Cancel -Delete - Exit) in each form i use keys (F1-F2-F3-F4-F5) on form key down event to run my code ,
F keys run good when not focused in form objects or sub form objects code as follow
Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    Select Case KeyCode
        Case vbKeyF1
            KeyCode = 0
            MsgBox "F1 Pressed"
        Case vbKeyF2
            KeyCode = 0
            MsgBox "F2 Pressed"
        Case vbKeyF3
            KeyCode = 0
            MsgBox "F3 Pressed"
        Case vbKeyF4
            KeyCode = 0
            MsgBox "F4 Pressed"
        Case vbKeyF5
            KeyCode = 0
            MsgBox "F5 Pressed"
    End Select
End Sub

Problem : when i'm set focused in a textbox in subform code not run , how could i set up code as public sub to set ( keyDown Event for each object included in this form , so i can call while form load or opening
thanks
 

CJ_London

Super Moderator
Staff member
Local time
Today, 13:23
Joined
Feb 19, 2013
Messages
16,553
Only way would be to Apply your code to every control on the form and subform
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 08:23
Joined
Feb 28, 2001
Messages
26,998
The problem will probably be in HOW you refer to the objects that you are planning to manipulate.

You have a form and a sub-form. What you must realize is that they both have their own "locale" - their own separate context. If the way that you refer to the main form is written incorrectly, then if you press a key, I don't believe that changes focus - and that means that you are in an unpredictable place.

Look at this reference:

Note this sentence that is so important that it stands at the start of a paragraph in the article:

The object with the focus receives all keystrokes.

The Keypress event does not change focus. It uses the focus you have. That means that if you press a key and the control in focus doesn't have a Keypress event (and further, that the form owning that control doesn't either) then the Keypress is lost.

CJ's solution is correct, using the F-n keys is an all or nothing at all solution. The OTHER alternative is that you don't use the F-n keys, but instead implement command buttons on some form. When you click THOSE controls, focus changes immediately before the Keypress event. (See the middle of the referenced article to see event firing order in that case.)
 

Users who are viewing this thread

Top Bottom