?
Solved

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

Posted on 2006-12-02
5
Medium Priority
?
171 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
[X]
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
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 300 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 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.

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

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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month8 days, 4 hours left to enroll

765 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