setfocus of a textbox AfterUpdate 
Author Message
 setfocus of a textbox AfterUpdate

How about using the beforeupdate which has a cancel argument:

Public bBlockEvents As Boolean

Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
Dim Msg, Style, Title, Help, Ctxt, Response
If bBlockEvents Then Exit Sub
bBlockEvents = True
TextBox1.Text = ""
Msg = "Blah Blah"
Title = ""
Style = vbOKCancel
Help = ""
Ctxt = 0
Response = MsgBox(Msg, Style, Title, Help, Ctxt)
If Response = vbOK Then
   TextBox1.SetFocus
   Cancel = True
End If
bBlockEvents = False
End Sub

This worked for me.

Regards,
Tom Ogilvy
MVP Excel

Quote:

>I have a userform  with to textboxes.  I mpake the firtstextbox empty
afrter
>update.
>I want that te first textbox stay active, after i press the ok button on
the
>messagesbox
>The focus go to the second textbox.
>How can i do it
>Glenn
>That is the test code i wrote

>Private Sub TextBox1_AfterUpdate()
>TextBox1 = ""
>MessageBox
>End Sub

>Private Sub MessageBox()
>Dim Msg, Style, Title, Help, Ctxt, Response
>Msg = "Blah Blah"
>Title = ""
>Style = vbOKCancel
>Help = ""
>Ctxt = 0
>Response = MsgBox(Msg, Style, Title, Help, Ctxt)
>If Response = vbOK Then
>   TextBox1.SetFocus
>End If
>End Sub



Wed, 18 Jun 1902 08:00:00 GMT  
 setfocus of a textbox AfterUpdate
Tom (and Glenn),

This is the same kind of code I came up with, but I didn't post it because
it doesn't seem to solve Glenn's problem entirely.  It appears that using a
MsgBox in this way messes things up a bit.  When I tried this code, the
focus did not get returned to TextBox1 after clicking OK in the MsgBox; I
can't type anything into the TextBox unless I click on it again.  However,
the Tab key does cause the BeforeUpdate event to fire again. (??)

Is this happening on your machine as well?  If so, is there a way to truly
set the focus back to TextBox1 after a MsgBox is displayed?  I tried all
sorts of combinations of SetFocus, SelStart, SelLength, etc., to no avail.

Regards,
Jake Marx


Quote:
> How about using the beforeupdate which has a cancel argument:

> Public bBlockEvents As Boolean

> Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
> Dim Msg, Style, Title, Help, Ctxt, Response
> If bBlockEvents Then Exit Sub
> bBlockEvents = True
> TextBox1.Text = ""
> Msg = "Blah Blah"
> Title = ""
> Style = vbOKCancel
> Help = ""
> Ctxt = 0
> Response = MsgBox(Msg, Style, Title, Help, Ctxt)
> If Response = vbOK Then
>    TextBox1.SetFocus
>    Cancel = True
> End If
> bBlockEvents = False
> End Sub

> This worked for me.

> Regards,
> Tom Ogilvy
> MVP Excel


> >I have a userform  with to textboxes.  I mpake the firtstextbox empty
> afrter
> >update.
> >I want that te first textbox stay active, after i press the ok button on
> the
> >messagesbox
> >The focus go to the second textbox.
> >How can i do it
> >Glenn
> >That is the test code i wrote

> >Private Sub TextBox1_AfterUpdate()
> >TextBox1 = ""
> >MessageBox
> >End Sub

> >Private Sub MessageBox()
> >Dim Msg, Style, Title, Help, Ctxt, Response
> >Msg = "Blah Blah"
> >Title = ""
> >Style = vbOKCancel
> >Help = ""
> >Ctxt = 0
> >Response = MsgBox(Msg, Style, Title, Help, Ctxt)
> >If Response = vbOK Then
> >   TextBox1.SetFocus
> >End If
> >End Sub



Wed, 18 Jun 1902 08:00:00 GMT  
 setfocus of a textbox AfterUpdate
I pasted all the code I have in the userform code module   (xl97, SR2).  It
works fine for me.  The
bBlockEvents  should take care of the BeforeUpdate Event firing a second
time.

I return right to the cleared textbox and type in a new entry. It continues
to loop until whether I finish with a mouse click outside the textbox or use
the tab or enter keys. The only thing I find problematic is if I go from the
textbox to the close button in the upper right corner - then the msgbox is
left behind and the form is gone.  Of course liberal use of the queryclose
event could help here.

Jake, I have had problems getting back to a textbox with setfocus and
selstart and so forth in the past.  Jennifer Campion and I had an exchange
where that approach was working for her, but it wouldn't work for me.  It
appeared we were set up the same way, so I don't know what to say.  I know
you are using Excel 2K, and it seems to me (although I could be wrong)
someone was talking about a problem with a messagebox over a userform, but
It could just be a faulty memory.  Anyway, in any event, I don't recall the
particulars.

Regards,
Tom Ogilvy

Quote:

>Tom (and Glenn),

>This is the same kind of code I came up with, but I didn't post it because
>it doesn't seem to solve Glenn's problem entirely.  It appears that using a
>MsgBox in this way messes things up a bit.  When I tried this code, the
>focus did not get returned to TextBox1 after clicking OK in the MsgBox; I
>can't type anything into the TextBox unless I click on it again.  However,
>the Tab key does cause the BeforeUpdate event to fire again. (??)

>Is this happening on your machine as well?  If so, is there a way to truly
>set the focus back to TextBox1 after a MsgBox is displayed?  I tried all
>sorts of combinations of SetFocus, SelStart, SelLength, etc., to no avail.

>Regards,
>Jake Marx



>> How about using the beforeupdate which has a cancel argument:

>> Public bBlockEvents As Boolean

>> Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
>> Dim Msg, Style, Title, Help, Ctxt, Response
>> If bBlockEvents Then Exit Sub
>> bBlockEvents = True
>> TextBox1.Text = ""
>> Msg = "Blah Blah"
>> Title = ""
>> Style = vbOKCancel
>> Help = ""
>> Ctxt = 0
>> Response = MsgBox(Msg, Style, Title, Help, Ctxt)
>> If Response = vbOK Then
>>    TextBox1.SetFocus
>>    Cancel = True
>> End If
>> bBlockEvents = False
>> End Sub

>> This worked for me.

>> Regards,
>> Tom Ogilvy
>> MVP Excel


>> >I have a userform  with to textboxes.  I mpake the firtstextbox empty
>> afrter
>> >update.
>> >I want that te first textbox stay active, after i press the ok button on
>> the
>> >messagesbox
>> >The focus go to the second textbox.
>> >How can i do it
>> >Glenn
>> >That is the test code i wrote

>> >Private Sub TextBox1_AfterUpdate()
>> >TextBox1 = ""
>> >MessageBox
>> >End Sub

>> >Private Sub MessageBox()
>> >Dim Msg, Style, Title, Help, Ctxt, Response
>> >Msg = "Blah Blah"
>> >Title = ""
>> >Style = vbOKCancel
>> >Help = ""
>> >Ctxt = 0
>> >Response = MsgBox(Msg, Style, Title, Help, Ctxt)
>> >If Response = vbOK Then
>> >   TextBox1.SetFocus
>> >End If
>> >End Sub



Wed, 18 Jun 1902 08:00:00 GMT  
 setfocus of a textbox AfterUpdate
Tom,

Well, your code (and the code I had written) works perfectly under Excel 97
SR2.  But, when I try it in XL 2000, it won't set the focus back to the
TextBox.  I'm running NT4 SP5.  Does anyone else encounter this problem?
Should this be chalked up to a bug, or does anyone know of a workaround?

Thanks,
Jake Marx


Quote:
> I pasted all the code I have in the userform code module   (xl97, SR2).
It
> works fine for me.  The
> bBlockEvents  should take care of the BeforeUpdate Event firing a second
> time.

> I return right to the cleared textbox and type in a new entry. It
continues
> to loop until whether I finish with a mouse click outside the textbox or
use
> the tab or enter keys. The only thing I find problematic is if I go from
the
> textbox to the close button in the upper right corner - then the msgbox is
> left behind and the form is gone.  Of course liberal use of the queryclose
> event could help here.

> Jake, I have had problems getting back to a textbox with setfocus and
> selstart and so forth in the past.  Jennifer Campion and I had an exchange
> where that approach was working for her, but it wouldn't work for me.  It
> appeared we were set up the same way, so I don't know what to say.  I know
> you are using Excel 2K, and it seems to me (although I could be wrong)
> someone was talking about a problem with a messagebox over a userform, but
> It could just be a faulty memory.  Anyway, in any event, I don't recall
the
> particulars.

> Regards,
> Tom Ogilvy


> >Tom (and Glenn),

> >This is the same kind of code I came up with, but I didn't post it
because
> >it doesn't seem to solve Glenn's problem entirely.  It appears that using
a
> >MsgBox in this way messes things up a bit.  When I tried this code, the
> >focus did not get returned to TextBox1 after clicking OK in the MsgBox; I
> >can't type anything into the TextBox unless I click on it again.
However,
> >the Tab key does cause the BeforeUpdate event to fire again. (??)

> >Is this happening on your machine as well?  If so, is there a way to
truly
> >set the focus back to TextBox1 after a MsgBox is displayed?  I tried all
> >sorts of combinations of SetFocus, SelStart, SelLength, etc., to no
avail.

> >Regards,
> >Jake Marx



> >> How about using the beforeupdate which has a cancel argument:

> >> Public bBlockEvents As Boolean

> >> Private Sub TextBox1_BeforeUpdate(ByVal Cancel As

MSForms.ReturnBoolean)

- Show quoted text -

Quote:
> >> Dim Msg, Style, Title, Help, Ctxt, Response
> >> If bBlockEvents Then Exit Sub
> >> bBlockEvents = True
> >> TextBox1.Text = ""
> >> Msg = "Blah Blah"
> >> Title = ""
> >> Style = vbOKCancel
> >> Help = ""
> >> Ctxt = 0
> >> Response = MsgBox(Msg, Style, Title, Help, Ctxt)
> >> If Response = vbOK Then
> >>    TextBox1.SetFocus
> >>    Cancel = True
> >> End If
> >> bBlockEvents = False
> >> End Sub

> >> This worked for me.

> >> Regards,
> >> Tom Ogilvy
> >> MVP Excel


> >> >I have a userform  with to textboxes.  I mpake the firtstextbox empty
> >> afrter
> >> >update.
> >> >I want that te first textbox stay active, after i press the ok button
on
> >> the
> >> >messagesbox
> >> >The focus go to the second textbox.
> >> >How can i do it
> >> >Glenn
> >> >That is the test code i wrote

> >> >Private Sub TextBox1_AfterUpdate()
> >> >TextBox1 = ""
> >> >MessageBox
> >> >End Sub

> >> >Private Sub MessageBox()
> >> >Dim Msg, Style, Title, Help, Ctxt, Response
> >> >Msg = "Blah Blah"
> >> >Title = ""
> >> >Style = vbOKCancel
> >> >Help = ""
> >> >Ctxt = 0
> >> >Response = MsgBox(Msg, Style, Title, Help, Ctxt)
> >> >If Response = vbOK Then
> >> >   TextBox1.SetFocus
> >> >End If
> >> >End Sub



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Using .setfocus with afterupdate

2. SetFocus Problem + AfterUpdate Problem

3. AfterUpdate, SetFocus, and item receiving focus

4. textbox AfterUpdate event

5. AfterUpdate on Textbox fires more than once.

6. TextBox and AfterUpdate Event

7. Textbox AfterUpdate problem

8. AfterUpdate For TextBox Not Launching?

9. AfterUpdate event problem for a textbox on a form

10. Forms, Textboxes, SetFocus


 
Powered by phpBB® Forum Software © phpBB Group