Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 30

Thread: Class related Stuff Userforms

  1. #11
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,458
    Rep Power
    10
    For later use

    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
    https://www.youtube.com/watch?v=QdwDnUz96W0&lc=Ugx3syV3Bw6bxddVyBx4AaABAg
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=UgxsozCmRd3RAmIPO5B4AaABAg.9fxrOrrvTln9g9wr8mv2 CS
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=Ugw6zxOMtNCfmdllKQl4AaABAg
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=UgyT1lo2YMUyZ50bLeR4AaABAg.9fz3_oaiUeK9g96yGbAX 4t
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=Ugx5d-LrmoMM_hsJK2N4AaABAg.9fyL20jCtOI9g7pczEpcTz
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=UgyT1lo2YMUyZ50bLeR4AaABAg.9fz3_oaiUeK9g7lhoX-ar5
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=Ugx5d-LrmoMM_hsJK2N4AaABAg.9fyL20jCtOI9gD0AA-sfpl
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=Ugx5d-LrmoMM_hsJK2N4AaABAg.9fyL20jCtOI9gECpsAVGbh
    https://www.youtube.com/watch?v=U76ZRIzBhOA&lc=Ugw6zxOMtNCfmdllKQl4AaABAg.9g9wJCunNRa9gJGhDZ4R I2
    https://www.youtube.com/watch?v=Sh1kZD7EVj0&lc=Ugz-pow-E8FDG8gFZ4l4AaABAg.9f8Bng22e5d9f8hoJGZY-5
    https://www.youtube.com/watch?v=Sh1kZD7EVj0&lc=Ugxev2gQt7BKZ0WYMfh4AaABAg.9f6hAjkC0ct9f8jleOui-u
    https://www.youtube.com/watch?v=Sh1kZD7EVj0&lc=Ugxg9iT7MPWGBWruIzR4AaABAg
    https://www.youtube.com/watch?v=tzbKqTRuRzU&lc=UgyYW2WZ2DvSrzUKnJ14AaABAg
    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA



    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
    https://www.youtube.com/watch?v=UywjKEMjSp0&lc=UgxIySxHPqM1RxtVqoR4AaABAg.9edGvmwOLq99eekDyfS0 CD
    https://www.youtube.com/watch?v=UywjKEMjSp0&lc=UgxIySxHPqM1RxtVqoR4AaABAg.9edGvmwOLq99eevG7txd 2c
    https://www.youtube.com/watch?v=SIDLFRkUEIo&lc=UgzTF5vvB67Zbfs9qvx4AaABAg
    https://www.youtube.com/watch?v=9P6r7DLS77Q&lc=UgzytUUVRyw9U55-6M54AaABAg
    https://www.youtube.com/watch?v=9P6r7DLS77Q&lc=UgzCoa6tOVIBxRDDDbN4AaABAg
    https://www.youtube.com/watch?v=9P6r7DLS77Q&lc=UgyriWOelbVnw4FHWT54AaABAg.9dPo-OdLmZ09dc21kigjmr
    https://www.youtube.com/watch?v=363wd2EtQZ0&lc=UgzDQfo5rJqyVwvv2r54AaABAg
    https://www.youtube.com/watch?v=363wd2EtQZ0&lc=UgzHTSka7YppBdmUooV4AaABAg.9cXui6zzkz09cZttH_-2Gf
    https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgxhXnQ-mWYhrHWuM354AaABAg.9bepnegjnRu9iMmBDtf4m1
    https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgxFIZ858qf7w_uA9bd4AaABAg.9dKpEpUk3YT9dVEGnka6 yj
    https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=Ugz8oC8iGd6-SPhpaQZ4AaABAg.9bhRt-kPXri9brzh_99JF9
    https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=Ugz8oC8iGd6-SPhpaQZ4AaABAg.9bhRt-kPXri9bsrQIgXb3L
    https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgzFkoI0n_BxwnwVMcZ4AaABAg
    https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgxwJDkFskrMW8EpcXt4AaABAg.9bmKMz5-Z1g9bmx0REIz41
    https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgxhXnQ-mWYhrHWuM354AaABAg.9bepnegjnRu9bmyko2YUvQ
    https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgxwJDkFskrMW8EpcXt4AaABAg.9bmKMz5-Z1g9bmzpPqfLRD
    https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgzZwbV_Y_7UFzHwNBh4AaABAg.9dKb0Vc7MOB9dVK8si3o nt
    https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=Ugx6Ec_r4kb9EYOVgIt4AaABAg.9dOW613fb8V9dVIJECZI dC
    https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgwBho9tBLQ4nPVdYqd4AaABAg.9fWvoBWY3Da9g9cLjhPi az
    https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgzZy1NAMBx5Uv4U2cJ4AaABAg.9f0XX-_JaGp9g9bYLMZiIy
    https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgyL-xp8IiiahmQ12kJ4AaABAg.9f7xHCpAEx29g9asFhVFfT
    https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgxRxyFNNp3WHTzuiJJ4AaABAg.9fFR6ECmXk69g9afNBcS 4Z
    https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgwsdMh0FGDfvA249_B4AaABAg.9fLR6FHCIVI9g9aLlUyz og
    https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgwBho9tBLQ4nPVdYqd4AaABAg.9fWvoBWY3Da9g9_4422N zK
    https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=Ugwyy8JXr56HJ8m_od94AaABAg.9gSFgqqJQNV9gTXco41b 5l
    https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgyS8stMz5B9NrpPrbR4AaABAg.9gOjiS0rs8l9gTYl6Rld pA
    https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgyS8stMz5B9NrpPrbR4AaABAg.9gOjiS0rs8l9gTfhAWU9 ju
    https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgyS8stMz5B9NrpPrbR4AaABAg.9gOjiS0rs8l9gTfuYQGm Ua
    https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgyS8stMz5B9NrpPrbR4AaABAg.9gOjiS0rs8l9gTg3AmMP Uc
    https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgyS8stMz5B9NrpPrbR4AaABAg.9gOjiS0rs8l9gTgEqh5w do
    https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgxmUK0S_aZVZWz8-gt4AaABAg.9gLc3DfWfHl9gTZ3y6fL1H
    https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgzZloYeY2wQr7-xTOh4AaABAg.9gB2bbbs9mB9gTZUkNYI8e
    https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgzlM96nGEhW9J1Gpgd4AaABAg.9fmOFVcXZh49gT_8CYeQ gz
    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
    A Folk, A Forum, A Fuhrer ….

  2. #12
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,458
    Rep Power
    10
    https://www.excelforum.com/excel-pro...ml#post4382894
    https://www.excelforum.com/excel-pro...ml#post4383066
    https://www.excelfox.com/forum/showt...ll=1#post24176
    https://www.excelfox.com/forum/showt...ll=1#post24178




    Quote Originally Posted by romperstomper View Post
    ……………… As soon as you use ufResults for the first time, a new instance of the form is created. It's confusing because you effectively get a variable of the same name as the class. So that instance was not the same as the fm instance. (If you'd added a Debug.Print Userforms.Count line in there, you'd have seen 2 forms)

    For a simple demo, in a new workbook, add a blank userform, then add this code and run it:
    Code:
    Sub foobar()
        Dim fm As UserForm1
        Set fm = New UserForm1 ' created an instance of the form
        MsgBox UserForms.Count ' you'll see 1 here
        UserForm1.Caption = "Loaded another instance" ' this loaded a new instance of the form and assigned a variable called Userform1
        MsgBox UserForms.Count ' so you'll see 2 here
    End Sub
    Thanks Rory.
    That is very helpful. These niggly small discrepancies can be very annoying.
    Nice to get them cleared up
    This also clears up some problems i had once when looking at different Add-Ins and getting confused which one was running. I had been changing the class UserForm, rather than the Instance of it. Or the other way around, or whatever... At the end of the day I thought I was changing a code somewhere I was not. ( I sorted all that out by exporting the UserForm , changing the name of the in the File then importing the original back.....and then being very careful to check where I was when I changed anything )
    _
    So Fuck he’s off agiain...

    In these two lines The first line changes a Property of the instance, which then “dies” when the File closes. No Changes are made to the User Form ( Class ).
    So when I “make this “ instance we have a “ByValue Call” here, as it were, passing a Copy of the User Form to the screen.
    Not 100% sure, yet what a Load is... I have a Load of...____x ... when...?

    The second code line seems to have no effect. ( I guess I have to change things to the class manually )
    fm2.Caption = "Poo"
    ufResults.Caption = "Poo"

    I said The second seems to have no effect
    _........
    So I thought it did nothing.. now I know better..
    So
    _ ....I did what you said, added a User form to a new File, ( and I added a button.. because I could )
    I get a Variable of that name which refers to a new instance, but then I cannot seem to add a caption to it... or I can , you did.. and with my modified version of your code below I see “it” . So maybe it is indeed suddenly a new instance with the same name. And as it is an Instance, when I save and close and reopen the File, I find that the name on UserForm1 is “UserForm1”, and not as I aptly named the instance in the code below:
    "Loaded another instance annoyingly with the same name of The User Form Class" as in the VB Project .
    Code:
    Option Explicit
    Sub foobar_Loadof____() '                               http://www.excelforum.com/showthread.php?t=1138300&p=4382894#post4382894
    Dim fm3 As UserForm1:                                  '                                           '                                                  ' Preparing a "Pointer" to an Initial "Blue Print" in Memory of the Object ( Pigeon Hole with a bit of paper or code lines on that can be filled in to refer to a specific Objec of this type ) . This also us to get easily at the Methods and Properties throught the applying of a period ( .Dot) ( intellisense )
    Set fm3 = New UserForm1 '                              Created an instance of the form                         Still just an "un Filled in" Blueprint. A copy of the Blue print
    MsgBox UserForms.Count '                               you'll see 1 here
        If Not fm3.Visible Then fm3.Show False            'I see my new instance, ByVal Copy of the UserForm1, A Load of_____
    fm3.Caption = " I Loaded this fm3 instance "
    MsgBox UserForms.Count '                               see I have 1
    Dim v: v = UserForm1.CheckBox1.Value                   'Always will return False,  ( unless I check it on this instance ), - A Load of it has just been done ;)
    MsgBox UserForms.Count                                 ' I see I have 2
        If Not UserForm1.Visible Then UserForm1.Show False 'I see something, and... either this line or the next or the previous were capable of making it. The last one did
    MsgBox UserForms.Count '                                I see I have 2
    UserForm1.Caption = "Loaded another instance annoyingly with the same name of The User Form Class" ' this loaded a new instance of the form and assigned a variable called Userform1
    MsgBox UserForms.Count '                                so you'll see 2 here - either of the last three lines will cause this
    '
    Dim fm4 As UserForm1
        '    If Not fm4.Visible Then UserForm1.Show False'  This will not work   The fact that I can create an instance of the Class ( With the name of the class ) without the   Dim Set   pair is an interesting oddity.
    End Sub
    _.........................

    I think I really have this now as far as I am ever likely to need. Thanks very much. But I think in this case you will agree with me that we are indeed talking a “Load of ..... “ . This goes right back to your original answer which ,
    Originally Posted by rorya
    HOw/when are you loading the form? If it's not still loaded when you run that code, you will get False.

    There you were talking a Load of..... i was also talking about a Load of...... Unknowingly I had created another instance in testing for ufResults button values, A Load of ... ByVal .. Copy of... and I had been doing checks on that instance rather than that instance which was loaded.
    ( So I expect I cannot change the Class Properties by code, or not at any rate through UserForm1.___ as that refers to the instance. ( and even if I could do such , I do not see the point. Doing it manually is easy enough.. ) )

    The fact that I can create an instance of the Class ( With the name of the class ) without the
    Dim
    Set

    pair is an interesting oddity. But who knows... might have its uses.
    _................................................. ............

    I think I answered my other question as well, - How to uncheck a Button with a code. If a button is checked, I can uncheck it with
    Let fm2.Refresh.Value = False ‘ Refresh is the Button name

    That does have the annoying habit of kicking off the code behind it. But if I do not want that then this in the first line sorts that out
    If Refresh.Value = False Then Exit Sub

    Thanks again
    Alan
    Last edited by DocAElstein; 06-01-2024 at 07:32 PM.

  3. #13
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,458
    Rep Power
    10

    https://www.excelfox.com/forum/showt...ll=1#post24179
    https://www.excelforum.com/excel-pro...ml#post4383265
    https://www.excelforum.com/excel-pro...ml#post4383231




    Quote Originally Posted by romperstomper View Post
    You can change the class properties through code, using the Designer object.
    romperstomper: - You can change the class properties through code, using the Designer object.


    [QUOTE=Doc.AElstein;4383265]OK, thanks, can't think why I would want to do that, but worth knowing.
    Actually I was just going to Edit BTW. my last post and say I have it well solved now thanks .....
    _ the last bit I wanted I just did and was easy now. -As I had changed my Declaration of fm to be a Globie, as part of the Experiments today.( and like you originally said
    Originally Posted by rorya
    ..... need access to these from other routines, ..move ..declaration of fm appropriate...

    ....... I was able to just add a few lines to create the fm instance and check the button I wanted to on opening the file in the This Workbook code in the Workbook_Open code thus:

    Code:
        If fm Is Nothing Then Set fm = New ufResults ' if this line was not yet passed, then a new instance of the class ufResults with the name fm is made
        If Not fm.Visible Then fm.Show False 'make sure UserForm is always there,
    fm.OptionButton2.Value = True
    So well all sorted. Should keep me out of harms way with my project now for a while
    Thanks again for all your help.

    Alan
    Last edited by DocAElstein; 06-01-2024 at 07:41 PM.

  4. #14
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,458
    Rep Power
    10
    This is what I got. Rory did it for me
    Attachment 5858Attachment 5858


    Attachment 5859
    [code][color=black]'Static fm As ufResults ' instead of this I have it as a Global variable so that i can check it elsewhere, and I add it and check a box on opening the file
    'Dim fm As ufResults ' This would result in the UserForm "Dieing" a t every code ending
    If Fm Is Nothing Then Set Fm = New ufResults ' if this line was not yet done, then a new instance of the class ufResults with the name fm is made
    If Not Fm.Visible Then Fm.Show False 'make sure UserForm is always there,
    [color=darkgreen]'Start of Part only done for entry in the three C column Ranges and no check for Calculate all else. -- Things here are that can be done quickly, 1 "row" stuff!! http://www.excelforum
    Last edited by DocAElstein; 06-02-2024 at 03:07 PM.

  5. #15
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,458
    Rep Power
    10
    https://www.excelforum.com/excel-pro...ml#post4384440
    https://www.excelfox.com/forum/showt...ll=1#post24181





    Problem and Solution Summary

    Based on the Thread Title I think a Summary could be useful for anyone coming here on a Google Search. This was a subtle problem that caught me out. Explaining it and the solution is a nice Explanation of the Basic Class, instancing thing.

    First the Problem and answer, and then a bit of explanation. ( Names and the exact progression of what went on are changed a bit to protect the innocent and make this a bit clearer )

    _1a ) Problem:
    So I “had a UserForm.” – ( ( To “get” a UserForm ... in VB Editor .. Insert .. UserForm )
    This “thing” gets a default Name, UserForm1. I see that name in a few places.
    I double click on UserForm1 in the VB Editor, and without too much effort I can “ “drag” a check box into it “. “It” gets the Name CheckBox1. . I see in the VB Editor ( Alt + F11 ) the following:

    Attachment 460117
    The above is effectively an empty Blue Print form to represent a Class of Objects. It will be “used” by VBA as a guideline for when it creates such Objects, helping it to assign memory, has code instructions for what to do in later events associated with such Objects, how to store , use them ( in this case , for example, how to “show” it ) etc. ... etc - all to be explained further......

    So in my origianly problem, I tried to check in a code if the CheckBox1 check box is checked ( has a tick in it ) . I tried that with this code line, and everyone and every Google search told me it was how to do it

    If UserForm1.CheckBox1.value = True Then ______

    But that always gave me False.

    _1b) The Solution:

    Something like this did work

    If fm1.CheckBox1.Value = True Then

    _1c) What to do if you have this problem .. : check if your instancing of the actual userForm you are interested in has been done Explicitly, and not left to the Implicit Default

    _..................

    To er labberate
    _2 ) What’s going in?

    I got caught out because ( at least indirectly ) of the following VBA habit:
    VBA tends to guess what you want when you leave things out. ( the compiler tries to determine what it is you're asking it to do ). This is good sometimes. But then relying on these Implicit defaults can “bite you when you least expect it”. In addition I fear that this results in People forgetting what VBA does . The UserForm was done for me by someone who knows what he is doing and does not rely on the Implicit defaults. I think much of what I Googled had forgot ... something here....
    In all that lies the key to the problem I had


    That “thing” “called” UserForm1 up there is a Class. That is to say it is a Blue Print, or a form, or a questionnaire not yet filled in, a template ... .. etc. It does not really exist in the terms of a Final product. It is just describing how something of that form or type would be.

    In VBA you “Dim” things. In doing so you usually give:
    a)
    Variable : , a name, say fm1 if we are talking about UserForm things , or ws if we are talking about Worksheet things, and
    and
    b )
    A Type : , what sort of thing is it.

    For example as in these this “pseudo” code Lines

    10 Dim fm1As AnExistingBluePrint
    There is no Blue Print “AnExistingBluePrint” , that is just made up for now.
    11 Dim ws As AnExistingFilledInBluePrint ‘ There is no Blue Print “AnExistingFilledInBluePrint” , that is just made up for now.

    ( This prepares memory, instructions etc... for something of that and instructs how generally to deal with it. , and allows me to use intellisense through typing a period ( . ) to get a selection to choose from the available Methods and properties of that Object . For any particular Blue Print, the memory required may not be too different for an empty or filled in Blue Print. But there could be differences, that change further, when for example, things like strings are concerned that may vary considerable in length. Part of the instructions will explain how to handle such awkward things )
    _........................
    For the case of Line 10, we will be filling in from scratch our Blue Print, and do not want to mess up the original so need a copy of it.
    For the case of line 11, we are talking about the equivalent of like taking a UserForm Blue print and modifying it and filling it in such that it could be used to “make” a Worksheet. In such a case it is used by Excel itself to "do that" 1 - 3 times, every time I open up Excel !!
    So now I need a line to either copy a fresh Blue Print for An Existing Blue Print or pass over the Existing Filled In Blue Print
    Here we go

    20 Set fm1 = New UserForm1
    21 Set ws = Worksheets(“Sheet1”)


    That’s enough background there.
    _.............................

    2b) Implicit defaults for UserForms.
    Now, although one might want to, it is not so typical that one finally wants to “see” or “use” more than one UserForm of a particular form and layout at the same time. If I did , these would be the appropriate code lines for a UserForm like the one I made a Blue Print Copy for above

    10 Dim fm1 As UserForm1
    11 Dim fm2 As UserForm1
    20 Set fm1 = New UserForm1
    21 Set fm2 = New UserForm1


    If now I want to change any properties I can do so as in the following code line, for example, to change the state of the CheckBox1 ( say to check it - put a tick in it ), these would work, ( note each line is working on a different Object, a diffferent instance ot the Class – A differnet box is checked)

    50 Let fm1.CheckBox1.Value = True

    Let fm2.CheckBox1.Value = True
    It would not work without lines 10 and 20. It would error , moaning that the variable fm1 had not been declared.
    That sound reasonable.

    Because in most cases only one instance of a UserForm is used, VBA will, on any attempt to “use” something with the same name of the Class, Userform1, it will immediately effectively take code lines similar to the following as existing, ( this is the Implied default for when it first “sees” a code line attempting to use UserForm1 ):
    ( The two lines below is what VBA "does" effectively for you internally at Compile - you will never see these lines )
    10 Dim UserForm1 As UserForm1
    20 Set UserForm1 = New UserForm1

    This has actually declared a new variable UserForm1, which is an instance of the Class UserForm1.
    If I simply write

    Let UserForm1.CheckBox1.Value = True

    It will “work” as I am wanting, ( or at least as VBA is guessing I want. )

    If I change the name in the of the Class userForm1 ( double click on UserForm1 in the VB Project window and edit the first row in the Properties Window, typically bottom left ) to say myFormBluePrintClass then similarly in a code I can use

    Let myFormBluePrintClass.CheckBox1.Value = True

    A last passing observation, to help bring out the point about the Class. The following code will give you three instances, but they will always generally be the same. They are klones. They can only be distinguished bv the Variable names given ( ot the default one discussed abobe. ). Referrencing these variables can be used then to change the properties.
    But these are changes on the copy called into life by Dim Set based on / by Values of the Class UserForm1 rather than referring back to the oRefiginal.
    – P-Tang Pftang OLE Wigwam Biscuit Barrel Makro Du Moley Wolly Mod Pod ; )
    So they die when the UserForms are closed withh no effect on the oRefiginal Class UserForm1.
    Code: (and here
    http://www.excelforum.com/showthread...t=#post4384393
    Code:
    Sub KloneAUserFormWonks()   '         ' Post #16  http://www.excelforum.com/showthread.php?t=1138300&p=4383265&highlight=#post4383265
    Dim ws As Worksheet             '     ' Preparing a "Pointer" to an Initial "Blue Print" ( or a Form, or a Questionnaire not yet filled in, a template   etc.) in Memory of the Object ( Pigeon Hole with a bit of paper or code lines on that can be filled in to refer to a specific Object of this type ) . This also us to get easily at the Methods and Properties through the applying of a period ( .Dot) ( intellisense )
    Set ws = ThisWorkbook.Worksheets.Item(1)        ' Values are filled at the memory locations and the directions there are specified in the variable "Blue Print "Pointer". In this case a Filled in Blue Print is passed. This is the one used on Opening by Excel to call up the user Friendly Form of the Fiist Tab Worksheet counting from the left.
    Dim fm1 As UserForm1 '
    Set fm1 = New UserForm1  '                                                                        ' A copy of the Blue Print is made
    fm1.Left = 0
    fm1.CheckBox1.Value = True
    fm1.Show vbModeless         '         there are two ways ( modes ) of showing a form in vb - modal and modeless. a modal form takes the focus and won't return control to the calling module until it is closed. a modeless form is shown and then control is immediately returned to the calling module.     http://vbcity.com/forums/t/53342.aspx
    Dim fm2 As UserForm1
    Set fm2 = New UserForm1
    fm2.Show vbModeless
    fm2.Caption = "Klone fm2"
    fm2.Left = 50
        '    Dim UserForm1 As UserForm1     ' These two imaginary lines are effectively done by                                                               'The behavior (or invisible statement) serves to declare (and tell VBA to load on first use) an object variable, called UserForm1, of type UserForm1.   This is called the default instance of the class (also called the "magic form" or "self/auto initiating form" by some people).  This means that, while a userform operates in most ways like any other class, there is always an instance of the userform available for instant loading and use at any time. Any reference to that instance calls it into existence.
        '    Set UserForm1 = New UserForm1  ' VBA on compile when it sees the next line. Thes lines you would never see
    UserForm1.Caption = "This one VBA decalred and Instanced for me"
    UserForm1.Show vbModeless
    UserForm1.Height = 50
    UserForm1.Left = 200
    End Sub
    Attachment 460261




    Rem Ref http://gregmaxey.mvps.org/word_tip_p...nced_tips.html
    Rem Ref Rory as always


    _.........................................

    ¬_2c) Why can this catch one out, (why am I posting this “warning” )


    ......”........... Continued in next post..............”
    Last edited by DocAElstein; 06-01-2024 at 07:47 PM.

  6. #16
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,458
    Rep Power
    10
    ......”........... Continued from last post..............”......


    ¬_2c) Why can this catch one out, (why am I posting this “warning” )

    I expect many people will just take the UserForm they see as a “thing” and use it by its name, whether the default, such as UserForm1 , ( UserForm2 if they create a second User Form “thing” ) or the name they give it.
    I tried to access properties through the name of the “thing” I see in the VB Editor, which was actually the Class name, as it is always is.
    My Code kindly given to me by someone was more “correctly” ( IMO ) written so as not to rely on Implicit defaults, so I had something like this

    10 Dim fm1 As UserForm1
    20 Set fm1 = New UserForm1


    Subsequently code line such as
    Let UserForm1.CheckBox1.Value = True
    or in particular what was causing me problems
    If UserForm1.CheckBox1.Value = True Then
    Did not error as the first one of those used had creating a second new instance of Class UserForm1. But I was wanting to reference the first instance, fm1, as this was used further in the code for all dealings with the actual instance of the UserForm that I was interested in.

    Hope that helps anyone else Puzzled that any code line similar to this is not giving the correct results

    UserForm1.Property
    or
    myUserFormName.Property =

    _................................................. ..............


    _3) The solution / answer again to the main Question


    _3a) Question.
    UserForm.____ not working

    _3b) Answer
    If you have not yourself written the existing code, then check if your instancing of the actual userForm you are interested in has been done Explicitly, rather than left to the Implicit Default, which you are ( maybe unknowingly ) assuming

    Alan


    Edit P.s. Tell someone you are giving such a code : “I declared Explicitly the Userform, so bear that in mind if you attempt to access any of its Properties in code modifications – I did not leave it to the Implicit default of the UserForm Class name”. ( Not that would of made any difference to me – I would of just thought – “what load of old B llox is he making up now” .. Lol.. )
    Rem Ref
    http://www.excelforum.com/showthread...t=#post4383895
    Rem Ref http://www.excelforum.com/showthread...t=#post4381274
    Rem Ref http://www.excelforum.com/showthread...t=#post4381275
    Rem Ref http://www.excelforum.com/showthread...t=#post4381420
    Last edited by DocAElstein; 06-01-2024 at 07:53 PM.

  7. #17
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,458
    Rep Power
    10
    Code for last post with a few more explaining Comments and colour conventions to suit the last Posts # 16 and #17 ( Posted here due to Forum Post size limitations: )


    oRefiginal Class UserForm1.
    Code:
    Sub KloneAUserFormWonks()   '         ' Post #16  http://www.excelforum.com/showthread.php?t=1138300&p=4383265&highlight=#post4383265
    Dim ws As Worksheet             '     ' Preparing a "Pointer" to an Initial "Blue Print" ( or a Form, or a Questionnaire not yet filled in, a template   etc.) in Memory of the Object ( Pigeon Hole with a bit of paper or code lines on that can be filled in to refer to a specific Object of this type ) . This also us to get easily at the Methods and Properties through the applying of a period ( .Dot) ( intellisense )
    Set ws = ThisWorkbook.Worksheets.Item(1)        ' Values are filled at the memory locations and the directions there are specified in the variable "Blue Print "Pointer". In this case a Filled in Blue Print is passed. This is the one used on Opening by Excel to call up the user Friendly Form of the Fiist Tab Worksheet counting from the left.
    Dim fm1 As UserForm1 '
    Set fm1 = New UserForm1  '                                                                        ' A copy of the Blue Print is made
    fm1.Left = 0
    fm1.CheckBox1.Value = True
    fm1.Show vbModeless         '         there are two ways ( modes ) of showing a form in vb - modal and modeless. a modal form takes the focus and won't return control to the calling module until it is closed. a modeless form is shown and then control is immediately returned to the calling module.     http://vbcity.com/forums/t/53342.aspx
    Dim fm2 As UserForm1
    Set fm2 = New UserForm1
    fm2.Show vbModeless
    fm2.Caption = "Klone fm2"
    fm2.Left = 50
        '    Dim UserForm1 As UserForm1     ' These two imaginary lines are effectively done by
        '    Set UserForm1 = New UserForm1  ' VBA on compile when it sees the next line. Thes lines you would never see
    UserForm1.Caption = "This one VBA decalred and Instanced for me"
    UserForm1.Show vbModeless
    UserForm1.Height = 50
    UserForm1.Left = 200
    End Sub
    Last edited by DocAElstein; 06-01-2024 at 08:00 PM.

  8. #18
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,458
    Rep Power
    10
    later
    Last edited by DocAElstein; 06-01-2024 at 08:00 PM.
    A Folk, A Forum, A Fuhrer ….

  9. #19
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,458
    Rep Power
    10
    This is post https://www.excelfox.com/forum/showt...age2#post24185
    https://www.excelfox.com/forum/showthread.php/2965-Class-related-Stuff-Userforms/page2#post24185
    https://www.excelfox.com/forum/showt...ll=1#post24185
    https://www.excelfox.com/forum/showthread.php/2965-Class-related-Stuff-Userforms?p=24185&viewfull=1#post24185




    An attempt at an initial summary on UserForm (module)
    We are really talking about Class things and class ideas. Inevitably to get some understanding you must accept/ appreciate / be aware of, the hierarchical Object Orientated Programming concept that VBA is modelled on: Very simplified: we start, as it were towards the top, or top left, with classes and from those objects are made and they finally have Properties. The final objects with their properties ( and methods etc.), could be crudely be regarded as some "actual" or significant "things", whereas the class is just the details of how to make the objects, or a template to be used over and over again and added to or filled in appropriately to suit the particular instance like a classic simple example,
    _ The concept of a car could be regarded as a Class,
    _ a particular model as an instance of it,
    _ and a color would be a property.
    ( _ A method might

    The VB Editor
    The VB editor is a tool we use , often referred to as the development environment, that , amongst other things, by default ,allows us to conveniently look at and develop/ change our objects. It is almost impossible not to use objects from a class if you are using Excel. By default we have limited direct access to any classes other than to make objects from them, assign variables to them, or modify the properties of the objects made from them.
    But there are two sorts of Class that we can create, and we can use the VB Editor to do that as well

    Use Keys Alt+F11 from Excel to get the VB Editor
    , or alternatively
    , right click on any worksheet tab and select view code, - https://i.postimg.cc/J04LpnsW/Right-...eet-coding.jpg
    Right Click a tab to show worksheet coding.JPG
    ,That will show you initially a representation of the Excel objects associated with your file, which usually by default will be for the workbook itself, ThisWorkbook, and the worksheets you have. (If you took this second approach to get the VB Editor then initially the big window will be for any coding you may add for the worksheet who’s tab you selected.)
    https://i.postimg.cc/nh69N8KW/Big-wi...u-selected.jpg
    Big window for any coding you might want to add associated with the worksheet who s tab you sele.JPG
    We are for now not interested in the existing things. They are associated with existing Excel objects.
    It is important to realise that most object things in Excel follow the object orientated programming ideas, which for us in the current discussions means they were made from referring to some sort of blueprint / template / set of written instructions etc. We refer to these blueprint / template / set of written instructions etc. as a Class.
    For most of the existing Excel objects, we have only limited, if any, direct access to the Class. That is because it will be propriety information allowing you to construct Excel, so trade secrets as it were.


    We are interested in the available adding of a New Class UserForm. Associated with that UserForm Class thing is one of two class related module types that Microsoft makes available to us.
    Get one with a bit of right clicking near the representation of your Excel file in the left hand VBA project window of the VB Editor,
    https://i.postimg.cc/GmZzK54L/Do-a-b...-Form-opti.jpg , https://i.postimg.cc/kgVfxmXY/Do-a-b...orm-option.jpg
    Do a bit of right clicking near your file in the left VBA projct explorer, find the UserForm opt.JPG , Do a bit of right clicking near your file in the left VBA projct explorer and find the UserForm .JPG

    You should then see the basic tools of
    _ a simple pin board looking box or "form", usually with the default name of UserForm1 ,( both for the main class thing and the caption name on the pin board thing top left , ( more to that later ** ) )
    , and
    _ a Toolbox dialog should also be visible. (If it’s not visible select View->Toolbox from the VB editor top ribbon menu. We use the toolbox to add controls to our UserForm.)
    The bottom left window should also now show the properties related to this UserForm
    https://i.postimg.cc/W3Fgbm8z/User-F...-VB-Editor.jpg , https://i.postimg.cc/852WFFXH/User-F...-VB-Editor.jpg


    I will change the Name property to suit the previous discussions, to ufResults , ** , and note that the Caption did not change,
    https://i.postimg.cc/FzN5szZd/Captio...User-Form1.jpg

    , and we will leave it like that for now, as this will help along the way, to demonstrate a quirk with UserForm Class things, which was brought out by our discussions … Unlike normal classes, UserForms are auto-instantiating, so you can simply call them by name and a new instance of the class is created. ….. as you use ufResults for the first time, a new instance of the form is created.
    It's confusing because you effectively get a variable of the same name as the class.

    This last statement we will come back to after getting the basics behind us now.

    Important summary of what we have so far.
    So we have a class now. Parts of this class is the added UserForm module. The name ufResults is effectively the name of the greater thing that is the Class. Exactly what that greater thing is, is deep in the innards of Excel and Office, it concerns how the thing eventually works , and is anyone’s guess in the meantime what / where it is. We are only interested in the available interface that we see, which we can use to determine what sort of object can be made from it. We are only concerned with tools and interface given to us to help shape how the blueprint / template / set of written instructions etc finally "looks". But note what we see and finally have is not much more significant than an instruction manual. Itself it will not "do anything". We use it to make the ( or as many as we like ) final object(s) that "does" something.
    Generally a class is designed to be used over and over again, just like a template.

    What is a class- what we have so far
    As we said previously , it’s a blueprint / template / set of written instructions
    In simple layman beginner terms its like a piece of paper in your pocket with some notes and instructions on how to build an object of this type / class. Apart from the information on it, as a thing its pretty insignificant in itself.
    The whole point of the class module is just to make it more convenient to have the text / instructions / explaining diagrams etc. conveniently for you to get at in Excel and use automatically / programmatically, and thereby automatically build an object from, rather than having to painstakingly read from the bit of paper and manually construct the thing and manually add coding to it etc.
    A main difference between A UserForm class module, and the other standard Class module, is that we have that main extra box with grid points on it, and other bits and pieces in the tool box, like empty lists and boxes, to make it a bit easier to fill in. In simplest layman terns, the difference between,
    _ For a normal class module being like a blank piece of paper . Mainly you would put the text of coding you were interested in for you final object, on it. (Part of that typical coding text is there for you to select and add to, to save you a bit of time typing)
    , and
    _ The UserForm is similar, but like a bit of Graph paper along with some other pre prepared bits and pieces to stick on it. Associate with most of those things are then also available part of the text of coding that might be typically associated with those things, there for you to select and add to, to save you a bit of time typing
    Last edited by DocAElstein; 06-03-2024 at 12:27 PM.

  10. #20
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,458
    Rep Power
    10



    Make UserForm(s) object of the type / class ufResults


    Very important to remember at this point is that we have no "thing" or object, just some notes / sketches of how we would like an object to be. We need to talk about making an object from those notes / sketches, what we refer to as something like Instantiating, instanciating, Initialising, making an object fro a class.
    We won’t go here into the exact details of all the different ways to do this, but they are similar to the ideas of referring to some external libraries, as discussed here for example https://www.excelfox.com/forum/showt...ll=1#post10568
    https://www.excelfox.com/forum/showt...ll=1#post10567

    Strictly speaking an external Library is a package that can include amongst other things Classes to provide functionality of some sort of another. But in end effect we want the same thing: An object variable which can then be used in such a form
    ___= ThatLibraryObject.StufffromItIWant

    Our class module so far is nothing more than a blank pop up ( a UserForm is basically a pop up , sometimes called a Form )
    There is not a lot we can do with an object made from it, and an object made form it similarly can’t do much.
    One thing we can do is get it to pop up. Another thing we can do is programmatically change the caption on a made object.
    So this next simple coding :
    _ makes an object from the class, ufResults, and the variable objufResults will be pointing to it
    _ Shows that User form
    _ Changes the caption to New Caption
    As this is "real" coding, it can go in any existing object code module, or a normal code module. https://i.postimg.cc/nrHyDfsT/Real-c...mal-module.jpg
    (It shouldn’t go in anything to do with the class module ufResults , or any other class module, as you can not run coding from a bit of paper….. )
    Code:
    Option Explicit
    Sub ufTest() '  https://www.excelfox.com/forum/showthread.php/2965-Class-related-Stuff-Userforms?p=24186&viewfull=1#post24186
    Rem 1 making an instance of ufResults
    Dim objufResults As ufResults
     Set objufResults = New ufResults
     
    Rem 2 The object exist, and could be considerd perhaps to have a similar status to a worksheet, although we do not have a corresponding  code  module for it, but we can imagine one looking like the  ufResults  class module
     objufResults.Show vbModeless              '  vbModeless means I means I can do anything outside the UserForm whilst its open, and also that the code moves on. (If I used vbModeless, then I would effectively "hang" at this code line until I closed the UserForm )
                       Application.Wait Time:=Now + TimeValue("00:00:03")
    Rem 3 I am changing the caption on the current
     Let objufResults.Caption = "New Caption"  '  This changes the name in the current  objufResults
                       Application.Wait Time:=Now + TimeValue("00:00:03")
    
    Rem 4 Close the userform instance, (checking the number of loaded userforms before and after)
     Debug.Print UserForms.Count ' 1
     Unload Object:=objufResults ' close the user form instance  objufResults
     Debug.Print UserForms.Count ' 0
    End Sub
    Important to note is that after running the coding, the caption seen in the class module, ufResults is as initially. We should expect this: That caption name, UserForm1 , is what is given every time we make an instance of ufResults.
    https://i.postimg.cc/gJPFGZmC/User-F...ass-module.jpg
    UserForm1 remains the caption name in the Form diagram in the class module.jpg
    _._________________________

    Here is the quirk ###
    This is almost the same coding as the last, coding, and on running it, exactly the same thing happens
    Code:
    Sub ufTest2()
    Rem 1 making an instance of ufResults
    'Dim ufResults As ufResults
    ' Set ufResults = New ufResults
     
    Rem 2 The object does not exist ?????
     ufResults.Show vbModeless
                       Application.Wait Time:=Now + TimeValue("00:00:03")
    Rem 3 I am changing the caption on the current
     Let ufResults.Caption = "New Caption"
                       Application.Wait Time:=Now + TimeValue("00:00:03")
    
    Rem 4 Close the userform instance, (checking the number of loaded userforms before and after)
     Debug.Print UserForms.Count
     Unload Object:=ufResults
     Debug.Print UserForms.Count
    End Sub
    
    I have made just two main coding changes.
    _ I have changed the name of the variable which was pointing to the object ,in all the corresponding places in the coding, from objufResults to ufResults, (which remains our class name as well)
    , and
    _ I have removed the coding that instantiates an object from the class, ( I have ' commented out Rem 1)

    By the way if I put that (Rem 1 coding back in, then once again on running it, exactly the same thing happens . This latter statement is perhaps understandable, (although it is generally regarded to be unwise to use the same name for different things and can cause unexpected problems )
    What is less understandable is how the coding should work fine when I appear to not instantiate the class, that is to say make an instance of the class. In most coding of this nature we would receive an error saying something like the object variable, (in this second coding ufResults, does not exist when we first try to use it , ( in this coding at code line ufResults.Show vbModeless )

    So what is going on???
    As Rory said…. Unlike normal classes, UserForms are auto-instantiating, so you can simply call them by (class) name and a new instance of the class is created. ….. as you use ufResults for the first time, a new instance of the form is created.
    It's confusing because you effectively get a variable (pointing to the object instantiated from the class) of the same name as the class. ###


    Whilst this may be considered convenient, it could easily be one reason why many people do not understand the subtle point about class, and may miss altogether that they are dealing with a class for the case of a UserForm, in particular as many people are only interested in a single instance of any particular UserForm type, and on top of this, they may not be interested particularly in changing the class name, as we did.
    So they insert a UserForm module, for example a single first one. It gets the default name UserForm1. Then they go off happily in coding towards the start of the coding doing things like, pseudo coding,
    UserForm1.DoSomething
    They may well believe they are dealing with an object variable, UserForm1, from the start, and indeed think they have been exclusively dealing with an object, or object related things, fir example whilst working with the UserForm module and it’s various tools . But that is not the case. They where dealing with class things, and then the first time they used the word UserForm1 they effectively had this coding going on
    Dim UserForm1 As UserForm1
    Set UserForm1 = New UserForm1
    UserForm1.DoSomething

    , where UserForm1 is the class, and UserForm1 is the object
    Code:
    Option Explicit
    Sub ufTest3()  '  https://www.excelfox.com/forum/showthread.php/2965-Class-related-Stuff-Userforms?p=24186&viewfull=1#post24186
    Rem 1 making an instance of ufResults  - we can do this, or it will be done automatically by first attempt to use  UserForm1  where an object is expected
    'Dim UserForm1 As UserForm1
    ' Set UserForm1 = New UserForm1
     
    Rem 2 The object does not exist , but will when the code line is done
     UserForm1.Show vbModeless
                       Application.Wait Time:=Now + TimeValue("00:00:03")
    Rem 3 I am changing the caption on the current
     Let UserForm1.Caption = "New Caption"
                       Application.Wait Time:=Now + TimeValue("00:00:03")
    
    Rem 4 Close the userform instance, (checking the number of loaded userforms before and after)
     Debug.Print UserForms.Count ' 1
     Unload Object:=UserForm1
     Debug.Print UserForms.Count ' 0
    End Sub
    https://i.postimg.cc/wjNGmyb0/class-...User-Form1.jpg
    class UserForm1 and object variable UserForm1.jpg

    _._______


    In the next post we will do just a very simple example of a userform with some coding "in it"
    Attached Files Attached Files
    Last edited by DocAElstein; 06-08-2024 at 01:13 AM.

Similar Threads

  1. Class Stuff: VBA Custom Classes & Objects, Class Modules
    By DocAElstein in forum Excel and VBA Tips and Tricks
    Replies: 29
    Last Post: 06-02-2024, 01:49 PM
  2. Replies: 42
    Last Post: 05-29-2023, 01:19 PM
  3. Test my rights , to do stuff
    By TestAccount in forum Test Area
    Replies: 0
    Last Post: 10-07-2020, 11:49 AM
  4. Backup all modules, class modules and userforms to a selectable folder
    By MrBlackd in forum Excel and VBA Tips and Tricks
    Replies: 1
    Last Post: 04-06-2014, 08:33 AM
  5. Pass Values Between Multiple Userforms
    By Excel Fox in forum Excel and VBA Tips and Tricks
    Replies: 0
    Last Post: 07-24-2011, 03:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •