Results 1 to 28 of 28

Thread: Why are the variables not downloaded?

Threaded View

  1. #11
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,915

    Re: Why are the variables not downloaded?

    I'm going to weigh in on this one as well. LeandroA, we must think of a form as having two pieces: 1) the user interface (UI) piece, and 2) the code/COM piece.

    The code/COM piece is exactly like a class (CLS) module. It gets instantiated, you can uninstantiate it, and you can use its methods while it's instantiated.

    Now, the UI piece is what actually gets loaded/unloaded. However, as a rule, the code/COM piece virtually always gets instantiated when the UI piece gets loaded. However, the opposite is not at all true. When you unload the UI piece (with something like Unload Form1), the code/COM piece typically stays instantiated.

    Now, there's another "trick" going on here as well. These (FRM) modules have a hidden property named VB_PredeclaredId always set to TRUE. For typical class (CLS) modules, this VB_PredeclaredId property is typically FALSE, but we can set it to TRUE if we like.

    When VB_PredeclaredId=TRUE, what does that mean? It means we get an implicit object variable for the form that's the same as the name of the form. That's why things like Load Form1, or Form1.Show, or Unload Form1 work. This implicit object variable is quite similar to an object variable declared with the New keyword (eg, Dim MyObj As New MyClass). Anytime you "touch" these VB_PredeclaredId=TRUE classes (including FRM classes), if they're not instantiated, they'll auto-instantiate (just like the object variables declared with the New keyword).

    So, a FRM module has two pieces, the UI and the code/COM piece. So, if you'd like to continue using that implicit object variable (that's always the same as the form's name), when you unload the form, you must also uninstantiate the the code/COM piece. To do that, here's a statement I often place in my form's unload event:

    Code:
    
    Private Sub Form_Unload(Cancel As Integer)
        Set Form1 = Nothing     ' This clears the Form1 object variable, which will probably uninstantiate this form once this event finishes.
    End Sub
    
    
    Now, note that you must use the implicit object variable's (i.e., form's) name. You can not say:

    Code:
    
    Private Sub Form_Unload(Cancel As Integer)
        Set Me = Nothing     ' Bad!!!!
    End Sub
    
    To appreciate this, we must remember how COM objects work. They auto-uninstantiate when all references to them are gone (i.e., all object variables are set to Nothing). (To be technically correct, their internal reference count has to go to zero.) And the Me keyword is a separate reference from the Form1 object variable. We can set Me to Nothing, but Form1 will still be referencing the object.

    (To avoid this confusion, many recommend not using these implicit object variable names. But personally, I have no problems with them. We must simply understand how they work.)

    And lastly, to directly address your question, all module level variables you declare in a form's code will belong to the code's/COM's instantiation, and have very little to do with the UI portion of the form. And that's why you're seeing what you see.

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

    EDIT1: Also, just as a further FYI, events like Form_Load, Form_Unload, and Form_Activate really have more to do with the UI. However they are code, and therefore, they're instantiated with the code/COM part of a form. Now, in contrast, events like Form_Initialize and Form_Terminate are only concerned with the code/COM portion and have nothing to do with the UI portion of a form.

    EDIT2: Also, if you edit a FRM file with something like Notepad, you can clearly see the two section (UI and code/COM). Also, you can see that VB_PredeclaredId property.
    Last edited by Elroy; Dec 2nd, 2019 at 03:44 PM.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

Posting Permissions

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



Click Here to Expand Forum to Full Width