Re: UserForm Is A Class




"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.



--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org


.