[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


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

Posted on 2006-12-02
Medium Priority
Last Modified: 2010-04-30
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?

Question by:Unimatrix_001
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
LVL 13

Expert Comment

ID: 18064860

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.
LVL 13

Assisted Solution

Mark_FreeSoftware earned 300 total points
ID: 18064863
for the code completion:

Dim myMDIChild1 As mdiChild
instead of the object way
LVL 13

Accepted Solution

jmundsack earned 1700 total points
ID: 18068074
"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.

LVL 13

Expert Comment

ID: 18069540

i stand corrected,

Unimatrix_001 : sorry for providing wrong information,

HTH-Jon : thanks for correcting me!

Author Comment

ID: 18072025
Mark: No problem. Mistakes happen to even the best of us. ;) Cheers for the info Jon! Didn't realise that... :S

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question