ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   SetFocus Not working (https://www.excelbanter.com/excel-programming/438844-setfocus-not-working.html)

Mike Archer

SetFocus Not working
 
Hello. I have a userform with a textbox and a commandbutton that are not
working as expected with regard to setfocus.

After commandbutton3 is clicked, I want to set the focus to textbox3. This
works just fine. However, I have a keydown event in textbox3 that runs
commandbutton3_click sub if the enter key is pressed. When this event runs,
everything works except textbox3.setfocus. Any help is appreciated.

Private Sub CommandButton3_Click()
If TextBox3.Value < Empty Then
ListBox1.AddItem TextBox3.Value
TextBox3.Value = Empty
TextBox3.SetFocus
End If
End Sub

Private Sub TextBox3_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal
Shift As Integer)
If KeyCode = 13 Then
CommandButton3_Click
End If
End Sub


--
Thanks,
Mike

Jeff

SetFocus Not working
 
I tried your code & added a debug.Print KeyCode statement.
On my machine it retuned 40. I adjusted the If statment accordingly and it
worked perfectly. Maybe then Key_Down event is keyboard specific?


"Mike Archer" wrote:

Hello. I have a userform with a textbox and a commandbutton that are not
working as expected with regard to setfocus.

After commandbutton3 is clicked, I want to set the focus to textbox3. This
works just fine. However, I have a keydown event in textbox3 that runs
commandbutton3_click sub if the enter key is pressed. When this event runs,
everything works except textbox3.setfocus. Any help is appreciated.

Private Sub CommandButton3_Click()
If TextBox3.Value < Empty Then
ListBox1.AddItem TextBox3.Value
TextBox3.Value = Empty
TextBox3.SetFocus
End If
End Sub

Private Sub TextBox3_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal
Shift As Integer)
If KeyCode = 13 Then
CommandButton3_Click
End If
End Sub


--
Thanks,
Mike


michdenis

SetFocus Not working
 
Hi,

Try this way :

'-------------------------------------
Private Sub CommandButton3_Click()
If TextBox3.Value < Empty Then
ListBox1.AddItem TextBox3.Value
TextBox3.Value = Empty
End If
TextBox3.SetFocus
End Sub
'-------------------------------------



"Mike Archer" a écrit dans le message de groupe de
discussion : ...
Hello. I have a userform with a textbox and a commandbutton that are not
working as expected with regard to setfocus.

After commandbutton3 is clicked, I want to set the focus to textbox3. This
works just fine. However, I have a keydown event in textbox3 that runs
commandbutton3_click sub if the enter key is pressed. When this event runs,
everything works except textbox3.setfocus. Any help is appreciated.

Private Sub CommandButton3_Click()
If TextBox3.Value < Empty Then
ListBox1.AddItem TextBox3.Value
TextBox3.Value = Empty
TextBox3.SetFocus
End If
End Sub

Private Sub TextBox3_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal
Shift As Integer)
If KeyCode = 13 Then
CommandButton3_Click
End If
End Sub


--
Thanks,
Mike


Ryan H

SetFocus Not working
 
Naturally when you hit the Enter key (KeyCode = 13) Excel exits the textbox.
Actually, your code is working properly, but the Exit_Event fires after your
TextBox3.SetFocus code runs. Check out the code in the Exit Event. This
worked for me. Try it! Hope this helps! If so, let me know, click "YES"
below.

Option Explicit

Dim mySetFocus As Boolean

Private Sub CommandButton3_Click()

If TextBox3.Value < Empty Then
ListBox1.AddItem TextBox3.Value
TextBox3.Value = Empty
TextBox3.SetFocus
mySetFocus = True
End If

End Sub

Private Sub TextBox3_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Cancel = mySetFocus
mySetFocus = False
End Sub

Private Sub TextBox3_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal
Shift As Integer)

If KeyCode = 13 Then
CommandButton3_Click
End If

End Sub
--
Cheers,
Ryan


"Mike Archer" wrote:

Hello. I have a userform with a textbox and a commandbutton that are not
working as expected with regard to setfocus.

After commandbutton3 is clicked, I want to set the focus to textbox3. This
works just fine. However, I have a keydown event in textbox3 that runs
commandbutton3_click sub if the enter key is pressed. When this event runs,
everything works except textbox3.setfocus. Any help is appreciated.

Private Sub CommandButton3_Click()
If TextBox3.Value < Empty Then
ListBox1.AddItem TextBox3.Value
TextBox3.Value = Empty
TextBox3.SetFocus
End If
End Sub

Private Sub TextBox3_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal
Shift As Integer)
If KeyCode = 13 Then
CommandButton3_Click
End If
End Sub


--
Thanks,
Mike


Mike Archer

SetFocus Not working
 
Perfect. Thank you!
--
Thanks,
Mike


"Ryan H" wrote:

Naturally when you hit the Enter key (KeyCode = 13) Excel exits the textbox.
Actually, your code is working properly, but the Exit_Event fires after your
TextBox3.SetFocus code runs. Check out the code in the Exit Event. This
worked for me. Try it! Hope this helps! If so, let me know, click "YES"
below.

Option Explicit

Dim mySetFocus As Boolean

Private Sub CommandButton3_Click()

If TextBox3.Value < Empty Then
ListBox1.AddItem TextBox3.Value
TextBox3.Value = Empty
TextBox3.SetFocus
mySetFocus = True
End If

End Sub

Private Sub TextBox3_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Cancel = mySetFocus
mySetFocus = False
End Sub

Private Sub TextBox3_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal
Shift As Integer)

If KeyCode = 13 Then
CommandButton3_Click
End If

End Sub
--
Cheers,
Ryan


"Mike Archer" wrote:

Hello. I have a userform with a textbox and a commandbutton that are not
working as expected with regard to setfocus.

After commandbutton3 is clicked, I want to set the focus to textbox3. This
works just fine. However, I have a keydown event in textbox3 that runs
commandbutton3_click sub if the enter key is pressed. When this event runs,
everything works except textbox3.setfocus. Any help is appreciated.

Private Sub CommandButton3_Click()
If TextBox3.Value < Empty Then
ListBox1.AddItem TextBox3.Value
TextBox3.Value = Empty
TextBox3.SetFocus
End If
End Sub

Private Sub TextBox3_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal
Shift As Integer)
If KeyCode = 13 Then
CommandButton3_Click
End If
End Sub


--
Thanks,
Mike



All times are GMT +1. The time now is 11:41 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com