By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,401 Members | 793 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,401 IT Pros & Developers. It's quick & easy.

KeyDown event doesn't occur for Text Box

P: 12
I have a form with several combo boxes in the header used to determine the records that will be displayed on the form. When I tab or enter from the last combo box in the header, I want the focus to move to the first control on the form. When I Shift+tab from the first control on the form, I want the focus to move to the last control in the header. The form is a continuous form.

The first control on the form is either a text box or a combo box, depending on whether you are entering a new record (the combo box is visible only when entering a new record). The problem is that the KeyDown event of the Text Box never occurs when I press a key and the TextBox appears to have focus.

Other relevant information: The SCI_Name_TextBox and computerComboBox are tab stops, but SCINameComboBox is not. All three are Enabled and Not Locked.

Any ideas as to why this event is not getting triggered? Is there an alternative way to capture the Shift+Tab from the text box?

Following is my relevant code:

Expand|Select|Wrap|Line Numbers
  1. 'This is the text box on top of the combo box in the detail section of the form.
  2. ' This event never gets triggered, so this code never runs.
  3. Private Sub SCI_Name_TextBox_KeyDown(KeyCode As Integer, Shift As Integer)
  4.  
  5.  ' If user presses SHIFT+TAB on the first row....
  6.    If (KeyCode = vbKeyTab) And ((Shift And acShiftMask) > 0) Then
  7.             'only if we're on the first row...
  8.             If Me.CurrentRecord = 1 Then
  9.                 ' Disable the keystroke by setting it to 0
  10.                 KeyCode = 0
  11.                 'Set the focus to computer combo box
  12.                 Me.computerComboBox.SetFocus
  13.             End If
  14.  
  15.    End If
  16.  
  17. End Sub
  18.  
  19. 'if the combo box is visible, then go ahead and set focus to it when you enter
  20. 'the text box
  21. 'I only included this because it's possibly related to the problem
  22. 'this appears to work correctly though....
  23. Private Sub SCI_Name_TextBox_Enter()
  24.     If Me.SCINameComboBox.Visible Then
  25.         Me.SCINameComboBox.SetFocus
  26.     End If
  27. End Sub
  28.  
  29. 'This is the combo box underneath the text box.  This event gets triggered
  30. 'when a key is pressed and works like a champ.  (focus goes to the form
  31. 'header when you shift+tab from the first record, goes to the last control
  32. 'from the previous record otherwise)
  33. Private Sub SCINameComboBox_KeyDown(KeyCode As Integer, Shift As Integer)
  34.  
  35.  ' If user presses SHIFT+TAB on the first row....
  36.    If (KeyCode = vbKeyTab) And ((Shift And acShiftMask) > 0) Then
  37.             'only if we're on the first row...
  38.             If Me.CurrentRecord = 1 Then
  39.                 If ((Shift And acShiftMask) > 0) Then
  40.                     ' Disable the keystroke by setting it to 0
  41.                     KeyCode = 0
  42.                     'Set the focus to computer combo box
  43.                     Me.computerComboBox.SetFocus
  44.                 End If
  45.             End If
  46.  
  47.     Else
  48.  
  49.         Select Case KeyCode
  50.             ' If user presses TAB, ENTER
  51.             Case 13, 9
  52.                 ' Disable the keystroke by setting it to 0
  53.                 KeyCode = 0
  54.  
  55.                 'Set the focus to the version drop down
  56.                 Me.SCIDescriptionComboBox.SetFocus
  57.             Case Else
  58.                 Debug.Print KeyCode, Shift
  59.         End Select
  60.  
  61.     End If
  62. End Sub
  63.  
  64. 'combo box in the form header
  65. 'captures and ignores the error 2110 (couldn't set focus to the text box)
  66. 'because the set focus appears to work even though an error is thrown. 
  67. 'not the best solution.. but this may be related to the other problem I'm seeing
  68. Private Sub computerComboBox_KeyDown(KeyCode As Integer, Shift As Integer)
  69. On Error GoTo Err_computerComboBox_KeyDown
  70.     Select Case KeyCode
  71.         ' If user presses TAB, ENTER
  72.         Case 13, 9
  73.             If Not IsNull(Me.computerComboBox) Then
  74.                 ' Disable the keystroke by setting it to 0
  75.                 KeyCode = 0
  76.  
  77.                 'Set the focus to the first control in the detail view
  78.                 Me.SCI_Name_TextBox.SetFocus
  79.             End If
  80.         Case Else
  81.             Debug.Print KeyCode, Shift
  82.     End Select
  83. Exit_computerComboBox_KeyDown:
  84.     Exit Sub
  85.  
  86. Err_computerComboBox_KeyDown:
  87.     If Not (Err.Number = 2110) Then
  88.         MsgBox Err.Description
  89.     End If
  90.     Resume Exit_computerComboBox_KeyDown
  91.  
  92. End Sub
  93.  
Thanks in advance for any ideas!

Dana
Mar 14 '07 #1
Share this Question
Share on Google+
1 Reply


P: 12
Sheepishly... I must admit that only moments after posting, I thought of one more thing to try. The event procedure, for whatever reason, didn't get hooked up properly to the text box. So, I went into my text box properties and set the onKeyDown property to [Event Procedure] and everything is great.

Sorry!

Although if anyone has any ideas about that 2110 error.. feel free to respond :-)

Dana
Mar 14 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.