How to put focus to textbox as needed

后端 未结 3 1622
北恋
北恋 2021-01-23 05:20

I have a textbox on a userform. If the user fails to enter anything in this textbox, I need to trap that to force an entry. I can do this easily enough, but after notifying the

相关标签:
3条回答
  • 2021-01-23 05:56

    The other answers seem really complicated. I had a similar problem and really wanted a text warning. It seemed easier for me to just make an invisible label on the form that would show up if the input was incorrect. I also made the background of the label red so that the user would notice something was wrong. Doing it this way kept the cursor visible and right where they left off.

    Public Function amount(ByRef cont As MSForms.TextBox) As Integer
    'makes sure that a number is used
    'could change to account for decimals if necessary
    Dim i As Long
    On Error Resume Next
    i = 0
    If (cont.Value = "") Then Exit Function
        Do While i < 1000000
            If (cont.Value = i) Then
                UserForm1.Label257.Visible = False
                Exit Function
            End If
            i = i + 1
        Loop
    UserForm1.Label257.Visible = True
    amount = 1
    End Function
    
    Public Sub qty_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
    If amount(UserForm1.qty) = 1 Then
        Cancel = True
    End If
    End Sub
    

    I hope this helps other who run into this problem later on.

    0 讨论(0)
  • 2021-01-23 06:11

    Looking at the above code, I assume the i counter is to keep it going? Sorry a bit rusty, been a few years since I've done code.

    At any rate, if thats the case you could always run it while i=0, do (or while true).

    Sorry, first time posting here, hope that made sense.

    0 讨论(0)
  • 2021-01-23 06:20

    Why are you not using an 'ok' button to complete the action?

    You should not bother users with messages while they are typing in a form. Do it at the end.

    Private Sub OK_Click()
    
        '// Validate form
        If txtAnswer.Text = vbNullString Then
            MsgBox "You need to enter an answer!", vbExclamation, "No Answer Found!"
            txtAnswer.SetFocus
            Exit Sub
        End If
    
        '// You have reached here so form is correct carry on
        recordAnswer
    
    End Sub
    

    If you really want to use the behaviour you asked for then try this:

    Private Sub txtAnswer_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    
        Select Case KeyCode
        Case 13:
            If Me.txtAnswer.Value = "" Then
                temp = MsgBox("You need to enter an answer!", vbCritical + vbOKOnly, "No Answer Found!")              
                KeyCode = 0
            Else
                recordAnswer
            End If
        End Select
    
    End Sub
    

    The problem is that in your code you are setting focus but the enter key is firing afterwards. You don't need to set focus because the textbox already has the focus you just need to cancel the enter key.

    0 讨论(0)
提交回复
热议问题