hicksi
Board Regular
- Joined
- Mar 5, 2012
- Messages
- 203
I needed to provide multiple uses for Command-Buttons in an Excel UserForm.
Ie, to Preview a report (click); to Print the report without preview (Alt-Click); to Save the report to file (Control-click)
The following code was developed to illustrate the options, so I post it here. Use it as you wish...
Using a form that looks like this:
I have the behind-code
which uses the single common module code of:
Ie, to Preview a report (click); to Print the report without preview (Alt-Click); to Save the report to file (Control-click)
The following code was developed to illustrate the options, so I post it here. Use it as you wish...
Using a form that looks like this:
I have the behind-code
VBA Code:
Option Explicit
Dim tState As ShiftKeys
Dim isDblClick As Boolean
Private Sub CommandButton1_Click()
Call showKeySelection
Label1.Caption = tState
isDblClick = False
End Sub
Private Sub CommandButton1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
isDblClick = True
Call CommandButton1_Click
End Sub
Private Sub showKeySelection()
tState = CheckKeyState
CheckBoxLS.Value = tState And KeyLeftShift
CheckBoxRS.Value = tState And KeyRightShift
' CheckBoxS.Value = tState And (KeyLeftShift + KeyRightShift)
CheckBoxS.Value = tState And KeyShift
CheckBoxLC.Value = tState And KeyLeftCtrl
CheckBoxRC.Value = tState And KeyRightCtrl
' CheckBoxC.Value = tState And (KeyLeftCtrl + KeyRightCtrl)
CheckBoxC.Value = tState And KeyCtrl
CheckBoxLA.Value = tState And KeyLeftAlt
CheckBoxRA.Value = tState And KeyRightAlt
' CheckBoxA.Value = tState And (KeyLeftAlt + KeyRightAlt)
CheckBoxA.Value = tState And KeyAlt
'
CheckBoxDblClick.Value = isDblClick
End Sub
which uses the single common module code of:
VBA Code:
Option Explicit
' This routine demonstrates the tests for 'shift' key options when you click a button
' It determines whether you have pressed any of the Shift, Alt or Ctrl keys as well
'
' Based on information gleaned from 'http://www.cpearson.com/excel/keytest.aspx'
'
'==========================================================================================
Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
Public Enum ShiftKeys
KeyLeftShift = 1
KeyRightShift = 2
KeyLeftCtrl = 4
KeyRightCtrl = 8
KeyLeftAlt = 16
KeyRightAlt = 32
KeyShift = 64 ' Common ones aren't really necessary,
KeyCtrl = 128 ' but are included for completeness.
KeyAlt = 256 '-------------------------------------
End Enum
Private Const VK_LSHIFT As Long = &HA0 ' 160
Private Const VK_RSHIFT As Long = &HA1 ' 161
Private Const VK_LCTRL As Long = &HA2 ' 162
Private Const VK_RCTRL As Long = &HA3 ' 163
Private Const VK_LALT As Long = &HA4 ' 164
Private Const VK_RALT As Long = &HA5 ' 165
'
' vbKeyShift = &H10 ' 16
' vbKeyControl = &H11 ' 17
' vbKeyMenu = &H12 ' 18
'------------------------------------------------
Public Function CheckKeyState() As ShiftKeys
CheckKeyState = 0
If GetKeyState(VK_LSHIFT) < 0 Then CheckKeyState = CheckKeyState + KeyLeftShift
If GetKeyState(VK_RSHIFT) < 0 Then CheckKeyState = CheckKeyState + KeyRightShift
If GetKeyState(VK_LCTRL) < 0 Then CheckKeyState = CheckKeyState + KeyLeftCtrl
If GetKeyState(VK_RCTRL) < 0 Then CheckKeyState = CheckKeyState + KeyRightCtrl
If GetKeyState(VK_LALT) < 0 Then CheckKeyState = CheckKeyState + KeyLeftAlt
If GetKeyState(VK_RALT) < 0 Then CheckKeyState = CheckKeyState + KeyRightAlt
'=====
If GetKeyState(vbKeyShift) < 0 Then CheckKeyState = CheckKeyState + KeyShift
If GetKeyState(vbKeyControl) < 0 Then CheckKeyState = CheckKeyState + KeyCtrl
If GetKeyState(vbKeyMenu) < 0 Then CheckKeyState = CheckKeyState + KeyAlt
End Function