Code Examples

Using Shortcut Keys

acCmdSizeToGrid, acCmdAlignBottom, acCmdAlignLeft, acCmdAlignRight, acCmdAlignTop, acCmdAlignToGrid, acCmdAlignToShortest, acCmdAlignToTallest, acCmdSizeToWidest, acCmdSizeToNarrowest

In his book Access 97 Expert Solutions, Stan Leszynski suggests using AutoKeys macros to add shortcut keys to Access that can be used in the design of the form. He was using RunCommand Macro actions but I have adapted the idea to use code instead. This allows you to add error trapping.

You need to set up an Autokeys Macro with the Shortcuts, you wish to use. Set the action for each one to RunCode and then in the Function Name action argument enter MacroKey (X) where X is the number in the select case statement in the code.

'***************** Code Start *******************
  Function MacroKey(intRequire As Integer)
  ' Based on an idea in
  ' Access 97 Expert Solutions by Stan Leszynski
      On Error GoTo ErrHandler
      Select Case intRequire
          Case 1 'Size to Grid
              DoCmd.RunCommand acCmdSizeToGrid
          Case 2 'Align Bottoms
              DoCmd.RunCommand acCmdAlignBottom
          Case 3 'Align Lefts
              DoCmd.RunCommand acCmdAlignLeft
          Case 4 'Align rights
              DoCmd.RunCommand acCmdAlignRight
          Case 5 'align tops
              DoCmd.RunCommand acCmdAlignTop
          Case 6 'align to grid
              DoCmd.RunCommand acCmdAlignToGrid
          Case 7 'Align to shortest
              DoCmd.RunCommand acCmdAlignToShortest
          Case 8 'Align to tallest
              DoCmd.RunCommand acCmdAlignToTallest
          Case 9 'Align to widest
              DoCmd.RunCommand acCmdSizeToWidest
          Case 10 'Align to shortest
              DoCmd.RunCommand acCmdSizeToNarrowest
      End Select
      Exit Function
  ErrHandler:
      Select Case Err.Number
          Case 2046
              'Not available at this time
              'Ignore
          Case Else
              MsgBox Err.Number & vbCrLf & Err.Description
      End Select
  End Function
  '****************** Code End ********************

© 1998 - 2011 Terry Wickenden TKW Design Site developed, maintained and hosted by TKW Design. This site is best viewed at 1024 x 768. Optimised for Firefox.