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