Microsoft KB Archive/158931

= ACC: How to Set a Keystroke+Click to Activate a Command Button =

Article ID: 158931

Article Last Modified on 1/19/2007

-

APPLIES TO


 * Microsoft Access 2.0 Standard Edition
 * Microsoft Access 95 Standard Edition
 * Microsoft Access 97 Standard Edition

-



This article was previously published under Q158931



Moderate: Requires basic macro, coding, and interoperability skills.



SUMMARY
This article describes a method for using a keystroke-plus-click combination with a command button. For example, you might want click, ALT+Click, and CTRL+Click to have different actions on the same command button.

This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the "Building Applications with Microsoft Access" manual.

NOTE: Visual Basic for Applications is called Access Basic in version 2.0. For more information about Access Basic, please refer to the "Building Applications" manual.



MORE INFORMATION
The sample database Northwind.mdb (or NWIND.MDB in version 2.0) contains a Main Switchboard form with several buttons, one of which reads "Exit Microsoft Access." When you click this button, it closes Microsoft Access completely. Sometimes, you may want to just close the Main Switchboard form and keep Microsoft Access and the database open.

Instead of creating one command button to close Microsoft Access and another button to close the Main Switchboard form, the following example shows you how to create one button which is capable of doing both, depending on whether you click the button or press CTRL and then click the button.  Open the sample database Northwind.mdb (or NWIND.MDB in version 2.0). Open the Main Switchboard form in Design view.  Add the following event procedure to the OnMouseDown property of the Exit Microsoft Access command button:

In Microsoft Access 7.0 and 97: Private Sub ExitMicrosoftAccess_MouseDown(Button As Integer, Shift _           As Integer, X As Single, Y As Single) If Shift And acCtrlMask Then DoCmd.Close End If     End Sub In Microsoft Access 2.0:

NOTE: In the following sample code, an underscore (_) at the end of a line is used as a line-continuation character. Remove the underscore from the end of the line when re-creating this code in Access Basic. Sub Exit_Microsoft_Access_MouseDown (Button As Integer, Shift As _           Integer, X As Single, Y As Single) If Shift And CTRL_MASK Then DoCmd Close End If     End Sub Other constants available for Microsoft Access 7.0 and 97 are acShiftMask and acAltMask, representing the SHIFT and ALT keys respectively. In Microsoft Access 2.0, you can use SHIFT_MASK and ALT_MASK.  Save the form and open it in Form view. Click the Exit Microsoft Access button. Note that Microsoft Access closes. Restart Microsoft Access and open the Main Switchboard form in Form view.</li> Press CTRL and then click the Exit Microsoft Access button. Note that only the Main Switchboard form closes.</li></ol>

<div class="references_section">