How to put focus to textbox as needed

后端 未结 3 1623
北恋
北恋 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.

提交回复
热议问题