Re: Cancel Form and Hopelessly Lost Again

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Firstly, an apology. As if you weren't confused enough, I was confused and
should have gone to bed instead of posting. Sorry. Let me try again.

All that setting the Cancel property on the button really does is make
pressing the ESC key equivalent to clicking the button. It has no
relationship to the Cancel argument on the Textbox Exit - or any other -
Event. The Cancel argument is for you to "set", not query; setting it to
True cancels the event (stops the user leaving the textbox, or stops the
form closing, or - whatever the event is, it is canceled)

When you press the Cancel button the Cancel Button Click event fires. What
you do in here is up to you but let's assume you code "Unload Me". This will
trigger the "QueryClose" event (with a CloseMode of 1 meaning closed by an
unload statement). Assuming you don't set Cancel to True, the UserForm will
be closed. But - before closing (or as part of the closing process) - some
tidy up events occur - in your case, the textbox exit and, perhaps, others.
It is up to you to make sure that textbox exit code doesn't do anything it
shouldn't in these circumstances and for this you need to have a module
level flag (set in the button click or query close, checked in the textbox
exit) so that you know what is going on.

I should, perhaps, point out that all this is slightly odd (to my eyes
anyway) because the textbox is in a Frame. The Frame exit will have happened
long ago but for reasons best known to itself the userform still considers
you to be in the textbox even after you have left the frame and entered the
button.

--
Enjoy,
Tony


"Greg Maxey" <gmaxey@xxxxxxxxxxxxxxxxxxx> wrote in message
news:O7Wr0X4$FHA.504@xxxxxxxxxxxxxxxxxxxxxxx
> Tony,
>
> I am probably mixing apples an oranges. Yes, I have a command button name
> cmdCancel, Lable, Cancel with the .Cancel (I guess that is property) set
to
> True.
>
> There is one Frame containing one TextBox. I want to validate the value
in
> the textbox on exit. If the entry is invalid I want to stay in the
textbox
> and focus on the invalid text. Ok, I have figure out that part by using
the
> Cancel value of the Frame as shown in the code below.
>
> Here is where I am stuck. If I am in the textbox/frame with a valid or
> invalid value, and I want to cancel the form by clicking on my Cancel
button
> or clicking on the X close button on the form, then I simply want to
cancel
> and bypass the validation.
>
> Clicking my Cancel button or the X close button triggers the Frame1_Exit
> event and I don't see anyway of telling it to just exit if the form is
> canceled. Of course if I unstet the If CmdCanel Then Exit Sub it will
> cancel but that obviously isn't what I need.
>
> Is what I am trying to do even possible? Thanks.
>
>
>
> The code I have now is:
>
> Private Sub Frame1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
> ''******If CmdCancel.Cancel Then Exit Sub *******
> If Not SizeValidator(TextBox1.Value) Then
> With Me
> .Frame1.Caption = Me.TextBox1.Text & " is invalid." _
> & " Please enter a valid size."
> With .TextBox1
> .SetFocus
> .SelStart = 0
> .SelLength = Len(.Text)
> End With
> End With
> Cancel = True
> End If
> End Sub
>
> --
> Greg Maxey/Word MVP
> See:
> http://gregmaxey.mvps.org/word_tips.htm
> For some helpful tips using Word.
>
> Tony Jollans wrote:
> > Hi Greg,
> >
> > Very briefly before I go to bed ...
> >
> > Is your button called Cancel?
> >
> > The Cancel argument to the Exit Event is for you to *set*, not query.
> > It has nothing to do with any buttons on the Form. You can set it to
> > True to Cancel the Exit (i.e. leave the cursor in the textbox) or you
> > can set it (or leave it to default) to False to let the Exit go ahead.
> >
> > When you exit your textbox the focus will leave it and go elsewhere
> > (the button you click perhaps) but before that various events run.
> > When the textbox exit event is running the shift of the focus to the
> > button is waiting to happen - no matter what you do with the focus in
> > the exit event, it will afterwards be moved to the button.
> >
> > When your textbox exit event is running it has no access to the fact
> > that you have clicked the cancel button and you cannot put code in
> > the exit event subject to that button press which is, as far the code
> > is concerned, yet to happen.
> >
> >
> > "Greg Maxey" <gmaxey@xxxxxxxxxxxxxxxxxxx> wrote in message
> > news:#VLNOT3$FHA.516@xxxxxxxxxxxxxxxxxxxxxxx
> >> One more thing. Even though I can step through and watch the
> >> setfocus steps, the focus does not return to the text in textbox1.
> >> Unfathomable
> > :-(
> >>
> >>
> >> --
> >> Greg Maxey/Word MVP
> >> See:
> >> http://gregmaxey.mvps.org/word_tips.htm
> >> For some helpful tips using Word.
> >>
> >> Greg Maxey wrote:
> >>> I have a UserForm with a Frame holding 1 textbox, an OK button and a
> >>> Cancel Button. The Cancel button is set with Cancel = True
> >>>
> >>> I have a TextBox1_Exit Event as follows:
> >>>
> >>> Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
> >>> If Not Cancel Then
> >>> If Not SizeValidator(TextBox1.Value) Then
> >>> With Me
> >>> .Frame1.Caption = Me.TextBox1.Text & " is invalid." _
> >>> & " Please enter a valid size."
> >>> With .TextBox1
> >>> .SetFocus 'This isn't working?????
> >>> .SelStart = 0
> >>> .SelLength = Len(.Text)
> >>> End With
> >>> End With
> >>> End If
> >>> End If
> >>> End Sub
> >>>
> >>> Nothing I do will cause the Cancel value to read as True. How to
> >>> you code the command button so that ByVal Cancel changes to True
> >>> when I
> >>> try to cancel the form?
> >>>
> >>> Thanks
>
>


.



Relevant Pages

  • Re: Goto Specific Textbox in Userform
    ... Cancel is a variable defined by the event declaration. ... BeforeExit, setting cancel to true still causes the Exit action not to ... >> would need to create one for each textbox. ... >>> End Sub ...
    (microsoft.public.excel.programming)
  • RE: Exit event/textbox/frame
    ... Private Sub Frame1_Exit(ByVal Cancel As MSForms.ReturnBoolean) ... Private Sub TextBox1_Exit ... 'This is 1st textbox in Frame1 ... same TextFrame but failed when you tabbed to a different frame. ...
    (microsoft.public.excel.programming)
  • Re: Check by leaving frame
    ... I am also assuming that your Cancel button is outside of the frame. ... The problem with this is that the frame exit event is triggered before ... Private Sub cmdAnnuleren_Click ...
    (microsoft.public.word.vba.beginners)
  • Re: If statement with a Null
    ... In the first instance, you are checking to see if ... Private Sub txtDeductibleCreditsStartDate_Exit(ByVal Cancel As ... You only need one 'If' statement to check the value of the TextBox ... and a search of the VBA help only returned 3 topics - ...
    (microsoft.public.word.vba.general)
  • Re: If statement with a Null
    ... help and explanations. ... Private Sub txtDeductibleCreditsStartDate_Exit(ByVal Cancel As ... You only need one 'If' statement to check the value of the TextBox ... I don't know why that website would 'Null' in Word VBA examples; ...
    (microsoft.public.word.vba.general)