Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 15
Default Setfocus Problem

Hi there....
I am trying to clear and setfocus to textbox1 after I enter a number!
I must be setting the controls wrong because I can't get textbox1 to accept
focus without hitting enter twice.
Here is the sample code below... please help!

Option Explicit

Private Sub TextBox1_AfterUpdate()
Dim varOne As Long
Dim varTwo As Long
Dim varTotal As Long
If TextBox1.Value < "" And TextBox1 < 0 Then
varOne = TextBox1.Value
varTwo = TextBox2.Value
varTotal = varOne + varTwo
TextBox2.Value = Format(varTotal, "Standard")
TextBox1.Value = ""
TextBox1.SetFocus
Else
TextBox1.Value = ""
TextBox1.SetFocus
End If
End Sub
Private Sub UserForm_Activate()
TextBox1.Value = ""
TextBox2.Value = Format(0, "Standard")
End Sub


Thanks
Craig


  #2   Report Post  
Posted to microsoft.public.excel.programming
Jay Jay is offline
external usenet poster
 
Posts: 671
Default Setfocus Problem

Hi Craig -

Your code looks good. Only problem is that the Exit event fires after your
AfterUpdate event procedure and it transfers focus to TextBox2, superceeding
your TextBox1.Setfocus statement.

Try adding an Exit event procedure. If that works for you, you can remove a
few unnecessary lines from your afterupdate event.

Jay
----------
Option Explicit

Private Sub TextBox1_AfterUpdate()

Dim varOne As Long
Dim varTwo As Long
Dim varTotal As Long
If TextBox1.Value < "" And TextBox1 < 0 Then
varOne = TextBox1.Value
varTwo = TextBox2.Value
varTotal = varOne + varTwo
TextBox2.Value = Format(varTotal, "Standard")
' TextBox1.Value = ""
' TextBox1.SetFocus
' Else
' TextBox1.Value = ""
' TextBox1.SetFocus
End If
End Sub

Private Sub TextBox2_Enter()
TextBox1.Value = ""
TextBox1.SetFocus
End Sub

Private Sub UserForm_Activate()
TextBox1.Value = ""
TextBox2.Value = Format(0, "Standard")
End Sub
-----



"Craig M" wrote:

Hi there....
I am trying to clear and setfocus to textbox1 after I enter a number!
I must be setting the controls wrong because I can't get textbox1 to accept
focus without hitting enter twice.
Here is the sample code below... please help!

Option Explicit

Private Sub TextBox1_AfterUpdate()
Dim varOne As Long
Dim varTwo As Long
Dim varTotal As Long
If TextBox1.Value < "" And TextBox1 < 0 Then
varOne = TextBox1.Value
varTwo = TextBox2.Value
varTotal = varOne + varTwo
TextBox2.Value = Format(varTotal, "Standard")
TextBox1.Value = ""
TextBox1.SetFocus
Else
TextBox1.Value = ""
TextBox1.SetFocus
End If
End Sub
Private Sub UserForm_Activate()
TextBox1.Value = ""
TextBox2.Value = Format(0, "Standard")
End Sub


Thanks
Craig



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Setfocus Problem

Hi Craig

The userform should be modeless for setfocus to work.
When you show the form add vbModeless at the end.
e.g.: Userform1.Show vbModeless

Sharad

"Craig M" wrote in message
news:DdaGh.1215890$R63.187115@pd7urf1no...
Hi there....
I am trying to clear and setfocus to textbox1 after I enter a number!
I must be setting the controls wrong because I can't get textbox1 to
accept focus without hitting enter twice.
Here is the sample code below... please help!

Option Explicit

Private Sub TextBox1_AfterUpdate()
Dim varOne As Long
Dim varTwo As Long
Dim varTotal As Long
If TextBox1.Value < "" And TextBox1 < 0 Then
varOne = TextBox1.Value
varTwo = TextBox2.Value
varTotal = varOne + varTwo
TextBox2.Value = Format(varTotal, "Standard")
TextBox1.Value = ""
TextBox1.SetFocus
Else
TextBox1.Value = ""
TextBox1.SetFocus
End If
End Sub
Private Sub UserForm_Activate()
TextBox1.Value = ""
TextBox2.Value = Format(0, "Standard")
End Sub


