Unloading Form Runs Forms Initialize Event, Why?

Unloading Form Runs Forms Initialize Event, Why?

Anonymous
Not applicable
1,800 Views
5 Replies
Message 1 of 6

Unloading Form Runs Forms Initialize Event, Why?

Anonymous
Not applicable
I just realized that unloading my VBA forms via
Unload frmMain, causes frmMain's initialize
event to run. Why is this? Can I keep it from
happening?

Thank You,
Paul
0 Likes
1,801 Views
5 Replies
Replies (5)
Message 2 of 6

Anonymous
Not applicable
Paul,

Within the ACAD VBA help docs you'll see what basically says "Don't expect ACAD events to be when and how you think they are" not ver-b but close. My interpretation of that portion of the help files is that we will not be informed so don't bet the bank.

I don't think you have a work around to this and in searching for resolve I found other characteristics. I did the following which made me believe we will never have control over ACAD event sinking or even be able to give a good guess

1 - you can't cast your own defined event but only work with ACAD doc events

2 - if you set your IDE at a breakpoint and leave your machine...my test left the IDE for 20 minutes at a breakpoint call to another dll....when I returned and hit F8...a message informed me my IDE session was no longer going to work (forget the exact message). This made me feel that there are many event sinks going on behind ACAD that could be called event controls because they terminate standing states of the IDE. What other states do they control?

just my .02 cents having seen interesting charactersitics with events.

Bob
0 Likes
Message 3 of 6

Anonymous
Not applicable
I'm guessing that frmMain is an "As New" variable that is not loaded until you reference it by trying to unload it. How do you load and show the form?
0 Likes
Message 4 of 6

Anonymous
Not applicable
"Can I keep it from happening?"
How about a module level boolean that would be true when the terminate event fires.

[code]
Private Sub UserForm_Initialize()
If mbIsClosing = False Then
'your code here
End If
End Sub


Private Sub UserForm_Terminate()
mbIsClosing = True
End Sub
[/code]
0 Likes
Message 5 of 6

Anonymous
Not applicable
Using Unload method to unload a form DOES NOT cause UserForm's Initialize
event. Reply from "fantum" points out your problem.

It is obviously in your code

Unload frmMain

the Form represented by variable "frmMain" is not loaded previously (the
form you loaded couls be loaded by "Set xxxx = New xxxx" ). So, when this
line of code is run, VBA first load the form since it is not loaded, then
unload it.

VB/VBA sometime does too much for programmers: such as: you do not need to
explicitly load a form, it get loaded automatically when your code refers to
its property/method; Dim xx As New xxxxx is another example. Good
programming practice is to avoid to let VB/A does too much for you, take
control in your own hand.

Carefully exame how the said form is loaded and how it is unloaded.


"Paul Richardson" wrote in message
news:4861808@discussion.autodesk.com...
I just realized that unloading my VBA forms via
Unload frmMain, causes frmMain's initialize
event to run. Why is this? Can I keep it from
happening?

Thank You,
Paul
0 Likes
Message 6 of 6

Anonymous
Not applicable
Yes Fantum...Thanks All. I was calling unload again accidently after
the from was unloaded. This was triggering the initialize
event. Although as Bob mentioned when this fires is not a given
so Oberer's suggestion is nice as a catch.

wrote in message news:4862060@discussion.autodesk.com...
I'm guessing that frmMain is an "As New" variable that is not loaded until
you reference it by trying to unload it. How do you load and show the form?
0 Likes