PDA

View Full Version : Calling UserForms: FormName.Show or Dim Object/Set New?



Dr.K
09-10-2007, 04:57 PM
The first way I learned to call a UserForm is to just use the name of the form:

FormName.show

However, I've seen a bunch of code examples on the web which use "Class" style calling: you dim an Object as FormName, and then set the object as a new instance of the class object:

Dim frmMyForm As FormName
Set frmMyForm = New FormName

Why would you do it the second way? Are there any advantages?

I suppose if you wanted to use multiple instances of the Form simultaneously, you'd have to use the second method.

When you use the first method, is VBA just creating a new instance of the form, and calling it "FormName"?

Any advice/explanation is appreciated.

Bob Phillips
09-11-2007, 01:01 AM
Basically the argument is that a userform is just a special type of class, so you should handle it like a class, instantiate it explicitly, process that class object, and release it at the end.

rory
09-11-2007, 02:06 AM
The second way is safer because if something unexpected happens in your code and your form variable is destroyed, you know about it. The first way will simply create a new instance of the form and you will lose any data from the form without knowing why.

Dr.K
09-12-2007, 10:00 AM
Thanks for the input, guys!

I'm not sure I understand "your form variable is destroyed" but I'm kind of slow on the uptake for this kind of stuff.

One thing I ran into yesterday, is when you get into loops with show/hide multiple forms at once, it gets weird as to which one you are referring too. Assigning an Object Variable to each instance of a UserForm would make that a lot more clear.

For that matter, it seems weird that the code branches around .hide and .show... It makes it difficult to hide and show a form for cosmetic purposes while working on another form, because you get an immediate code branch with each show and hide.




handle it like a class, instantiate it explicitly, process that class object, and release it at the end.


I'm assuming that "Unload Me" properly releases it... But if I do it the other way, how do you release it? Do I use "= Nothing" like this:

Set frmUserFormName = Nothing

Would "Set as New" be equivalent to "Load" ?

Thank again.

Bob Phillips
09-12-2007, 10:10 AM
I'm not sure I understand "your form variable is destroyed" but I'm kind of slow on the uptake for this kind of stuff.

The form variable is the object variable that you assigned to the form, frmMyForm in your example. You know about it, because if you reference the form variable later, it will error, or you test it for nothing. If it is referencing the form directly and that gets destroyed, reference it later and it is re-loaded, due to its implicit nature.


I'm assuming that "Unload Me" properly releases it... But if I do it the other way, how do you release it? Do I use "= Nothing" like this:

Set frmUserFormName = Nothing

Would "Set as New" be equivalent to "Load" ?

No it doesn't. It removes the form from memory, but you still have the form object which needs to be released as you show.

Dr.K
09-12-2007, 11:01 AM
Hmmmm...

So, regardless of wether or not I assign a separate object variable, the best way to release a form is both unload and set = nothing?

First method:

Unload MyForm
Set MyForm = Nothing


Second method:

Unload frmMyForm
Set frmMyForm = Nothing

Is this correct?

Bob Phillips
09-12-2007, 11:54 AM
You have to hand control back to the initiator, so if you show the form, you either have to hide it or unload it to give focus back.