Thanks
Craig



  #4   Report Post  
Posted to microsoft.public.excel.programming
Jay Jay is offline
external usenet poster
 
Posts: 671
Default Setfocus Problem

Sorry, my previous post should read:

"Try adding an Enter event procedure (for TextBox2)." instead of
"Try adding an Exit event procedure."
--
Jay


"Craig M" wrote:

Hi there....
I am trying to clear and setfocus to textbox1 after I enter a number!
I must be setting the controls wrong because I can't get textbox1 to accept
focus without hitting enter twice.
Here is the sample code below... please help!

Option Explicit

Private Sub TextBox1_AfterUpdate()
Dim varOne As Long
Dim varTwo As Long
Dim varTotal As Long
If TextBox1.Value < "" And TextBox1 < 0 Then
varOne = TextBox1.Value
varTwo = TextBox2.Value
varTotal = varOne + varTwo
TextBox2.Value = Format(varTotal, "Standard")
TextBox1.Value = ""
TextBox1.SetFocus
Else
TextBox1.Value = ""
TextBox1.SetFocus
End If
End Sub
Private Sub UserForm_Activate()
TextBox1.Value = ""
TextBox2.Value = Format(0, "Standard")
End Sub


Thanks
Craig



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 15
Default Setfocus Problem

Thank You... adding the enter event for textbox2 did the trick!
Something so easy was killing me!

Thanks Again
Craig



"Jay" wrote in message
...
Sorry, my previous post should read:

"Try adding an Enter event procedure (for TextBox2)." instead of
"Try adding an Exit event procedure."
--
Jay


"Craig M" wrote:

Hi there....
I am trying to clear and setfocus to textbox1 after I enter a number!
I must be setting the controls wrong because I can't get textbox1 to
accept
focus without hitting enter twice.
Here is the sample code below... please help!

Option Explicit

Private Sub TextBox1_AfterUpdate()
Dim varOne As Long
Dim varTwo As Long
Dim varTotal As Long
If TextBox1.Value < "" And TextBox1 < 0 Then
varOne = TextBox1.Value
varTwo = TextBox2.Value
varTotal = varOne + varTwo
TextBox2.Value = Format(varTotal, "Standard")
TextBox1.Value = ""
TextBox1.SetFocus
Else
TextBox1.Value = ""
TextBox1.SetFocus
End If
End Sub
Private Sub UserForm_Activate()
TextBox1.Value = ""
TextBox2.Value = Format(0, "Standard")
End Sub


Thanks
Craig







  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Setfocus Problem

Actually Jay was right the first time, but doesn't seem to know it.

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim varOne As Long
Dim varTwo As Long
Dim varTotal As Long
If TextBox1.Value < "" And TextBox1 < 0 Then
varOne = TextBox1.Value
varTwo = TextBox2.Value
varTotal = varOne + varTwo
TextBox2.Value = Format(varTotal, "Standard")
cancel = True
Else
TextBox1.Value = ""
cancel = True
End If
End Sub

Private Sub UserForm_Activate()
TextBox1.Value = ""
TextBox2.Value = Format(0, "Standard")
End Sub

So you don't need a kludge solution with setfocus and extra events. It is
usually best not to fight the behavior of the controls if you can help it.

--
Regards,
Tom Ogilvy




"Craig M" wrote in message
news:%qbGh.1206747$5R2.728175@pd7urf3no...
Thank You... adding the enter event for textbox2 did the trick!
Something so easy was killing me!

Thanks Again
Craig



"Jay" wrote in message
...
Sorry, my previous post should read:

"Try adding an Enter event procedure (for TextBox2)." instead of
"Try adding an Exit event procedure."
--
Jay


"Craig M" wrote:

Hi there....
I am trying to clear and setfocus to textbox1 after I enter a number!
I must be setting the controls wrong because I can't get textbox1 to
accept
focus without hitting enter twice.
Here is the sample code below... please help!

Option Explicit

Private Sub TextBox1_AfterUpdate()
Dim varOne As Long
Dim varTwo As Long
Dim varTotal As Long
If TextBox1.Value < "" And TextBox1 < 0 Then
varOne = TextBox1.Value
varTwo = TextBox2.Value
varTotal = varOne + varTwo
TextBox2.Value = Format(varTotal, "Standard")
TextBox1.Value = ""
TextBox1.SetFocus
Else
TextBox1.Value = ""
TextBox1.SetFocus
End If
End Sub
Private Sub UserForm_Activate()
TextBox1.Value = ""
TextBox2.Value = Format(0, "Standard")
End Sub


Thanks
Craig







  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 15
Default Setfocus Problem

Tom, I tried your method. It worked after I placed a Textbox1.Value =""
above the first cancel=True.
The only problem is that it seems to create an endless loop, when I placed a
commandbutton to exit the form I am unable to use it.

Craig


"Tom Ogilvy" wrote in message
...
Actually Jay was right the first time, but doesn't seem to know it.

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim varOne As Long
Dim varTwo As Long
Dim varTotal As Long
If TextBox1.Value < "" And TextBox1 < 0 Then
varOne = TextBox1.Value
varTwo = TextBox2.Value
varTotal = varOne + varTwo
TextBox2.Value = Format(varTotal, "Standard")
cancel = True
Else
TextBox1.Value = ""
cancel = True
End If
End Sub

Private Sub UserForm_Activate()
TextBox1.Value = ""
TextBox2.Value = Format(0, "Standard")
End Sub

So you don't need a kludge solution with setfocus and extra events. It is
usually best not to fight the behavior of the controls if you can help it.

--
Regards,
Tom Ogilvy




"Craig M" wrote in message
news:%qbGh.1206747$5R2.728175@pd7urf3no...
Thank You... adding the enter event for textbox2 did the trick!
Something so easy was killing me!

Thanks Again
Craig



"Jay" wrote in message
...
Sorry, my previous post should read:

"Try adding an Enter event procedure (for TextBox2)." instead of
"Try adding an Exit event procedure."
--
Jay


"Craig M" wrote:

Hi there....
I am trying to clear and setfocus to textbox1 after I enter a number!
I must be setting the controls wrong because I can't get textbox1 to
accept
focus without hitting enter twice.
Here is the sample code below... please help!

Option Explicit

Private Sub TextBox1_AfterUpdate()
Dim varOne As Long
Dim varTwo As Long
Dim varTotal As Long
If TextBox1.Value < "" And TextBox1 < 0 Then
varOne = TextBox1.Value
varTwo = TextBox2.Value
varTotal = varOne + varTwo
TextBox2.Value = Format(varTotal, "Standard")
TextBox1.Value = ""
TextBox1.SetFocus
Else
TextBox1.Value = ""
TextBox1.SetFocus
End If
End Sub
Private Sub UserForm_Activate()
TextBox1.Value = ""
TextBox2.Value = Format(0, "Standard")
End Sub


Thanks
Craig









  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Setfocus Problem

One easy fix is to select that commandbutton to exit and change the
..takefocusonclick property to false.

But it looks like your code is written to never let you leave that text box. If
that's correct, you may want to change that other textbox to a label--just to
display the value.

Craig M wrote:

Tom, I tried your method. It worked after I placed a Textbox1.Value =""
above the first cancel=True.
The only problem is that it seems to create an endless loop, when I placed a
commandbutton to exit the form I am unable to use it.

Craig

"Tom Ogilvy" wrote in message
...
Actually Jay was right the first time, but doesn't seem to know it.

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim varOne As Long
Dim varTwo As Long
Dim varTotal As Long
If TextBox1.Value < "" And TextBox1 < 0 Then
varOne = TextBox1.Value
varTwo = TextBox2.Value
varTotal = varOne + varTwo
TextBox2.Value = Format(varTotal, "Standard")
cancel = True
Else
TextBox1.Value = ""
cancel = True
End If
End Sub

Private Sub UserForm_Activate()
TextBox1.Value = ""
TextBox2.Value = Format(0, "Standard")
End Sub

So you don't need a kludge solution with setfocus and extra events. It is
usually best not to fight the behavior of the controls if you can help it.

--
Regards,
Tom Ogilvy




"Craig M" wrote in message
news:%qbGh.1206747$5R2.728175@pd7urf3no...
Thank You... adding the enter event for textbox2 did the trick!
Something so easy was killing me!

Thanks Again
Craig



"Jay" wrote in message
...
Sorry, my previous post should read:

"Try adding an Enter event procedure (for TextBox2)." instead of
"Try adding an Exit event procedure."
--
Jay


"Craig M" wrote:

Hi there....
I am trying to clear and setfocus to textbox1 after I enter a number!
I must be setting the controls wrong because I can't get textbox1 to
accept
focus without hitting enter twice.
Here is the sample code below... please help!

Option Explicit

Private Sub TextBox1_AfterUpdate()
Dim varOne As Long
Dim varTwo As Long
Dim varTotal As Long
If TextBox1.Value < "" And TextBox1 < 0 Then
varOne = TextBox1.Value
varTwo = TextBox2.Value
varTotal = varOne + varTwo
TextBox2.Value = Format(varTotal, "Standard")
TextBox1.Value = ""
TextBox1.SetFocus
Else
TextBox1.Value = ""
TextBox1.SetFocus
End If
End Sub
Private Sub UserForm_Activate()
TextBox1.Value = ""
TextBox2.Value = Format(0, "Standard")
End Sub


Thanks
Craig








--

Dave Peterson
  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Setfocus Problem

Your just too restrictive. This will allow the user to exit if the textbox1
is blank (which is should be when they want to exit).

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim varOne As Long
Dim varTwo As Long
Dim varTotal As Long
If TextBox1.Value < "" And TextBox1 < 0 Then
varOne = TextBox1.Value
varTwo = TextBox2.Value
varTotal = varOne + varTwo
TextBox2.Value = Format(varTotal, "Standard")
TextBox1.Value = ""
Cancel = True
Else
If TextBox1.Value = "0" Then
TextBox1.Value = ""
Cancel = True
End If
End If
End Sub

--
Regards,
Tom Ogilvy

"Craig M" wrote in message
news:wjkGh.1218995$R63.23056@pd7urf1no...
Tom, I tried your method. It worked after I placed a Textbox1.Value =""
above the first cancel=True.
The only problem is that it seems to create an endless loop, when I placed
a commandbutton to exit the form I am unable to use it.

Craig


"Tom Ogilvy" wrote in message
...
Actually Jay was right the first time, but doesn't seem to know it.

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim varOne As Long
Dim varTwo As Long
Dim varTotal As Long
If TextBox1.Value < "" And TextBox1 < 0 Then
varOne = TextBox1.Value
varTwo = TextBox2.Value
varTotal = varOne + varTwo
TextBox2.Value = Format(varTotal, "Standard")
cancel = True
Else
TextBox1.Value = ""
cancel = True
End If
End Sub

Private Sub UserForm_Activate()
TextBox1.Value = ""
TextBox2.Value = Format(0, "Standard")
End Sub

So you don't need a kludge solution with setfocus and extra events. It
is usually best not to fight the behavior of the controls if you can help
it.

--
Regards,
Tom Ogilvy




"Craig M" wrote in message
news:%qbGh.1206747$5R2.728175@pd7urf3no...
Thank You... adding the enter event for textbox2 did the trick!
Something so easy was killing me!

Thanks Again
Craig



"Jay" wrote in message
...
Sorry, my previous post should read:

"Try adding an Enter event procedure (for TextBox2)." instead of
"Try adding an Exit event procedure."
--
Jay


"Craig M" wrote:

Hi there....
I am trying to clear and setfocus to textbox1 after I enter a number!
I must be setting the controls wrong because I can't get textbox1 to
accept
focus without hitting enter twice.
Here is the sample code below... please help!

Option Explicit

Private Sub TextBox1_AfterUpdate()
Dim varOne As Long
Dim varTwo As Long
Dim varTotal As Long
If TextBox1.Value < "" And TextBox1 < 0 Then
varOne = TextBox1.Value
varTwo = TextBox2.Value
varTotal = varOne + varTwo
TextBox2.Value = Format(varTotal, "Standard")
TextBox1.Value = ""
TextBox1.SetFocus
Else
TextBox1.Value = ""
TextBox1.SetFocus
End If
End Sub
Private Sub UserForm_Activate()
TextBox1.Value = ""
TextBox2.Value = Format(0, "Standard")
End Sub


Thanks
Craig











  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 15
Default Setfocus Problem

Thanks Tom... that did the trick!
Craig


"Tom Ogilvy" wrote in message
...
Your just too restrictive. This will allow the user to exit if the
textbox1 is blank (which is should be when they want to exit).

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim varOne As Long
Dim varTwo As Long
Dim varTotal As Long
If TextBox1.Value < "" And TextBox1 < 0 Then
varOne = TextBox1.Value
varTwo = TextBox2.Value
varTotal = varOne + varTwo
TextBox2.Value = Format(varTotal, "Standard")
TextBox1.Value = ""
Cancel = True
Else
If TextBox1.Value = "0" Then
TextBox1.Value = ""
Cancel = True
End If
End If
End Sub

--
Regards,
Tom Ogilvy

"Craig M" wrote in message
news:wjkGh.1218995$R63.23056@pd7urf1no...
Tom, I tried your method. It worked after I placed a Textbox1.Value =""
above the first cancel=True.
The only problem is that it seems to create an endless loop, when I
placed a commandbutton to exit the form I am unable to use it.

Craig


"Tom Ogilvy" wrote in message
...
Actually Jay was right the first time, but doesn't seem to know it.

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim varOne As Long
Dim varTwo As Long
Dim varTotal As Long
If TextBox1.Value < "" And TextBox1 < 0 Then
varOne = TextBox1.Value
varTwo = TextBox2.Value
varTotal = varOne + varTwo
TextBox2.Value = Format(varTotal, "Standard")
cancel = True
Else
TextBox1.Value = ""
cancel = True
End If
End Sub

Private Sub UserForm_Activate()
TextBox1.Value = ""
TextBox2.Value = Format(0, "Standard")
End Sub

So you don't need a kludge solution with setfocus and extra events. It
is usually best not to fight the behavior of the controls if you can
help it.

--
Regards,
Tom Ogilvy




"Craig M" wrote in message
news:%qbGh.1206747$5R2.728175@pd7urf3no...
Thank You... adding the enter event for textbox2 did the trick!
Something so easy was killing me!

Thanks Again
Craig



"Jay" wrote in message
...
Sorry, my previous post should read:

"Try adding an Enter event procedure (for TextBox2)." instead of
"Try adding an Exit event procedure."
--
Jay


"Craig M" wrote:

Hi there....
I am trying to clear and setfocus to textbox1 after I enter a number!
I must be setting the controls wrong because I can't get textbox1 to
accept
focus without hitting enter twice.
Here is the sample code below... please help!

Option Explicit

Private Sub TextBox1_AfterUpdate()
Dim varOne As Long
Dim varTwo As Long
Dim varTotal As Long
If TextBox1.Value < "" And TextBox1 < 0 Then
varOne = TextBox1.Value
varTwo = TextBox2.Value
varTotal = varOne + varTwo
TextBox2.Value = Format(varTotal, "Standard")
TextBox1.Value = ""
TextBox1.SetFocus
Else
TextBox1.Value = ""
TextBox1.SetFocus
End If
End Sub
Private Sub UserForm_Activate()
TextBox1.Value = ""
TextBox2.Value = Format(0, "Standard")
End Sub


Thanks
Craig













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
SetFocus problem cesw Excel Programming 1 September 10th 05 08:19 PM
SetFocus problem JH Excel Programming 1 April 18th 05 03:55 PM
SetFocus Problem Todd Huttenstine[_2_] Excel Programming 2 January 19th 04 10:50 PM
TextBox SetFocus Problem Tom Ogilvy Excel Programming 1 September 12th 03 01:27 PM
SetFocus problem RFraley Excel Programming 2 September 7th 03 01:18 PM


All times are GMT +1. The time now is 10:52 AM.

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"