Consulting

Results 1 to 7 of 7

Thread: Calling UserForms: FormName.Show or Dim Object/Set New?

  1. #1
    VBAX Contributor
    Joined
    Jun 2007
    Posts
    150
    Location

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

    The first way I learned to call a UserForm is to just use the name of the form:

    [vba] FormName.show [/vba]

    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:

    [vba] Dim frmMyForm As FormName
    Set frmMyForm = New FormName [/vba]

    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.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    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.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    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.
    Regards,
    Rory

    Microsoft MVP - Excel

  4. #4
    VBAX Contributor
    Joined
    Jun 2007
    Posts
    150
    Location
    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:

    [vba]Set frmUserFormName = Nothing[/vba]

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

    Thank again.

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Dr.K
    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.

    Quote Originally Posted by Dr.K
    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:

    [vba]Set frmUserFormName = Nothing[/vba]

    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.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    VBAX Contributor
    Joined
    Jun 2007
    Posts
    150
    Location
    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?

    [vba]First method:

    Unload MyForm
    Set MyForm = Nothing


    Second method:

    Unload frmMyForm
    Set frmMyForm = Nothing[/vba]

    Is this correct?

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    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.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •