Search code examples
excelvbatextboxuserformsetfocus

Excel Userform Textbox Behavior


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.

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).

Here is the code. It is very simple, except that the Enter key isn't triggering the event. Can anyone see why? Thanks.

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

Solution

  • I found a way to accomplish this. In the code above I took out the _KeyPress and _AfterUpdate events and replaced them with:

    Private Sub txtAnswer_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
        Select Case KeyCode
            Case 13: recordAnswer
        End Select
    End Sub
    

    Not sure why the other methods didn't work, but this does.

    Also not sure why simply setting the focus directly didn't work. I suspect that the focus was being set, but then something else was happening subsequently that was changing the focus off of the textbox. Just a guess.

    Thanks for the help. I appreciate it.