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

ID: Q158931

The information in this article applies to:

SUMMARY

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

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.

1. Open the sample database Northwind.mdb (or NWIND.MDB in version 2.0).

2. Open the Main Switchboard form in Design view.

3. 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.

4. Save the form and open it in Form view.

5. Click the Exit Microsoft Access button. Note that Microsoft Access

   closes.

6. Restart Microsoft Access and open the Main Switchboard form in Form
   view.

7. Press CTRL and then click the Exit Microsoft Access button. Note that
   only the Main Switchboard form closes.

REFERENCES

For more information about mouse events, search the Help Index for "mouse events," or ask the Microsoft Access 97 Office Assistant.

Additional Query Words:

Keywords          : kbprg FmsEvnt FmsHowto 
Version           : 2.0 7.0 97
Platform          : WINDOWS
Hardware          : x86
Issue type        : kbhowto

Last Reviewed: November 21, 1998