Re: UserForm Is A Class
- From: "Greg Maxey" <gmaxey@xxxxxxxxxxxxxxxxxxx>
- Date: Sat, 10 Dec 2005 16:51:22 -0500
Jonathan,
You said:
> No. This syntax will also cause the QueryClose and Terminate events
> to fire
> Sub FunWithForms()
> Dim oForm as myForm
> Set oForm = New myForm
> oForm.Show
> End Sub
>
> In this case, the Unload is done implictly when then routine ends and
> the oForm variable goes out of scope. This triggers the QueryClose
> and Terminate events.
I see the Terminate event, but not the QueryClose event.
I have a form "myForm" with the event code you provided earlier. The form
has one command button that runs Me.Hide on click.
If I call with:
Sub CM5()
Dim oForm As myForm
Set oForm = New myForm
oForm.Show
Unload oForm
Set oForm = Nothing
End Sub
The debug.Print shows:
myForm initialized.
myForm activated
myForm queryclosed, mode 1
myForm terminated.
If I call with:
Sub CM6()
Dim oForm As myForm
Set oForm = New myForm
oForm.Show
Set oForm = Nothing
End Sub
Debug.Print shows:
myForm initialized.
myForm activated
myForm terminated.
If I call with:
Sub CM7()
Dim oForm As myForm
Set oForm = New myForm
oForm.Show
End Sub
Debug.Print shows:
myForm initialized.
myForm activated
myForm terminated.
The only thing that triggers the QueryClose event is the Unload oForm in the
first example.
If I change the Me.Hide to Unload Me in the Command Button code then yes the
QeuryClose event is triggered.
I expected the behaviour wrt the form not reinitializing and therefore
wasn't surprised. Thanks for the confirmation.
--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.
Jonathan West wrote:
> "Greg Maxey" <gmaxey@xxxxxxxxxxxxxxxxxxx> wrote in message
> news:eNvktqc$FHA.916@xxxxxxxxxxxxxxxxxxxxxxx
>> Jonathan,
>>
>> I am looking at the experiments that you proposed now.
>>
>> I have a simple userform with one command button. That command
>> button's code is simply:
>>
>> Me.Hide
>>
>> I see the behaviour that you point out about Load and Show and wil
>> then assume the using "Load" is the correct in a situation like your
>> example. Sub CM2b()
>> Load myForm
>> With myForm
>> .Caption = "Caption changed at runtime"
>> .Show
>> End With
>> End Sub
>>
>> However, as Tony Jolans mentioned, it appears to work equally as well
>> without the load statement. Either way, the Caption is changed when
>> the form is displayed. Is there something that I am missing here?
>
> "With myForm" will do an implicit Load, firing the Initialize event.
> So you don't need "Load myForm". In fact, I can't think of a case
> where the Load command is needed to ensure that you can separate the
> Initialize and Activate events.
>
>
>>
>> It appears that Unload oForm or Unload myForm is the only thing that
>> fires the UserForm_QueryClose event.
>
> No. This syntax will also cause the QueryClose and Terminate events
> to fire
> Sub FunWithForms()
> Dim oForm as myForm
> Set oForm = New myForm
> oForm.Show
> End Sub
>
> In this case, the Unload is done implictly when then routine ends and
> the oForm variable goes out of scope. This triggers the QueryClose
> and Terminate events.
>
> Furthermore, clicking the X in the top right corner of the form will
> trigger the QueryClose event (with a different value for the Mode
> parameter) followed by the Terminate event.
>
>> I assume that that is significant and therefore Unload is a best
>> practice?
>
> If you are using the default instance, then you must use Unload to
> get rid of the form. The simplest way of doing this is to include
> "Unload Me" instead of "Me.Hide" in the event routine for the
> appropriate commandbutton, unless you need to access the properties
> of the form from the calling routine after you have hidden it.
>
>>
>> I see from these demonstrations that using just:
>>
>> myForm.Show
>>
>> performs equally as well as:
>>
>> Dim oForm as myForm
>> Set oFrom = New myForm
>> myForm.Show
>>
>> but other than your pragmatism do you see "anything" wrong with
>> explicitly declaring a UserForm even for the simplest of forms?
>
> There is nothing at all wrong with it except for the extra lines of
> code involved. If you decide that doing things consistently this way
> is a coding practice that you want to adopt, then I think that is
> fine. I choose not to, but that is partly out of habit. Either way
> works, provided you understand what you are doing with it.
>
>>
>> I didn't see any surprised from using Me.Hide in any of the
>> experiments. What sould or could I have expected?
>>
>
> You may notice that if you use Me.Hide to close the form and do not
> have an Unload command in the calling routine, when you run the
> routine again, the Initialize event does *not* occur. This is because
> the form has not been unloaded from from memory, but merely hidden.
> Showing the form merely triggers the Activate event.
.
- Follow-Ups:
- Re: UserForm Is A Class
- From: Jonathan West
- Re: UserForm Is A Class
- References:
- UserForm Is A Class
- From: Greg Maxey
- Re: UserForm Is A Class
- From: Greg Maxey
- Re: UserForm Is A Class
- From: Jonathan West
- Re: UserForm Is A Class
- From: Greg Maxey
- Re: UserForm Is A Class
- From: Jonathan West
- Re: UserForm Is A Class
- From: Greg Maxey
- Re: UserForm Is A Class
- From: Jonathan West
- UserForm Is A Class
- Prev by Date: Re: Need help with border removal in a cell
- Next by Date: Re: UserForm Is A Class
- Previous by thread: Re: UserForm Is A Class
- Next by thread: Re: UserForm Is A Class
- Index(es):