Full Version: Combination Keys In Vba
UtterAccess Forums > Microsoft® Access > Access Forms
accesshawaii
Is there a way to programatically set key combinations e.g. (Alt+A) to perform a certain action such as going to a new record? I just really need a way to identify that a certain key combination was pressed and then the rest I can handle. Any assistance would be appreciated.
theDBguy
Hi Dan,
Is that the same thing as "Auto Keys?"
Just curious...
accesshawaii
Well, kind've I guess. I know it's simple enough to do with single keys but I need it to be where for example if the user presses the key combination of Alt+A then it will do some type of action. Does that make sense?
jleach
Set the KeyPreview property of the Form to Yes. Use the KeyDown event to check the code logic - this has KeyCode (the key being pressed) and Shift (the ctrl/alt/shift) arguments that you use to find out what the keystroke(s) were.
o check the Shift you use the acCtrlMask acShiftMask and acAltMask constants - use binary And operator and if the result is <> 0 the key was held.
CODE
WasShiftPressed = ((Shift and acCtrlShiftMask) <> 0)
WasAltPressed = ((Shift And acAltMask) <> 0)
WasCtrlPressed = ((Shift And acCtrlMask) <> 0)

If you do find a keycode combination you want to act on, don't forget to set KeyCode to 0 so it doesn't continue processing the keypress in addition to your code.
Example using Ctrl+Insert to go to new record:
CODE
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  If KeyCode = vbKeyInsert Then
    If ((Shift And acCtrlMask) <> 0) Then
      KeyCode = 0
      DoCmd.GotoRecord, acDataForm, Me.Name, acNewRec
    End If
  End If
End Sub

(ref: VB Key Code Constants)
That should do...
niesz
If all you need is ALT-<something> functionality, you can use a command button and put an ampersand in the text of the button.
For example, a button with this text:
&Next Record
...will cause the "N" to be underlined when viewing the button, and if the user presses ALT-N on the keyboard, the button will be pressed.
You can use the ampersand within the text, like:
Next &Record
This will cause ALT-R to be wired to the button press.
If you don't want to see the button, I believe it doesn't even have to be visible for it to work, IIRC.
accesshawaii
Thanks for the responses, guys. Lots of good ideas. I'll try them out.
theDBguy
Hi Dan,
ere's an MS Article on the AutoKeys Macro Group just in case it helps:
Run an Access macro by using a keyboard shortcut
Cheers!
jleach
Just for the record, Alt is the only standard key that can't be captured using the AutoKeys macro (ctrl and shift are fine though) - I just got done standardizing a keyboard UI and was looking it over trying to capture and quell the Alt+F4.
accesshawaii
Thanks for all the replies. If "Alt" isn't really an option or more trouble than it's going to be worth then that's not a big deal. I can use a combination of say Shift+A. I'm going to need to know when that combination is pressed though. I just looked through the existing module and the last field in the main form has a procedure to set the focus to the subform when the user leaves the field. So, what happens is if the user is in that last field and presses they key combination for the next record, it takes them to the subform instead. Any other field they're in, it works fine, it's just that one field. So, I would need something like the psuedo code below.
!--c1-->
CODE
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  If KeyCode = vbShift + A Then
         Go to Next Record
  Else
        MySubform Gets the Focus
  End If

If someone could assist me on how to put that logic into VBA, I'd appreciate it. Thanks.
jleach
Try this:
!--c1-->
CODE
If (KeyCode = vbKeyA) And ((Shift And acAltMask) <> 0) Then
  'alt+A was pressed
  KeyCode = 0
  DoCmd.GotoRecord acDataForm, Me.Name, acNewRec
Else
  KeyCode = 0
  Me.SubformControl.SetFocus
End If

Alt doesn't work in the AutoKeys macro, which IMO isn't too big a deal. You use the AutoKeys macro to assign keystrokes that are application-wide: not tied to any specific control or form. In your case, you have an situation you want to manage at the control level, Alt is fine, and if you want to manage it at Form level, set the form's KeyPreview property to True and use the Key events just as you would as shown above. No reason for an AutoKeys macro.
You may think hey, I want to do this on every form, so why not an AutoKeys macro so I don't have to code it every time?. Because AutoKeys will run even on a report or if you have a table or query open or whatever, I would advise against that. Instead I would create a public function to handle it and you can call that function from every form (I actually use a class module for this and other form-specific functions for my "core" forms).
Whenver you have some thing that happens at basically any given event (such as pressing any key on the keyboard) and that will run across the entire application no matter where the use happens to be, you need to give some deep consideration to making sure you won't be doing anything at all that might somehow interfere with whatever task the user might be doing at the time.
Anyway, AutoKeys, IMHO has very few practical uses. There's no problem using the Alt+Key combination in any other scenario, so feel free.
hth
jleach
Come to think of it, for performance reasons, I'll actuall put the Shift Code check inside the original KeyCode check so there's only one evaluation being performed (the A key) when every other key is pressed. The gain is likely negligable, but I do it anyway:
!--c1-->
CODE
If KeyCode = vbKeyA Then
  If ((Shift And acAltMask) <> 0) Then
    DoCmd.GotoRecord, , acNewRec
  End If
Else
  Me.SubformControl.SetFocus
End If
accesshawaii
Hi Jack,
Thanks for all that. Are you doing that in the "KeyDown" event? It's not picking up "Alt+A" when I'm doing it. I got something else working in the "KeyPress" event using ASCII, which has the actual values for combinations but I can't use the "Alt" key with that, so I'd much rather use your example if I can get it working. I'm sure I'm probably just doing something wrong on my end.
jleach
That is the KeyDown. The event should provide KeyCode and Shift arguments.
The code example should work... is it tied to a Control event or the Form's KeyDown event? If it's tied to the form's event you'll need to set the KeyPreview form property to True (in 2003 at least this property is listed a right below the Key events in the Events tab).
Note the event will fire twice making it upractical to step through the procedure: once for the initial Alt Down/Hold, and again when you press A (which is where the code should reset the key and move to the record). You can test by putting a Debug Print in there, Shift should be a nonzero number when the A key is pressed with the Alt/Ctrl or Shift key pressed as well.
hth
This is a "lo-fi" version of UA. To view the full version with more information, formatting and images, please click here.