View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Tushar Mehta Tushar Mehta is offline
external usenet poster
 
Posts: 1,071
Default UserForm Initialize event doesn't fire reliably

In article ,
says...
{snip}
Also, to show that USerForm1 is a class like any other one, you can create
several objects of that class at the same time (put a msgbox in the
Initialize event of the userform):
Dim frm1 as Userform1, frm2 as Userform1
Set frm1=new userform1 '<-- initialize fires for frm1
frm1.Show vbModeless '<-- show frm1
Set frm2 = new userform1 '<-- initialize fires for frm2
frm2.show vbmodeless '<-- show frm2 while frm1 is still showed

Nice example. Though, while a UF is a class it is a special class. I
remember running into some differences between a UF and a normal class
some time ago though I cannot recall what.

One that comes to mind and I just tested is that one can 'Load UF1' but
not 'Load Class1'.

--
Regards,

Tushar Mehta
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions

In article ,
says...
Hi,
Usually when you have a class say Class1, the Initialize event is fired when
an object of that calss is created (ie when some space is allocated in
memory). Eg:
Dim a as Class1 '<--- declaration, object not created yet
Set a = new Class1 '<--- object created, Initialize fires
With userforms it is similar, except one thing which can be a bit confusing.
Think about the form you have designed as a class (and it IS one), say
UserForm1. When you use the word Userform1 in a sub, vba automatically
creates a variable Userform1, ie a variable that has the same name as its
class. This is a default behavior. That is, vba creates the object the first
time you use it in your code and this is when the Initialize fires. It can be
because of a Show, a Visible, or a Caption request.
If you prefer to manage the behavior by yourself, consider using:
Dim frm as Userform1
Set frm = New Userform1 '<-- Initialize fires
Also, to show that USerForm1 is a class like any other one, you can create
several objects of that class at the same time (put a msgbox in the
Initialize event of the userform):
Dim frm1 as Userform1, frm2 as Userform1
Set frm1=new userform1 '<-- initialize fires for frm1
frm1.Show vbModeless '<-- show frm1
Set frm2 = new userform1 '<-- initialize fires for frm2
frm2.show vbmodeless '<-- show frm2 while frm1 is still showed

Hopefully, i was clear enough,
Sebastien

"RB Smissaert" wrote:

Finally discovered that the Initialize event of VBA userforms doesn't always
fire at the time when you expect it to fire.
I had some strange bugs that I couldn't figure out as I just presumed that
if you did Load Userform1 the initialize event
would fire and also that it would fire at no other occasion.
I found that for example it could fire when you do:

If Userform1.Visible = True Then

I have no completely bypassed the Initialize event and just made a normal
Sub that does the same and run this
just before doing Load Userform1.
Seems that this is much better.
I am dealing with a large complicated .xla add-in here and maybe the above
doesn't apply to simpler projects.


RBS