https://www.excelfox.com/forum/showt...ll=1#post24187
https://www.excelfox.com/forum/showt...age3#post24187
Initiataelize Starting
Before dragging any template sketches from the tool box, we can take a look at what is available for coding associated primarily with the main grid, that is to say the coding "behind" the main form.
One of the first ones that is perhaps worth an initial consideration is the Initialize Event of the VBA UserForm. To get the possibility to add to the coding for that ,
_ Right-click on the main UserForm grid showing in the VB Editor, and select View Code from the menu.
_ In the Dropdown list on the left above the main Window, select UserForm.
_ This will typically give something for you to add to the UserForm_Click() event. You can ignore this.
_ In the Dropdown list on the right above the main Window, select Initialize.
(_ Optional: Delete the UserForm_Click() sub which appeared in step 2. The event coding for this and all the other available will be there anyway on an instantiated object , at this stage it is just a bit of worthless text ignored when the Class is used to make an actual object )
Remember as ever, this is all just text, which will never itself be anything of significance. It is just used later by a user New, ( or the effective automatically done one )
https://i.postimg.cc/264GZpGV/Initia...-User-Form.jpg https://i.postimg.cc/05YX6SXc/Initia...-User-Form.jpg
Initialize Event of the VBA UserForm.jpgInitialize Event VBA UserForm.JPG
Code:
Private Sub UserForm_Initialize()
End Sub
As with all the event coding that is already available to us in VBA, this coding ios there with things in it that Microsoft prefer us not to see. This is understandable as it would be proprietary information and part of what is effectively the software Office Excel. But we can add our coding to it, for example a simple message box.
We will do this in a way to help us once again get clear the point about the possibility to neglect an instanciating,
Currently we have in the example file uploaded in the last post, two UserForm Classes, ufResults and UserForm1
We will put the same coding in both. This will be a message box to tell us the class name
Code:
Option Explicit
Private Sub UserForm_Click()
End Sub
Private Sub UserForm_Initialize()
MsgBox Prompt:="You just Initiataelized an object from the class " & Me.Name
End Sub
https://i.postimg.cc/qBK7WV5R/uf-Results-Initialize.jpg https://i.postimg.cc/8PVCSK2b/User-Form1-Initialize.jpg
UserForm1_Initialize().JPGufResults_Initialize().JPG
That coding will tell me the class name of the particular UserForm being "made"
Now we will do the coding to Instanciate, that is to say, "make" as it were, an object of each class. Remember this is generally what happens by the [color=Blue]New[/color] bit of the typical early declaration coding at the start of a coding to use the UserForm who’s class we made. We want to do this in two different ways, so as to demonstrate once again the confusing phenomena of a UserForm self instantiating if you neglect to do it.
These next codings can go inside any of the existing default modules.
Code:
Option Explicit ' https://www.excelfox.com/forum/showthread.php/2965-Class-related-Stuff-Userforms?p=24187&viewfull=1#post24187
Sub Instanciate_ufResults()
Dim Fm As ufResults
Set Fm = New ufResults ' Fm becomes the object variable pointing to an actual "thing" or object which the New efferctively caused to be "made"
End Sub
Sub Instanciate_UserForm1()
MsgBox prompt:=UserForm1.Visible ' This code bit =UserForm1.Visible effectively does the followoing
' UserForm1. ' This effectively causes the next two lines to be done internally as it were
' Dim UserForm1 As UserForm1
' Set UserForm1 = New UserForm1 ' UserForm1 becomes the object variable pointing to an actual "thing" or object which the New effectively caused to be "made" from using the class UserForm1
' MsgBox prompt:=UserForm1.Visible ' (This should return False because the form , is not visible)
End Sub
Bookmarks