Solved

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

Posted on 2006-12-02
5
166 Views
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

     myMDIChild1.someSub
     myMDIChild1.visible=True
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?


Thanks
0
Comment
Question by:Unimatrix_001
  • 3
5 Comments
 
LVL 13

Expert Comment

by:Mark_FreeSoftware
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.
0
 
LVL 13

Assisted Solution

by:Mark_FreeSoftware
Mark_FreeSoftware earned 75 total points
ID: 18064863
for the code completion:

Dim myMDIChild1 As mdiChild
instead of the object way
0
 
LVL 13

Accepted Solution

by:
jmundsack earned 425 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.

HTH-Jon
0
 
LVL 13

Expert Comment

by:Mark_FreeSoftware
ID: 18069540

i stand corrected,

Unimatrix_001 : sorry for providing wrong information,

HTH-Jon : thanks for correcting me!
0
 
LVL 3

Author Comment

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

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

831 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