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