Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 770
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 770
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 770
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default 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
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
margins keeping changing excel margins Excel Worksheet Functions 1 June 18th 09 02:16 AM
Keeping Charts from Changing Redhawkphi Charts and Charting in Excel 2 June 12th 08 04:57 PM
Keeping date from changing Richard Excel Discussion (Misc queries) 5 April 30th 08 02:57 AM
Keeping a transfered value the same after changing the original va DeVon Excel Worksheet Functions 0 February 4th 05 07:23 AM
changing focus Mike Macgowan Excel Programming 0 November 13th 03 03:31 AM


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

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"