问题
I have an MS Access database. The Shift key has previously been disabled but when the user presses F11 , she/he can see the navigation pane . Is there a solution to lock the F11 key to preventing opening the Navigation Pane?
回答1:
First, the answers here don’t make sense since if you have 100 forms and 100 reports, then you have 200 objects to modify. (this amounts a huge waste of developer time).
Much worse is if a report is displayed, then a form does not have the focus and again you see the nav pane when you hit F11.
Access for 20 years has had the ability to run code and take action for any Fkey or alt key hit to run a macro – and these settings are GLOBAL to the application.
Thus the simple solution is to make an auto keys macro for the application and include F11. This thus will work GLOBAL to the application.
So create a new macro (not a module) called autokeys. The macro will look like this:
You can set function keys in { }
You can also use ^ for control keys (so you can make a global print key) eg: ^P
And you can use + for the shift key, so
+{F11} would be the macro code that runs when you hit shift F11
+^P would be shift+ctrl P
In the above we could have F11 runcode or do anything we want such as launch a form, but as above shows we simply leave the action code inside the F11 block of code blank. So this can be used to have F1 launch a custom access “help” form, or re-map any alt keys that are GLOBAL to the application.
You then simple un-check the show nav pane in the current database start up settings and you are done. Of course as a developer you will hold down the shift key during start to prevent your start up code and forms launching – this also by-passes the auto key macro and ignores all of the settings you “set” in the current database such as hiding the nav pane. So hold down shift key and you can use F11 to show, hide the nav pane and not have your application start up code run so you can work as a developer.
回答2:
Try googling your problem first. I was able to find multiple solutions doing that.
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyF11 Then KeyCode = 0
End Sub
'(you need to set the form's Key Preview to Yes)
or try
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyF11 Then
MsgBox "F11 key is Disabled", vbCritical, "Error"
End If
End Sub
Or this:
File -> Option -> Current DB -> Use Access special Keys (Uncheck)
Ref: Disable F11 with VBA
回答3:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyF11
KeyCode = 0
MsgBox "F11 has been disabled.", vbOKOnly, "Disabled Key"
End Select
End Sub
This can be done this way with other keys as well. Just add another select case statement.
Also make sure you have a way to showing the Navigation Pane if you do decide to make changes in the future - otherwise you may lock yourself out from making changes.
来源:https://stackoverflow.com/questions/38093437/disable-f11-key-in-ms-access-to-prevent-opening-the-navigation-pane