Results 1 to 12 of 12
  1. #1
    Join Date
    Feb 2009
    Posts
    28

    Unanswered: Shortcut or Macro Key

    I'd like to assign a "F" key to automatically put some text into a memo field of a form.

    Is that even possible?

  2. #2
    Join Date
    Nov 2007
    Location
    Adelaide, South Australia
    Posts
    4,049
    Probably unwise. What happens if someone needs to type an F into a different field?

    It is more typical to make a button with a shortcut key of ALT-F and have the command button do what you want it to do. I think you can even make that button transparent, meaning that ALT-F would do what you want via the command button, but your user will never see that command button.
    Owner and Manager of
    CypherBYTE, Microsoft Access Development Specialists.
    Microsoft Access MCP.
    And all around nice guy!


    "Heck it's something understood by accountants ... so it can't be 'that' difficult..." -- Healdem
    "...teach a man to code and he'll be frustrated for life! " -- georgev

  3. #3
    Join Date
    Feb 2009
    Posts
    28
    Oops, sorry for not being too clear. I'm kinda new at this. I was referring to a key such as "f5".

    ALT-F would probably work too.

    I have another question too based upon user security but will make another post for it.

  4. #4
    Join Date
    Nov 2007
    Location
    Adelaide, South Australia
    Posts
    4,049
    Trapping F5 is also possible, it would be done by intercepting each keypress on the form and testing it for ... whatever value F5 is.

    I can probably look it up and get more specific if you want to... or if you go with ALT-F and do it via the command button method, it's up to you.
    Owner and Manager of
    CypherBYTE, Microsoft Access Development Specialists.
    Microsoft Access MCP.
    And all around nice guy!


    "Heck it's something understood by accountants ... so it can't be 'that' difficult..." -- Healdem
    "...teach a man to code and he'll be frustrated for life! " -- georgev

  5. #5
    Join Date
    Feb 2009
    Posts
    28
    How would I program the Alt-F command button?

  6. #6
    Join Date
    Feb 2004
    Posts
    25

    Trapping KeyDown events

    When you select Alt- keys make sure you don't override your menu and command button shortcut keys. I prefer Function keys to avoid any conflicts. Just add code to your fields_KeyDown event. I like to clear the Keycode and Shift values on keys I trap to prevent any other events from triggering. Be wary of "SendKeys" because it sends to the active window, but since you just entered this Event Code, this Application should still be the active one. Also Sendkeys will insert text at the cursor (it replaces any selected text) or you can equate the field to replace it with your string. Also, Sendkeys will generate new KeyDown events with each character sent, so make sure you don't get into a nasty loop that inserts forever!! Both approaches are illustrated...

    If KeyCode = vbKeyF5 Then
    KeyCode = 0
    ' Me.Notes = "replaces F5 text"
    SendKeys "inserts text F5 at cursor"
    Exit Sub
    End If

    If Shift = acAltMask Then
    Select Case KeyCode
    Case vbKeyA
    Shift = 0
    KeyCode = 0
    ' Me.Notes = "replaces A text"
    SendKeys "inserts text A at cursor"
    Exit Sub
    Case vbKeyF
    Shift = 0
    KeyCode = 0
    Me.Notes = "replaces text F"
    ' SendKeys "inserts F text at cursor"
    Exit Sub
    End Select
    End If

    Good Luck!
    Glenn

  7. #7
    Join Date
    Feb 2009
    Posts
    28
    Nice! I'll play with it later and post back the results. Thanks for the help.

  8. #8
    Join Date
    Jun 2005
    Location
    Richmond, Virginia USA
    Posts
    2,763
    Provided Answers: 19
    In Form Design View, goto Properties - Events and set Key Preview to Yes. Then use this code:

    Code:
    Private Sub form_keydown(KeyCode As Integer, Shift As Integer)
    Select Case KeyCode
     Case vbKeyF5
      KeyCode = 0
      If IsNull(Me.YourMemoField) Then
       Me.YourMemoField = "Your text to insert"
      Else
       Me.YourMemoField = Me.YourMemoField & " " & "Your text to insert"
      End If
    End Select
    End Sub
    Substitute your actual name for the memo field, and whatever "F" key you want. Substitue the text you want inserted.

    It's always a good idea to think carefully before re-assigning an "F" key. You don't want to re-assign one that the end users might need to use, such as F1 for Help.

    SendKeys is problematic and really should be avoided at all costs, unless there is absolutely no other alternative avaialible.
    Hope this helps!

    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  9. #9
    Join Date
    Feb 2009
    Posts
    28
    Whoa! That was slick. Thanks a lot.

  10. #10
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Personally I'd run with an AutoKeys Macro.

    Handling keystrokes with an AutoKeys macro
    George
    Home | Blog

  11. #11
    Join Date
    Feb 2009
    Posts
    28
    I got the other code to work but really like the link you sent and bookmarked it for later use.

    Thanks.

  12. #12
    Join Date
    Nov 2007
    Location
    Adelaide, South Australia
    Posts
    4,049
    AutoKeys would be fine if it was a GLOBAL shortcut, but this is for one single form only, so yeah, I think in this case, AutoKeys is a poor choice.
    Owner and Manager of
    CypherBYTE, Microsoft Access Development Specialists.
    Microsoft Access MCP.
    And all around nice guy!


    "Heck it's something understood by accountants ... so it can't be 'that' difficult..." -- Healdem
    "...teach a man to code and he'll be frustrated for life! " -- georgev

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •