What is Alternate method to change Focus from TextBox to another Control ?

NimishK

Well-known Member
Joined
Sep 4, 2015
Messages
684
What is Alternate method to change Focus from TextBox to another Control
whose following properties are


Textbox1.EnterKeyBehaviour = True
Textbox1.MultiLine = True

Following does not Trigger to set focus on another control
Private Sub Textbox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)


If KeyAscii = 13 Then 'The ENTER key.'
SendKeys "{tab}" 'Set the focus to the next control.
KeyAscii = 0 'Ignore this key.
End If
End Sub

NimishK
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
The code you posted works for me, but try this
Code:
Private Sub Textbox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    If KeyAscii = 13 Then 'The ENTER key.'
        SendKeys "{Enter}"[COLOR=#ff0000], True[/COLOR]  'Set the focus to the next control.
        KeyAscii = 0 'Ignore this key.
    End If
End Sub
 
Upvote 0
Am Afraid Yongle Even with Sendkeys "{Enter}" have no success Everything remains same. Hope you changed the foll. properties of Textbox1

Textbox1.EnterKeyBehaviour = True

Textbox1.MultiLine = True
 
Last edited:
Upvote 0
Its your userform, you know which control is next in the tab order. Send the focus to that control explicitily, code like
Code:
Private Sub Textbox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    If KeyAscii = 13 Then
        ComboBox1.SetFocus
        KeyAscii = 0 'Ignore this key.
    End If
End Sub

On the other hand if you want pressing Enter to send the focus to the next control, just set the .EnterKeyBehavior to False.
 
Last edited:
Upvote 0
Sir, Mikerickson

here Textbox1.Multiline = True
so if i change its EnterKeyBehaviour = False then i am not able to goto to next line of Textbox1. Though the focus shifted to next Control ie another Textbox. What can be done so that everyhting goes smoothly With
Textbox1.Multiline = True
Textbox1.EnterKeyBehaviour = True
In Textbox1 when Pressing Enter It goes to new line feed of Textbox1 if
Textbox1.Multiline = True
Textbox1.EnterKeyBehaviour = True

If you have a method with (Alt + Enter) for New Line feed in Textbox1 I shall be really grateful and achieve what i want
 
Last edited:
Upvote 0
Am Afraid Yongle Even with Sendkeys "{Enter}" have no success Everything remains same. Hope you changed the foll. properties of Textbox1

Textbox1.EnterKeyBehaviour = True

Textbox1.MultiLine = True


Yes
- my properties are set the same as yours and the cusor jumps to the next control whenever {Tab} is used

If any other event macros are associated with this textbox please post the code - and don't forget CODE TAGS ;)
 
Upvote 0
So you mean to say for this particular text box i need to use Tab key to setfocus to next control and other Controls with Enter.
Will it be not Odd Just for one textbox i ve to use tab key and for others Enter Key as per the following
Code:
Private Sub Textbox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    If KeyAscii = 13 Then 'The ENTER key.'
        SendKeys "{Tab}", True  'Set the focus to the next control.
        KeyAscii = 0 'Ignore this key.
    End If
End Sub
 
Upvote 0
Resolved :);)
Got the Hint below quoted from Sir Mikericksons Reply Post 6 from Following Thread : https://www.mrexcel.com/forum/excel-questions/354430-user-form-enter-key-behavior.html
User form ENTER Key behavior.

Have you played with the .EnterKeyBehaviour property?
It will prevent the next control getting the focus when Enter is pressed.

So i changed EnterKeyBehaviour to False in Property of Textbox1 and coded below
Code:
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
  If KeyAscii = 13 Then 'The ENTER key.'
     SendKeys "{Tab}" 'Set the focus to the next control.
     KeyAscii = 0 'Ignore this key.
End If
End Sub

Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If Shift = 4 And KeyCode = 13 Then
    TextBox1.Text = TextBox1.Text & vbLf
End If

End Sub
So the above is the same effect when data entered with Alt+Enter in a Cell of Worksheet
Thanks NimishK
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,260
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top