none
SetFocus RRS feed

  • Question

  • Hello ,

    why I do not get my Cursor again with SetFocus ? did anyone know why?

    Private Sub TextBox1_Change()
    Dim Text As String
    Dim i As Integer     
    Text = TextBox1.text  
    For i = 1 To Len(Text)
                          
    If Mid(Text, i, 1) Like "[!A-Za-z]" = True Then
    MsgBox ("Please enter a letter!")
    Text = Replace(Text, Mid(Text, i, 1), "")
    UserForm1.TextBox1.text = Text
    UserForm1.TextBox1.SetFocus
            End If
        Next
    End Sub

    Best regards,

    Neven



    • Edited by Req_En Monday, July 2, 2018 12:00 PM
    Monday, July 2, 2018 11:47 AM

Answers

  • The following should work

    Private Sub TextBox1_Change()
    Dim Text As String
    Dim i As Integer
        Text = TextBox1.Text
        For i = 1 To Len(Text)
            If Mid(Text, i, 1) Like "[!A-Za-z]" = True Then
                MsgBox ("Please enter a letter!")
                Text = Replace(Text, Mid(Text, i, 1), "")
                TextBox1.Text = Text
                TextBox1.SetFocus
            End If
        Next
    End Sub

    Personally I would use the following to validate the text box entry, which is easily edited to accommodate any choice of character.

    Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    Dim bTest As Boolean
        bTest = IsAllowed(CStr(KeyAscii))
        If bTest = False Then
            Beep
            MsgBox "The character '" & Chr(KeyAscii) & "' is not permitted. Letters only allowed."
            KeyAscii = 0
        End If
    lbl_Exit:
        Exit Sub
    End Sub
    
    Private Function IsAllowed(ByVal i As String) As Boolean
        Select Case Val(i)
            Case 65 To 90, 97 To 122
                IsAllowed = True
            Case Else
                IsAllowed = False
        End Select
    lbl_Exit:
        Exit Function
    End Function


    Graham Mayor - Word MVP
    www.gmayor.com

    • Marked as answer by Req_En Wednesday, July 4, 2018 9:16 AM
    Monday, July 2, 2018 12:55 PM

All replies

  • The following should work

    Private Sub TextBox1_Change()
    Dim Text As String
    Dim i As Integer
        Text = TextBox1.Text
        For i = 1 To Len(Text)
            If Mid(Text, i, 1) Like "[!A-Za-z]" = True Then
                MsgBox ("Please enter a letter!")
                Text = Replace(Text, Mid(Text, i, 1), "")
                TextBox1.Text = Text
                TextBox1.SetFocus
            End If
        Next
    End Sub

    Personally I would use the following to validate the text box entry, which is easily edited to accommodate any choice of character.

    Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    Dim bTest As Boolean
        bTest = IsAllowed(CStr(KeyAscii))
        If bTest = False Then
            Beep
            MsgBox "The character '" & Chr(KeyAscii) & "' is not permitted. Letters only allowed."
            KeyAscii = 0
        End If
    lbl_Exit:
        Exit Sub
    End Sub
    
    Private Function IsAllowed(ByVal i As String) As Boolean
        Select Case Val(i)
            Case 65 To 90, 97 To 122
                IsAllowed = True
            Case Else
                IsAllowed = False
        End Select
    lbl_Exit:
        Exit Function
    End Function


    Graham Mayor - Word MVP
    www.gmayor.com

    • Marked as answer by Req_En Wednesday, July 4, 2018 9:16 AM
    Monday, July 2, 2018 12:55 PM
  • Hello Graham,

    with this code

    Private Sub TextBox1_Change()
    Dim Text As String
    Dim i As Integer
        Text = TextBox1.Text
        For i = 1 To Len(Text)
            If Mid(Text, i, 1) Like "[!A-Za-z]" = True Then
                MsgBox ("Please enter a letter!")
                Text = Replace(Text, Mid(Text, i, 1), "")
                TextBox1.Text = Text
                TextBox1.SetFocus
            End If
        Next
    End Sub

    I still could not retern my Cursor !

    Monday, July 2, 2018 2:00 PM
  • It works here (Word 2016/Windows 10 latest builds) Replace it with the suggested alternative and see how you get on with that.

    Graham Mayor - Word MVP
    www.gmayor.com

    Monday, July 2, 2018 2:30 PM
  • Hello Graham ,

    Thank u for your reply , I need this code in Excel did u try it in Excel also ?

    Best regards,

    Neven


    • Edited by Req_En Tuesday, July 3, 2018 9:13 AM
    Tuesday, July 3, 2018 6:36 AM
  • There is no inherent difference between userform textboxes in Word and Excel

    Both options work in Excel.

    Without access to your userform and its associated code it is impossible to establish if there are other issues in play that might be causing the anomaly.


    Graham Mayor - Word MVP
    www.gmayor.com

    Tuesday, July 3, 2018 12:11 PM