Private Sub ComboBox1_AfterUpdate()
Dim s As String
s = Me.ComboBox1.Value
If Me.TextBox1 = "" Then
Me.TextBox1 = s
Else
Me.TextBox1 = Me.TextBox1 & ", " & s
End If
If Me.ComboBox1.Value = "" Then
Me.ComboBox1.SetFocus
End If
End Sub
Private Sub ComboBox1_Click()
Dim s As String
s = Me.ComboBox1.Value
If s <> "" Then
If Me.TextBox1 = "" Then
Me.TextBox1 = s
Else
Me.TextBox1 = Me.TextBox1 & ", " & s
End If
Me.ComboBox1.Value = ""
End If
End Sub
I've attached the workbook for your reference with the code you have provided.
I'm using AfterUpdate event because I want to give the user the choice to type the text that is not in the combo box list.
The code works if I press enter two times. That means the user types the text that's not in the combo box list and press enter two times so that the text gets copied into the textbox and then the Textbox1 in on setfocus.
Is there any possibility I could make the textbox1 as setfocus when the user press the enter button only once.
You do not have the required permissions to view the files attached to this post.
In the attached workbook I need the txtNotes to be set focus after the user enters values to listbox. but my code arrangement is not doing so in the txtNotes AfterUpdate event. But the
Private Sub txtNotes_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = vbKeyReturn Or KeyCode = vbKeyTab Then
CallAdd
KeyCode = 0
End If
End Sub
It works great. However when i click the tab to move to another field it keeps on giving me the message "please enter notes". is there an alternative for this?
I've found out why the code was not working. It's because I have the following lines of code. when I remove these lines of code from the txtNotes_Change event the keydown code works. How could I make the keydown code to work even with the txtNotes Change event code?
Private Sub txtNotes_Change()
If txtNotes.TextLength > 65 Then
MsgBox "You've reached the charater limit", vbCritical, "mydata"
txtNotes.Text = Left(txtNotes.Text, 65)
End If
Me.txtMessageCharacters.Value = Me.txtNotes.TextLength
Me.txtAvailableCharacters = Me.txtMessageLimit - Me.txtMessageCharacters
End Sub
Private Sub txtNotes_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = vbKeyReturn Or KeyCode = vbKeyTab Then
CallAdd
KeyCode = 0
End If
End Sub
Private Sub txtNotes_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = vbKeyReturn Then
CallAdd
KeyCode = 0
End If
End Sub
The original code does enter data with keyboard enter and tab button. The modified one enters data only with the enter button.
I wanted only the keyboard enter button to enter data to listbox and it works fine now.
Private Sub txtNotes_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = vbKeyReturn Then
If Me.TextBox1 = "" Then
CallAdd
Else
CallData
End If
KeyCode = 0
End If
End Sub