Results 1 to 11 of 11
  1. #1
    Remster is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Sep 2010
    Posts
    317

    Keydown and SendKeys

    Hi Folks

    I have a control, which I'll call 'MyField', on a form. I also have code for the control, which I'll simplify to this:
    Code:
     
    Private Sub MyField_GotFocus()
     
        SendKeys "{F2}"
     
    End Sub
    Code:
     
    Private Sub MyField_KeyDown(KeyCode As Integer, Shift As Integer)
     
        If Not (KeyCode = vbKeyF2 Or KeyCode = vbKeyTab) Then MsgBox "You can't do that." 
     
    End Sub
    What's puzzling me is this: when MyField receives the focus by tabbing, the message box appears. So the SendKeys command is triggering the KeyDown event, as required, but there's no recognition that the key in question is F2 (or Tab). What am I doing wrong?

    Thanks

    Remster

  2. #2
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    Why are you using SendKeys and F2? Are you wanting to select the entire field? That's what I seem to get when entering a control and hitting F2. So you can do that with code by using:
    Code:
    Me.TextBoxNameHere.SelStart = 0
    Me.TextBoxNameHere.SelLength = Len(Me.TextBoxNamehere)

  3. #3
    Remster is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Sep 2010
    Posts
    317
    Yes, that's exactly what I want to do (select the entire field on entry), so I'll try your suggestion later. Thanks.

    Here's a very closely related case that I'm struggling with:
    Code:
     
    Private Sub OptionButton_KeyDown(KeyCode As Integer, Shift As Integer)
     
        If KeyCode = vbKeyRight Then
            SendKeys "{TAB}"
            DoCmd.CancelEvent
        End If
     
        If Not (KeyCode = vbKeyRight Or KeyCode = vbKeyTab) Then MsgBox "You can't do that." 
     
    End Sub
    What I'm trying to achieve here is to make the cursor keys move to the next field rather than scroll through the options. I can't (or at least don't want to) use SetFocus, because the next enabled field will vary depending on a range of conditions. Is there a way to say something like 'move to the next enabled field'?

    If I can solve this, I shan't need to worry about the SendKeys issue.

  4. #4
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    Quote Originally Posted by Remster View Post
    What I'm trying to achieve here is to make the cursor keys move to the next field rather than scroll through the options.
    I don't get that. Cursor keys? Scroll through the options? What does this stuff mean??

    Quote Originally Posted by Remster View Post
    I can't (or at least don't want to) use SetFocus, because the next enabled field will vary depending on a range of conditions. Is there a way to say something like 'move to the next enabled field'?
    Of course there is, change the keycode to TAB, which is 9. so in your code, if you want to move to the next enabled field if the user presses Right arrow or tab, maybe write:

    Code:
    Private Sub OptionButton_KeyDown(KeyCode As Integer, Shift As Integer)
     
        If KeyCode = 27 or KeyCode = 9 Then
            KeyCode = 9
        else 
            MsgBox "You can't do that." 
        end if
    
    End Sub
    what does docmd.cancelEvent do? Never used it.

  5. #5
    Remster is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Sep 2010
    Posts
    317
    Quote Originally Posted by ajetrumpet View Post
    I don't get that. Cursor keys? Scroll through the options? What does this stuff mean??
    Sorry, perhaps that was too British and too elliptical.
    Cursor keys = arrow keys

    Scroll through the options = move the focus from the first option to the second, then the third, then the fourth, then back to the first, and so on
    Perhaps this would have been clearer: 'What I'm trying to achieve here is to make the arrow keys move the focus to the next control rather than to the next option button in the option group.'


    Of course there is, change the keycode to TAB, which is 9. so in your code, if you want to move to the next enabled field if the user presses Right arrow or tab, maybe write:
    Code:
    Private Sub OptionButton_KeyDown(KeyCode As Integer, Shift As Integer)
     
    If KeyCode = 27 or KeyCode = 9 Then
    KeyCode = 9
    else 
    MsgBox "You can't do that." 
    end if
     
    End Sub
    Thanks, I'll give that a try.


    what does docmd.cancelEvent do? Never used it.
    I think (it's several months since I wrote that code) it cancels the normal behaviour of the arrow key. So if I have just ...
    Code:
     
    If KeyCode = vbKeyRight Then
        SendKeys "{TAB}"
    End If
    ... I get the normal arrow key behaviour plus the tab key behaviour. But if I have ...
    Code:
     
    If KeyCode = vbKeyRight Then
        SendKeys "{TAB}"
        DoCmd.CancelEvent
    End If
    ... I get just the tab key behaviour. I hope this isn't Martian.

  6. #6
    Remster is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Sep 2010
    Posts
    317
    Okay, this works:
    If KeyCode = vbKeyRight Or KeyCode = vbKeyDown Then KeyCode = vbKeyTab
    Now what I need to know is how to simulate Shift+Tab for the other two arrow keys:
    If KeyCode = vbKeyLeft Or KeyCode = vbKeyUp Then KeyCode = vbKeyTab ... ?
    Any ideas? I've tried a variety of things, none of which has worked.

  7. #7
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    Quote Originally Posted by Remster View Post
    [/INDENT]Now what I need to know is how to simulate Shift+Tab for the other two arrow keys:
    If KeyCode = vbKeyLeft Or KeyCode = vbKeyUp Then KeyCode = vbKeyTab ... ?
    Any ideas? I've tried a variety of things, none of which has worked.
    for that you'll have to use sendkeys, and change the keycode to 0. using shift with other keys is in the sendkeys helpfile.

  8. #8
    Remster is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Sep 2010
    Posts
    317
    Damn, then it's back to Square One (see my original post). Thanks for your help though.

  9. #9
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    Quote Originally Posted by ajetrumpet View Post
    for that you'll have to use sendkeys, and change the keycode to 0. using shift with other keys is in the sendkeys helpfile.
    Wrong. You don't need SendKeys. You can do it like this:
    Code:
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
        Dim intTI  As Integer
        Dim ctl    As Control
        On Error GoTo Errors
        If KeyCode = vbKeyRight Then
            intTI = Me.Controls(Me.ActiveControl.Name).TabIndex + 1
            For Each ctl In Me.Controls
                '            On Error Resume Next
                If ctl.TabIndex = intTI Then
                    ctl.SetFocus
                    ctl.SelStart = 0
                    ctl.SelLength = Len(ctl.Text)
                End If
    GTNext:
                
            Next
        End If
        KeyCode = 0
    
    ExitHere:
        Exit Sub
    Errors:
        Select Case Err.Number
            Case 438, 3270
                Resume GTNext
            Case Else
                MsgBox "Error " & Err.Number & " - " & " (" & Err.Description & ") in procedure Form_KeyDown"
        End Select
        Resume ExitHere
        Resume
    End Sub

  10. #10
    Remster is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Sep 2010
    Posts
    317
    Thanks. I'll give that a try and let you know if I can get it to work.

  11. #11
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    Quote Originally Posted by Remster View Post
    Thanks. I'll give that a try and let you know if I can get it to work.
    Make sure that the FORM'S Key Preview property is set to YES as well.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Sendkeys replacement
    By numberguy in forum Programming
    Replies: 6
    Last Post: 07-14-2011, 08:29 AM
  2. Sendkeys replacement
    By numberguy in forum Forms
    Replies: 3
    Last Post: 10-29-2010, 07:20 AM
  3. docmd.SendKeys "{F9}" in 2007
    By RickM in forum Access
    Replies: 4
    Last Post: 09-10-2009, 09:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums