Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
Keeping The Focus From Changing. How?
Hi, I'm designing a userform for data entry. I'd like to have the userform set up that I can put text in a textbo and press enter, then it does what it's supposed to do and clears th textbox so that I can type new text in. I've seen other vb program that do this. With the way things are now, when I press enter it does everything it' supposed to do, but the focus always moves to the next control. Is there anyway that I can stop the focus from moving to the nex control when I press enter? Thanks a lot for the help. I appreciate it. : -- abx ----------------------------------------------------------------------- abxy's Profile: http://www.excelforum.com/member.php...nfo&userid=473 View this thread: http://www.excelforum.com/showthread.php?threadid=40057 |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
Keeping The Focus From Changing. How?
In the Exit event, set cancel = true
In the same event, Textbox1.Text = "" -- Regards, Tom Ogilvy "abxy" wrote in message ... Hi, I'm designing a userform for data entry. I'd like to have the userform set up that I can put text in a textbox and press enter, then it does what it's supposed to do and clears the textbox so that I can type new text in. I've seen other vb programs that do this. With the way things are now, when I press enter it does everything it's supposed to do, but the focus always moves to the next control. Is there anyway that I can stop the focus from moving to the next control when I press enter? Thanks a lot for the help. I appreciate it. :) -- abxy ------------------------------------------------------------------------ abxy's Profile: http://www.excelforum.com/member.php...fo&userid=4730 View this thread: http://www.excelforum.com/showthread...hreadid=400574 |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
Keeping The Focus From Changing. How?
Works perfect. Thanks a lot mate :) -- abxy ------------------------------------------------------------------------ abxy's Profile: http://www.excelforum.com/member.php...fo&userid=4730 View this thread: http://www.excelforum.com/showthread...hreadid=400574 |
#4
Posted to microsoft.public.excel.programming
|
|||
|
|||
Keeping The Focus From Changing. How?
Actually, this isn't working as perfect as I thought. Now, I can't click out of the textbox control. I'm trying to think of a way in code to say: if I press enter, don' change the focus (cancel = true), but if I click out of the contro then change the focus like normal (cancel = false) -- abx ----------------------------------------------------------------------- abxy's Profile: http://www.excelforum.com/member.php...nfo&userid=473 View this thread: http://www.excelforum.com/showthread.php?threadid=40057 |
#5
Posted to microsoft.public.excel.programming
|
|||
|
|||
Keeping The Focus From Changing. How?
abxy
You need a global variable to track whether enter was pressed. If it was, then do the actions as Tom described, else behave normally, i.e., you can click or tab out of the textbox: Dim enter_pressed As Boolean ' this is above all subroutines Option Explicit Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean) If enter_pressed Then 'if it was set in the keydown event below Cancel = True Me.TextBox1 = "" enter_pressed = False 'clear it, so back to normal behavior End If End Sub Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer) If KeyCode = 13 Then ' enter key enter_pressed = True End If End Sub hth, Doug "abxy" wrote in message ... Actually, this isn't working as perfect as I thought. Now, I can't click out of the textbox control. I'm trying to think of a way in code to say: if I press enter, don't change the focus (cancel = true), but if I click out of the control then change the focus like normal (cancel = false). -- abxy ------------------------------------------------------------------------ abxy's Profile: http://www.excelforum.com/member.php...fo&userid=4730 View this thread: http://www.excelforum.com/showthread...hreadid=400574 |
#6
Posted to microsoft.public.excel.programming
|
|||
|
|||
Keeping The Focus From Changing. How?
Doug Glancy wrote: abxy You need a global variable to track whether enter was pressed. If it was, then do the actions as Tom described, else behave normally, i.e., you can click or tab out of the textbox: Dim enter_pressed As Boolean ' this is above all subroutines Option Explicit Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean) If enter_pressed Then 'if it was set in the keydown event below Cancel = True Me.TextBox1 = "" enter_pressed = False 'clear it, so back to normal behavior End If End Sub Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer) If KeyCode = 13 Then ' enter key enter_pressed = True End If End Sub or more simply, just put the code you want to run when enter is pushed in the KeyDown event. Iain |
#7
Posted to microsoft.public.excel.programming
|
|||
|
|||
Keeping The Focus From Changing. How?
Iain,
I couldn't get what you suggest to work the way abxy wants. There is no cancel event in KeyDown event and SetFocus didn't bring it back to TextBox1. I'd be interested to see the code you'd use. Doug "Iain King" wrote in message oups.com... Doug Glancy wrote: abxy You need a global variable to track whether enter was pressed. If it was, then do the actions as Tom described, else behave normally, i.e., you can click or tab out of the textbox: Dim enter_pressed As Boolean ' this is above all subroutines Option Explicit Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean) If enter_pressed Then 'if it was set in the keydown event below Cancel = True Me.TextBox1 = "" enter_pressed = False 'clear it, so back to normal behavior End If End Sub Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer) If KeyCode = 13 Then ' enter key enter_pressed = True End If End Sub or more simply, just put the code you want to run when enter is pushed in the KeyDown event. Iain |
#8
Posted to microsoft.public.excel.programming
|
|||
|
|||
Keeping The Focus From Changing. How?
Doug Glancy wrote: Iain, I couldn't get what you suggest to work the way abxy wants. There is no cancel event in KeyDown event and SetFocus didn't bring it back to TextBox1. I'd be interested to see the code you'd use. To cancel keydown, set KeyCode to 0. I think his situation is something like having a textbox where you enter, say, a name, with an 'Add' button beside it, which adds that name to a list, then: Private Sub addName() list.Add(TextBox1.Value) TextBox1.Value = "" TextBox1.SetFocus() End Sub Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer) If KeyCode = 13 Then ' enter key KeyCode = 0 addName() End If End Sub Private Sub Add_Click() addName() End Sub Doug "Iain King" wrote in message oups.com... Doug Glancy wrote: abxy You need a global variable to track whether enter was pressed. If it was, then do the actions as Tom described, else behave normally, i.e., you can click or tab out of the textbox: Dim enter_pressed As Boolean ' this is above all subroutines Option Explicit Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean) If enter_pressed Then 'if it was set in the keydown event below Cancel = True Me.TextBox1 = "" enter_pressed = False 'clear it, so back to normal behavior End If End Sub Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer) If KeyCode = 13 Then ' enter key enter_pressed = True End If End Sub or more simply, just put the code you want to run when enter is pushed in the KeyDown event. Iain |
#9
Posted to microsoft.public.excel.programming
|
|||
|
|||
Keeping The Focus From Changing. How?
Iain,
Thanks. I didn't think of KeyCode = 0. That's a useful thing to know. Doug "Iain King" wrote in message oups.com... Doug Glancy wrote: Iain, I couldn't get what you suggest to work the way abxy wants. There is no cancel event in KeyDown event and SetFocus didn't bring it back to TextBox1. I'd be interested to see the code you'd use. To cancel keydown, set KeyCode to 0. I think his situation is something like having a textbox where you enter, say, a name, with an 'Add' button beside it, which adds that name to a list, then: Private Sub addName() list.Add(TextBox1.Value) TextBox1.Value = "" TextBox1.SetFocus() End Sub Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer) If KeyCode = 13 Then ' enter key KeyCode = 0 addName() End If End Sub Private Sub Add_Click() addName() End Sub Doug "Iain King" wrote in message oups.com... Doug Glancy wrote: abxy You need a global variable to track whether enter was pressed. If it was, then do the actions as Tom described, else behave normally, i.e., you can click or tab out of the textbox: Dim enter_pressed As Boolean ' this is above all subroutines Option Explicit Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean) If enter_pressed Then 'if it was set in the keydown event below Cancel = True Me.TextBox1 = "" enter_pressed = False 'clear it, so back to normal behavior End If End Sub Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer) If KeyCode = 13 Then ' enter key enter_pressed = True End If End Sub or more simply, just put the code you want to run when enter is pushed in the KeyDown event. Iain |
#10
Posted to microsoft.public.excel.programming
|
|||
|
|||
Keeping The Focus From Changing. How?
Iain, this works very perfectly. My problem is now solved. keycode = 0 was the key to the solution all along. Thanks for showin me (us) that. And a big thankyou to everyone that contributed to the thread to hel me get over this hump. : -- abx ----------------------------------------------------------------------- abxy's Profile: http://www.excelforum.com/member.php...nfo&userid=473 View this thread: http://www.excelforum.com/showthread.php?threadid=40057 |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
margins keeping changing | Excel Worksheet Functions | |||
Keeping Charts from Changing | Charts and Charting in Excel | |||
Keeping date from changing | Excel Discussion (Misc queries) | |||
Keeping a transfered value the same after changing the original va | Excel Worksheet Functions | |||
changing focus | Excel Programming |