ACC2000: How to Set a Keystroke + Click to Activate a Command Button (209984)



The information in this article applies to:

  • Microsoft Access 2000

This article was previously published under Q209984
Moderate: Requires basic macro, coding, and interoperability skills.

This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).

SUMMARY

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

MORE INFORMATION

The sample database Northwind.mdb contains the Main Switchboard form with several buttons, one of which is the Exit Microsoft Access button. When you click this button, it causes Microsoft Access to quit 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 quit Microsoft Access and another button to close the Main Switchboard form, the following example shows you how to create one button that is capable of doing both, depending on whether you click the button or press and hold CTRL, and then click the button.

CAUTION: If you follow the steps in this example, you modify the sample database Northwind.mdb. You may want to back up the Northwind.mdb file and follow these steps on a copy of the database.

  1. Start Microsoft Access and open the sample database Northwind.mdb.
  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:
    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
    					
    Other intrinsic constants available for Microsoft Access 2000 are acShiftMask and acAltMask, representing the SHIFT and ALT keys respectively.

  4. Save the form and open it in Form view.
  5. Click Exit Microsoft Access. Note that Microsoft Access quits.
  6. Restart Microsoft Access, and then open the Main Switchboard form in Form view.
  7. Press and hold down CTRL, and then click the Exit Microsoft Access button. Note that only the Main Switchboard form closes.

REFERENCES

For more information about mouse events, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type mouse events in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

For more information about constants applicable to mouse events, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type intrinsic constants as bit masks in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

Modification Type:MinorLast Reviewed:7/15/2004
Keywords:kbhowto kbinfo kbprogramming KB209984