title: Excel Userform Textbox Behavior
body: <p>I have a textbox on a userform. It is the only textbox on the form. There are three labels and two buttons in addition to this textbox. Basically, I want the focus to remain on this textbox under all scenarios, other than the moment that one of the buttons would be clicked, but then I want the focus to come right back to the text box. Both buttons have "TakeFocusOnClick" and "TabStop" set to False.  I was having problems with getting the focus set to the textbox, which is why I changed these two settings.</p>
  
  <p>Once I changed these settings, the Enter key in the textbox stopped having any effect. I have events written for _AfterUpdate and _KeyPress for the textbox, but they don't fire. As you can see in the code, I have commented out the lines to set the focus to this textbox. Since it is now the only object that can take focus, these lines are not needed (theoretically). When I allowed the other objects to take focus, these lines weren't having any effect (focus was switching to the buttons despite these SetFocus lines).</p>
  
  <p>Here is the code. It is very simple, except that the Enter key isn't triggering the event. Can anyone see why?  Thanks.</p>
  
  <pre><code>Private Sub btnDone_Click()
      Application.Calculation = xlCalculationAutomatic
      formMath.Hide
  
      'Clear statistics
      Range("attempts").Value = 0
      Range("correct").Value = 0
      Sheet5.Range("A2:W500").ClearContents
  
  End Sub
  
  
  Private Sub btnSubmit_Click()
      recordAnswer
      'formMath.txtAnswer.SetFocus
  End Sub
  
  Private Sub txtAnswer_AfterUpdate()
      recordAnswer
      'formMath.txtAnswer.SetFocus
  End Sub
  
  Private Sub txtAnswer_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
      If KeyAscii = 13 Then
          recordAnswer
      End If
  End Sub
  
  Private Sub UserForm_Initialize()
  
      'Initialize manual calculation
      Application.Calculation = xlCalculationManual
      Application.Calculate
  
      'Initialize statistics
      Range("attempts").Value = 0
      Range("correct").Value = 0
      Sheet5.Range("A2:W500").ClearContents
  
      'Initialize first problem
      newProblem
  
  End Sub
  
  Sub recordAnswer()
  
      'Update statistics
      Dim attempts, correct As Integer
      attempts = Range("attempts").Value
      correct = Range("correct").Value
  
      Range("results").Offset(attempts, 0).Value = attempts + 1
      Range("results").Offset(attempts, 1).Value = lblTopNum.Caption
      Range("results").Offset(attempts, 2).Value = lblBotNum.Caption
      Range("results").Offset(attempts, 3).Value = lblBop.Caption
      Range("results").Offset(attempts, 4).Value = Range("Answer").Value
      Range("results").Offset(attempts, 5).Value = txtAnswer.Text
  
      If (Range("Answer").Value = txtAnswer.Text) Then
          Range("results").Offset(attempts, 6).Value = 1
      Else
          Range("results").Offset(attempts, 6).Value = 0
      End If
  
      'Update attempts and success
      Range("attempts").Value = attempts + 1
      Range("correct").Value = correct + 1
  
      newProblem
  
  End Sub
  
  Sub newProblem()
  
      Application.Calculate
      formMath.lblTopNum.Caption = Range("TopNum").Value
      formMath.lblBotNum.Caption = Range("BotNum").Value
      formMath.lblBop.Caption = Range("ProbType").Value
      formMath.txtAnswer.Value = ""
      'formMath.txtAnswer.SetFocus
  
  End Sub
  </code></pre>