+ Reply to Thread
Results 1 to 12 of 12

Repeat Characters Appearing in a Text Box

Hybrid View

  1. #1
    Registered User
    Join Date
    10-24-2014
    Location
    Charlotte,NC
    MS-Off Ver
    2010
    Posts
    6

    Repeat Characters Appearing in a Text Box

    This is my first post so I'm bound to make some errors. Feedback appreciated.

    I have created code to populate the contents of a text box while simultaneously updating a label caption with search results from the entered text. The format of the worksheet looks like this:

    ID Number Group Name
    131010 Design Jones,J
    103005 Systems Smith,G
    146908 Programs Hunter,F

    The UserForm has a text box called NameText that can be selected by mouse click or tab from the previous field. My desired effect is that the user selects the NameText field and starts typing the Last name,First initial of the worker. As each character is typed, the function searches the database and returns the first occurrence of the name that starts with the entered text to an adjacent Label (labels 4 and 5). The code performs this function satisfactorily.

    When I run this code in Debug mode using the F8 key to advance one step at a time, the characters are displayed only once and everything works properly. However, when I run the routine from Run Sub/UserForm (F5), each typed character is displayed twice even though the first character is shown in the Watch as being the only character entered. Subsequent keystrokes then include the extra characters.

    My code to this point looks like this:
    Dim Location As Integer, EmployeeFound, StringFound As Boolean
    Private Sub NameText_Enter()
        NameText.Value = ""
        EmployeeIDText.Value = ""
    End Sub
    
    Private Sub NameText_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    ' Allow only letters and a comma
    Select Case KeyAscii
        Case Asc("a") To Asc("z")
        Case Asc("A") To Asc("Z")
        Case Asc(",")
        Case Else
            KeyAscii = 0
    End Select
    ' Increment the NameText value by the entered text
    NameText.Value = NameText.Value + Chr(KeyAscii)
    CurrentLength = Len(NameText.Value)
    If CurrentLength > 0 Then
        SearchText = NameText.Value
        With Sheets("Enrollment")
            Location = 1
            lastRw = .Range("A" & .Rows.Count).End(xlUp).Row
            StringFound = False
            Do While StringFound = False And Location < lastRw + 1
                Location = Location + 1
                CurrentNameString = Left$(Range("C" & Location).Value, CurrentLength)
                If CurrentNameString = SearchText Then
                    UserForm2.Label5.Caption = Range("C" & Location).Value
                    UserForm2.Label4.Caption = Range("A" & Location).Value
                    StringFound = True
                    EmployeeFound = True
                End If
            Loop
            If Location > lastRw Then
                UserForm2.Label4.ForeColor = &HFF&
                UserForm2.Label5.Caption = "Not Found"
                EmployeeFound = False
            End If
        End With
    End If
    End Sub
    Thank you for considering this issue.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Repeat Characters Appearing in a Text Box

    Can you upload an example workbook?

    Click on GO ADVANCED and use the paperclip icon to open the upload window.
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    10-24-2014
    Location
    Charlotte,NC
    MS-Off Ver
    2010
    Posts
    6

    Re: Repeat Characters Appearing in a Text Box

    I've attached a file with example data and my code. I appreciate the look.
    Example.xlsm

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Repeat Characters Appearing in a Text Box

    You should start of by removing this line of code.
      NameText.Value = NameText.Value + Chr(KeyAscii)
    That's basically adding the character you've just typed into the textbox to what's already in the textbox, so you end up with a duplicate.

    This also needs to go I think
       NameText.SelStart = 0
    That's moving the cursor back to the start of the textbox so the next character you press will appear before the previous character(s).

    Not 100% sure what this is doing but I don't think it's needed.
        If Len(NameText.Value) > 1 Then
            If Mid(NameText.Value, Len(NameText.Value), 1) = Mid(NameText.Value, Len(NameText.Value) - 1, 1) Then
                NameText.Value = Left$(NameText.Value, Len(NameText.Value) - 1)
            End If
        End If

    After making those changes the KeyPress event would look like this.
    Private Sub NameText_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    
        Select Case KeyAscii
            Case Asc("a") To Asc("z")
            Case Asc("A") To Asc("Z")
            Case Asc(",")
            Case Else
                KeyAscii = 0
        End Select
    
        CurrentLength = Len(NameText.Value)
        If CurrentLength > 0 Then
            SearchText = NameText.Value
            With Sheets("Enrollment")
                Location = 1
                lastRw = .Range("A" & .Rows.Count).End(xlUp).Row
                StringFound = False
                Do While StringFound = False And Location < lastRw + 1
                    Location = Location + 1
                    CurrentNameString = Left$(Range("C" & Location).Value, CurrentLength)
                    If CurrentNameString = SearchText Then
                        UserForm2.Label5.Caption = Range("C" & Location).Value
                        UserForm2.Label4.Caption = Range("A" & Location).Value
                        StringFound = True
                        EmployeeFound = True
                    End If
                Loop
                If Location > lastRw Then
                    UserForm2.Label4.ForeColor = &HFF&
                    UserForm2.Label5.Caption = "Not Found"
                    EmployeeFound = False
                End If
            End With
        End If
    
    End Sub

  5. #5
    Registered User
    Join Date
    10-24-2014
    Location
    Charlotte,NC
    MS-Off Ver
    2010
    Posts
    6

    Re: Repeat Characters Appearing in a Text Box

    Thanks for the look - that does fix the double character problem.

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Repeat Characters Appearing in a Text Box

    Is there another problem?

  7. #7
    Registered User
    Join Date
    10-24-2014
    Location
    Charlotte,NC
    MS-Off Ver
    2010
    Posts
    6

    Re: Repeat Characters Appearing in a Text Box

    Yes. KeyPress is supposed to respond to a Backspace (Ascii 8) event but I have been unable to make it work. The help file says:
    The KeyPress event occurs when the user presses a key that produces a typeable character (an ANSI key) on a running form while the form or a control on it has the focus. The event can occur either before or after the key is released. This event also occurs if you send an ANSI keystroke to a form or control using either the SendKeys action in a macro or the SendKeys Statement in Visual Basic.

    A KeyPress event can occur when any of the following keys are pressed:


    Any printable keyboard character.
    CTRL combined with a character from the standard alphabet.
    CTRL combined with any special character.
    BACKSPACE.
    ESC.
    Ideally, entries into the NameText Field using the Keypress code above should be able to respond with a single character deletion when the BS key is pressed using the following modification to the Case structure:
    Select Case KeyAscii
            Case Asc("a") To Asc("z")
            Case Asc("A") To Asc("Z")
            Case Asc(",")
            Case 8
                If Len(NameText.Value)=1 Then
                    NameText.Value = ""
                ElseIf Len(NameText.Value) > 1 Then
                    NameText.Value = Left(NameText.Value, Len(NameText.Value) - 1)
                End If
            Case Else
                KeyAscii = 0
        End Select
    However, I can not get the text box to return a backspace value. Any thoughts?

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Repeat Characters Appearing in a Text Box

    What do you want to do when backspace is presseed in the textbox?

  9. #9
    Registered User
    Join Date
    10-24-2014
    Location
    Charlotte,NC
    MS-Off Ver
    2010
    Posts
    6

    Re: Repeat Characters Appearing in a Text Box

    I want it to decrement NameText.Value by the last typed character. Unfortunately, the BS key is not being recognized by the KeyPress.

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Repeat Characters Appearing in a Text Box

    That will happen automatically when the user presses backspace, you don't need to use code to do that.

  11. #11
    Registered User
    Join Date
    10-24-2014
    Location
    Charlotte,NC
    MS-Off Ver
    2010
    Posts
    6

    Re: Repeat Characters Appearing in a Text Box

    Unfortunately, it was not working. I did, however, find a workaround:
    Private Sub NameText_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    If KeyCode = 8 And Len(NameText.Value) > 0 Then
        NameText.Value = Left(NameText.Value, Len(NameText.Value) - 1)
    End If
    End Sub
    This causes it to update instantly. Thanks very much for your help today - a great first forum experience.

  12. #12
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Repeat Characters Appearing in a Text Box

    You really don't need any code to handle key presses unless you are trying to restrict what the user is entering, which is what I originally thought you were doing here.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Unicode characters appearing as "?"
    By Musha in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-13-2013, 04:33 AM
  2. Replies: 4
    Last Post: 08-10-2009, 09:27 AM
  3. [SOLVED] Full Text is not appearing in column
    By Steve@rehabconsulting in forum Excel General
    Replies: 1
    Last Post: 07-27-2006, 11:45 AM
  4. [SOLVED] Why is my cell text not appearing?
    By Mister PM in forum Excel General
    Replies: 4
    Last Post: 07-07-2005, 12:05 PM
  5. Repeat characters to cell width
    By David in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 05-10-2005, 09:06 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1