?
Solved

Loading a Form without a form load.

Posted on 2003-03-25
17
Medium Priority
?
432 Views
Last Modified: 2010-05-18
Dear All,

We currently have the following code

Dim frmTest as Form
Set frmTest = TestForm

Where TestForm is a form in our system.

However we wish to soft code it so...

Set frmTest = Forms.Add("TestForm")

This however produces a problem. In the first scenario form Initialize is called, in the second, form Iinitialise AND form Load is called.

My question then is this. Is there a way of soft coding the form but only allow the Form Initilize to be called ?

Peter

0
Comment
Question by:PeterNolan
[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
  • 4
  • 2
  • 2
  • +7
17 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 8202058
Hi PeterNolan,

I don't think that there is, partly because there is no reason to.

If you simply declare the object as Form like your first example. There is no reason to instantiate the object itself until you need to do something with it. Any reference to a property, method or contained object on the form will automatically cause it to be implicitly loaded so there is little actually to be gained using either method.

Essentially, declare the form object as you do and then use whichever methodology is appropriate when you need to use it. I suppose what I am trying to say is that there is no advantage in practice to not running the form_load event in this way as the first time you set a property of the form however it is instantiated it will be implicitly loaded.

Tim Cottee MCSD, MCDBA, CPIM
http://www.timcottee.tk 

Brainbench MVP for Visual Basic
http://www.brainbench.com

Experts-Exchange Advisory Board Member
0
 
LVL 1

Expert Comment

by:hazgod
ID: 8202089
you can do it on form_activate
0
 

Author Comment

by:PeterNolan
ID: 8202117
Hi Tim,

Thanks for you reply.

There is a point to this. We have an application with 250+ forms. Currently all these forms are working, but have been hard coded in our MDI Parent Form / individual forms and the architecture behind is is being reviewed.

To add another layer of complexity we are now wanting "Dynamic" navigation, which would mean the the MDI Parent is becoming almost unmaintainable, i.e. on this data the following forms appear....

Therefore I am searching for a way of soft coding the forms, and placing the forms to be displayed in the database.

However with out current design we have code in our Form Load, but the Form Load in our system is only activated AFTER the form has the data, and there lies my problem.

Peter


0
Independent Software Vendors: 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!

 
LVL 43

Expert Comment

by:TimCottee
ID: 8202214
Peter, I still perhaps don't entirely understand the problem. My sticking point is that you cannot supply anything to a form until form_load has been processed. If for instance you do something as simple as set the caption of a form that has been declared but not referenced then you implicitly call form_load at the same time. This is normal behaviour and as far as I am aware cannot be circumvented.

Do you load this data to the form in the initialise event?

Is there any way that you can explain in a little more detail how your application hangs together? Perhaps I can see an alternative method that will suit.

If necessary you can also drop me an email to the address in my profile.

Tim.

0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 8202418
Can you create your own collection to hold all forms instead of using


Set frmTest = Forms.Add("TestForm")
0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 8202419
Can you create your own collection to hold all forms instead of using


Set frmTest = Forms.Add("TestForm")
0
 

Author Comment

by:PeterNolan
ID: 8202642
Hi Tim,

Thanks for you reply. In answer to your query, instanciating a form as follows

Dim frmTest as Form
Set frmTest = TestForm

does not generate a "Form_Load" event, you can however access the form and pass it information.

i.e

frmTest.PassData DataToBePassed

where PassData is a public sub on our child form.

I  our case we are passing it an object that has been created / instanciated from the MDI Parent, and passing that object to the form. If you require further info / proof of this see of you can grab a copy of the book "Visual Basic 6 Business Objects" by Wrox press.

Edd, in your case we still generate the form_load event.

Thanks

Peter
0
 
LVL 1

Expert Comment

by:BurtonRacing
ID: 8202966
FYI,
  If you use the form templates that you create properly then the object will react appropriately.  The forms you create during design time should never be directly referenced in a true OO world.  VB will allow you to use the template but the events of the object and the order they are triggered may not be what you expect.  I tend to make my forms encapsulated.  For example, I have a form that I use to allow the user to create a new or update an existing member.  I added a property to the forms template that accepts a memberID.  This is then used in the form_load event.  Here is my code:

Dim lobjForm as frmMember

set lobjForm = new frmMember(causes form_initialize to occur)
lobjForm.MemberID = 1
lobjForm.Show(causes form_load to occur)

So if you truly want to instanciate forms without having to load them use a variable of the forms type and instaciate it with the NEW statement.

FYI one problem people have trying to code using objects is the fact that they tend to couple forms into each other.  Bascially have form1 reference inside of its code to form2.  Bad practice!  If you define an interface to the object/form then this is not needed.  So if a form needs values to operate it should define properties for those values, then anywhere in the project can instanciate the form and pass the values.  This decouples your application and provides better maintenance in the future.  I can go on and on about how you code in VB the OO way and not the way most do!
0
 
LVL 1

Expert Comment

by:BurtonRacing
ID: 8202997
Remember that everything is a an object.  Now TimCoffee is correct that if you reference a control on the form then it will call the form_load because that control has not been loaded into memory.  The property methods that you can create do not react the same way.
0
 

Author Comment

by:PeterNolan
ID: 8203098
Thanks Burton,

That is actually what we do, and it seems I missed the "New" keyword from the example and forgot to say it was a property of the form that recieved the information, so sorry about the confusion.

Peter
0
 
LVL 5

Expert Comment

by:JMoon5FTM
ID: 8203105
A form created by the new operator is initialized, but is not loaded until a control is accessed, the form is explicitly loaded with the load statement, or the form is shown/hidden.

That is:

Set frmTest = new TestForm
'Form is initialized, but not loaded
Load frmTest
'Form is loaded

When you use forms this way, your form designers (like TestForm) are classes rather than objects.

Some VB versions/service packs have issues with a form used both as a class and as an object (it doesn't register with the Forms collection the way it should).  So, for any given form, you shouldn't mix the two approaches.  That is, if you ever create a new MyFormType, you shouldn't ever call MyFormType directly, but stick to using new.

Hope this helps.
0
 

Author Comment

by:PeterNolan
ID: 8203203
Dear All

Thank you for your responses but I believe we are getting away from the problem.

My question is how can we call a form using a string holding the form name, and not automatically generation a Form_Load.

Thanks
Peter
0
 
LVL 1

Expert Comment

by:hacibumbala
ID: 8203524
Subject is really complicated and don't know if this appropriate, but may help:

In Form2 declare a public variable like:

Public FormName As String

Private Sub Form_Load()
    MsgBox "I am loading..."
End Sub

And in Form1:

Private Sub Form_Load()
    Form2.FormName = "Form2"
    MsgBox Form2.FormName
End Sub

This procedure does not fire Form_Load event.
0
 
LVL 5

Expert Comment

by:JMoon5FTM
ID: 8218124
From http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon98/html/vbconcollectionsinvisualbasic.asp:

the Forms collection contains all of the currently loaded Visual Basic forms in the program.

Since initialized but unloaded forms don't appear in the forms collection, what you want can't be done in the Forms collection.

Thus, to create a form based on a string variable, you'll have to either:

a) Use a Select Case block.
b) Write a wrapper procedure for creating each type of form, and then use CallByName.

Neither is pretty, but it's all you've got.  An example of method b:

Function CreateForm1 as Form
   Set CreateForm1 = new Form1
End Function

Function CreateForm2 as Form
   Set CreateForm2 = new Form2
End Function

Sub CreateFormWithName(strName as String)
   Dim newForm as Form
   Set newForm = CallByName(Me, "Create" & strName)
   'Do something with newForm
End Sub
0
 

Expert Comment

by:CleanupPing
ID: 8899906
PeterNolan:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Experts: Post your closing recommendations!  Who deserves points here?
0
 
LVL 6

Expert Comment

by:GPrentice00
ID: 9441099
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

 -->PAQ - no points refunded

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER

GPrentice00
Cleanup Volunteer
0
 

Accepted Solution

by:
YensidMod earned 0 total points
ID: 9495874
This question is PAQed and no points refunded (of 125)

YensidMod
Expert Exchange Moderator
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

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 …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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 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…
Suggested Courses
Course of the Month13 days, 11 hours left to enroll

800 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