Scott G Sanders

Access Database VBA Detect Shift Key on Button Click

While working on a Time Logging database, I wanted to develop a simple way for the user to add a second entry without adding another button to the form.  Let’s say that 99% of the time there will be just one entry, so having the extra button would get in the way of a clean look.  Well, at least in my opinion.  At the same time, I did not want to trouble the user by having them leave the input form.  My solution was to enable the user to hold down the shift key, which would cause the save button to save the current record and automatically reset the form for a new entry.  Normally, the save button would save and close the input form.

The VBA Code

If you want to follow this example, start with a blank form and create a command button cmdTest.

The VBA code provided below is simple and to the point.  Basically, it will detect if the Shift key is pressed and run alternate code.

Shift Click VBA Code

Step 1:  create a public variable to store the status of the Shift key as TRUE or FALSE.

Dim boolShift as Boolean

Step 2: add MouseDown code to the cmdTest button.  This will update the public variable with the status of the shift key each time the user clicks on the button.

Private Sub cmdTest_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
”This is the actual status check for the Shift Key status used in the Click event.
”Set shift button status…  Place a negative (-) in front to make it True/False.
boolShift = -Shift

End Sub

Step 3: Now, it is time to make use of the public variable and build the cmdTest click code.  In the previous step, we set the public variable to the current shift key state.  In this step we will look at that variable and run code based on if the user is holding down the  shift key.

Private Sub cmdTest_Click()
”Determine the user intent based on the pressing of a shift key.
If boolShift = True Then
MsgBox “Shift Key was held Down during Button Click”
Else
MsgBox “Shift Key was NOT detected during Button Click”
End If
End Sub

Step 4:  This step is optional, but I find that it is helpful to let the user know what is happening.  For this example we will change the caption text of the command button based on the status of the shift key.  When the user moves the mouse over the cmdTest button code will run to determine the status of the shift key.  If the shift key is pressed the text will change to “Test + SHIFT” to let the user know something different will happen when the button is clicked.

Private Sub cmdTest_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
”Set Shift button status
boolShift = -Shift

”This is for visually changing the command button text.
If boolShift = True Then
Me.cmdTest.Caption = “Test + SHIFT”
Else
Me.cmdTest.Caption = “Test”
End If
End Sub

 

Finished Product

Below is an example of this code in action.


Posted

in

by

Tags:

Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.