Improve company productivity with a Business Account.Sign Up

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 176
  • Last Modified:

Using object or form for MDI variable & variable without triggering form load.

Hi there,

I've got a boot form, a main form (MDI) and a few smaller child MDI forms. The boot form loads up and gets some information off disk. This information is then put into a variable on the main form, however the main form uses that information in the load event. Although it seems a bit of a chicken and the egg, unable to set the information in the main form without triggering the main form load event, which in turn uses the information that was meant to be set before the load event fired.

Also, I'm trying to treat the child MDI forms as objects, as such:

Dim myMDIChild1 As Object
Dim myMDIChild2 As Object

Private Sub MDIForm_Load()
     Set myMDIChild1 = new mdiChild
     Set myMDIChild2 = new mdiChild

End Sub

A small problem, but I'm guessing because I declared them as objects, I don't get my nice code completion come up when attempting to access a method or variable of myMDIChild1. Any code workarounds?

  • 3
2 Solutions

it's not possible to use a function, variable or member from a form without loading the form.
if you want to use the variable without loading the form, add it to a module instead.
for the code completion:

Dim myMDIChild1 As mdiChild
instead of the object way
"it's not possible to use a function, variable or member from a form without loading the form."

This has not been my observation.  Keeping in mind that a form is a class (albeit one that implicitly implements a certain interface for the UI functionality), you can in fact access properties and methods of a form without loading it.

The one thing to keep in mind is that the instant you access one of the form's UI properties, or any of its constituent controls, VB will automatically load the form.  But if you don't access any of these things, VB will not load the form automatically.  Consider this example:

1. create a new project
2. remove the default form
3. add a new mdi form
4. add this code to the mdi form

Option Explicit

Private mSomething As String

Public Property Get Something() As String
    Something = mSomething
End Property
Public Property Let Something(ByVal NewValue As String)
    mSomething = NewValue
End Property

Private Sub MDIForm_Load()
    Debug.Print "MDIForm_Load was called"
End Sub

5. add a module
6. add this code to the module

Option Explicit

Public Sub Main()
    MDIForm1.Something = "Hello, World!"    'load doesn't happen
    MsgBox MDIForm1.Something               'Load doesn't happen
    Debug.Print MDIForm1.Caption            'this will invoke the Load
    Unload MDIForm1
End Sub

7. make sure the project startup object is Sub Main
8. run the project

You will notice that as of the time the message box is displayed, the form has not yet been loaded, even though we've accessed a property of the form (i.e., Something).  This is because the "Something" property of the form object does not reference any of the UI elements of the form, thus VB is smart enough not to implicitly load it.  As soon as we access the "Caption" property of the form, however, VB loads the form.

Simplistically, you can think of a form as having two elements: an object element, and a UI element.  The object element is created and destroyed using Set (e.g., Set someform = New Form1, Set someform = Nothing), and the UI element is created and destroyed using Load and Unload.  You can create and use the object element of a form without ever loading it, as long as you don't trigger the automatic load by accessing any part of the UI element.


i stand corrected,

Unimatrix_001 : sorry for providing wrong information,

HTH-Jon : thanks for correcting me!
Unimatrix_001Author Commented:
Mark: No problem. Mistakes happen to even the best of us. ;) Cheers for the info Jon! Didn't realise that... :S
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now