Should you always load a userform before showing it?

What is the advantage of loading the userform and then showing as opposed to
just showing?

Thanks.

Re: loading userforms by Doug

Doug
Mon May 21 12:28:33 CDT 2007

That's a pretty contentious subject and opinions vary. I have never had a
problem just using formname.Show, but here is a different view from one
Malcolm Smith:

By magic form I mean the practice of intantiating an object
implicitly.

So example, say you create a form class called frmLetter then the
wrong way to instantiate the form would be:

frmLetter.Show

This is because frmLetter is a CLASS and not an OBJECT. What happens
is that a default object is created. There are a load of issues with
this method.

The first is that the scope of such an implicit object is global.
Now, one of the whole point about writing proper code is that objects
and variables have their own scopes - all designed to be as tight as
possible.

If you go and bust the whole scope open then (a) it's bad programming
practice and (b) people can actually access the form from anywhere
within the code. Also, it goes to show other developers that the
programmer has a problem distinguishing between classes and objects.
Not good for one's reputation.

The other problem; one that I have seen time and time again is that
quite often the implicit object is not destroyed in memory if the same
template is run again within the same session. For example, the last
time I was called out to solve this problem was to a London Law Firm
who were complaining that the users were calling up a letter or fax
template and that the information entered in the fields last time were
there for the next letter.

The problem was with Magic Forms and the way that the programmer
called them. The answer is to create an object explicitly:

Dim oForm as frmLetter

Set oForm = New frmLetter
oForm.txtDate.Text = Format$(Date(), "d mmmm yyyy")
oForm.Show vbModal
' ----------------------------------
Unload oForm
Set oForm = Nothing


Then everything is nicely wrapped up at the end and there is nothing
horrible hanging about in memory afterwards.

It is not a co-incidence that in .Net magic forms are no longer
allowed. One has to create an explicit object. The general response
from the VB community was "finally they have fixed this".

Magic forms are one of the dreadful shortcuts and defaults permitted
(nay, encouraged) by Microsoft and they never, ever, should be used.
There are a number of these diseased defaults in Word and over the
course of the New Year I will itemise these on my web site.

But, please believe me that using magic forms (i.e. implicitly calling
the Class) is not good programming at all. I have not met anyone who
who disagrees with me and can put forward a valid reason. All of the
serious VB and VBA developers that I know and respect all say the same
thing "magic forms are a spawn of the devil and should be avoided".

Until someone can give me a good strong reason why magic forms are a
good idea this will be my stance. And, believe me, I and others
better than I have thought long and hard about this.


As an example of another diseased possibility in VB/VBA. Why on
earth is Option Explicit by default turned off?

Anyway, the only reason why I won't do the site now is that tomorrow
it's my birthday and I think that I deserve going out to celebrate
another year gone...




--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

"red6000" <red1000002001@yahoo.com> wrote in message
news:4651cadc$0$22402$c3e8da3@news.astraweb.com...
> Should you always load a userform before showing it?
>
> What is the advantage of loading the userform and then showing as opposed
> to just showing?
>
> Thanks.
>



Re: loading userforms by Perry

Perry
Mon May 21 16:08:27 CDT 2007

Regular VB developers always use " polymorphism"

Check out
http://en.wikipedia.org/wiki/Polymorphism_(computer_science)
to get a some understanding of this term.


--
Krgrds,
Perry

System:
Vista/Office Ultimate
VS2005/VSTO2005 SE



"red6000" <red1000002001@yahoo.com> schreef in bericht
news:4651cadc$0$22402$c3e8da3@news.astraweb.com...
> Should you always load a userform before showing it?
>
> What is the advantage of loading the userform and then showing as opposed
> to just showing?
>
> Thanks.
>


Re: loading userforms by Jonathan

Jonathan
Tue May 22 08:17:17 CDT 2007


"red6000" <red1000002001@yahoo.com> wrote in message
news:4651cadc$0$22402$c3e8da3@news.astraweb.com...
> Should you always load a userform before showing it?
>
> What is the advantage of loading the userform and then showing as opposed
> to just showing?
>

When you Load a form, just the Initialize event occurs. When you then show
it, the Activate event occurs.

if you Show the form directly, the Initialize event occurs immediately
followed by the Activate event.

There are occasions when you would want to do this - you might have changes
you want to make to the form that depend on the context in which you are
calling it - e.g. different caption, or textbox contents. You can achieve
that by something like this

Load myForm
With myForm
.Caption = "My Caption"
.TextBox1.Text = "My custom text"
.Show
End With


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


Re: loading userforms by red6000

red6000
Tue May 22 15:36:58 CDT 2007

Thanks Jonathan,

I have found that I can amend elements of a userform before the form is
loaded or displayed. ie the code you gave below works equally as well
without the load statement. Am I heading for a fall if I do this without
the load statement?

I always use unload to clear the memory.

"Jonathan West" <jwest@mvps.org> wrote in message
news:%23owvcUHnHHA.960@TK2MSFTNGP03.phx.gbl...
>
>
> There are occasions when you would want to do this - you might have
> changes you want to make to the form that depend on the context in which
> you are calling it - e.g. different caption, or textbox contents. You can
> achieve that by something like this
>
> Load myForm
> With myForm
> .Caption = "My Caption"
> .TextBox1.Text = "My custom text"
> .Show
> End With



Re: loading userforms by Jonathan

Jonathan
Tue May 22 16:15:20 CDT 2007


"red6000" <red1000002001@yahoo.com> wrote in message
news:465353f9$0$32276$c3e8da3@news.astraweb.com...
> Thanks Jonathan,
>
> I have found that I can amend elements of a userform before the form is
> loaded or displayed. ie the code you gave below works equally as well
> without the load statement. Am I heading for a fall if I do this without
> the load statement?

Not particularly, so long as you realise what is going on. If you use the
default instance of a form, then any mention of the form's variable name
will load the form if not already loaded, and will at that time fire the
Initialize event. You can control when the Initialize event is fired by
using the Load statement if you wish to.


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


Re: loading userforms by Terry

Terry
Wed May 23 18:01:03 CDT 2007

Pre-loading the userform, IMHO, will allow you to time when the form will
appear.Such as when your program has to load allot of data, tell your
program to load the userform before loading the data. Then, when the
dataload is complete, you show your form quickly then, not earlier.

Terry

"red6000" <red1000002001@yahoo.com> wrote in message
news:4651cadc$0$22402$c3e8da3@news.astraweb.com...
> Should you always load a userform before showing it?
>
> What is the advantage of loading the userform and then showing as opposed
> to just showing?
>
> Thanks.
>