Question : UserForm Initialization

Experts,
Given that the command Application.EnableEvents does not work within a UserForm, I read that one could create a Public EnableEvents within a UserForm and then use this variable as a "check" for each Control object, if one does not want these to be activated by another module.

However, whether I use the command UserForm.Show, or manually try to step through the code (F8)., the initialize program does not seem to run. The EnableEvents variable stays at its default of False.  I was under the assumption that the Initialize Sub was the first to run when a UserForm was called.

Below is my code.  Would you have any idea why this is occurring?

Thanks,

Cook
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
USerForm Code

Option Explicit

Public EnableEvents As Boolean
------------------

Private Sub frmHistology_Initialize(ByVal txt_Blocks As Integer, Optional ByVal Instructions As String, Optional PIName As String, Optional ptrPID As Range, Optional ListBox2 As Variant)

On Error GoTo mError
Me.EnableEvents = True
txt_Blocks.Value = 0
frmHistology.txtAmount.Value = UserTime

mError:

End Sub

------------------------

Sub within UserForm

Private Sub btn_Quit_Click()
If Me.EnableEvents = False Then
            Exit Sub
        End If
Unload frmHistology
        
End Sub

Answer : UserForm Initialization

Only if the form is unloaded will the initialization code run. To keep a form from unloading, use Me.Hide instead of Unload Me when done with the form (clicking Cancel).

Kevin
Random Solutions  
 
programming4us programming4us