Solved

Form_Load

Posted on 2002-05-08
12
204 Views
Last Modified: 2010-05-02
We have a VB application that runs all the time in the background (effectively as a service). However, it does contain a form that is only used in another mode (the program can run in a manual mode). It therefore contains a Form_Load function. Under what circumstances can Form_Load be called? I know, for example, that Form_Unload gets called if a user logs off from Windows.

Does anyone know all events that might cause Form_Load to be called?

0
Comment
Question by:simonslr
  • 2
  • 2
  • 2
  • +5
12 Comments
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 6995937
Form_Load event only fired once when a form is loading. What i would suggest is make the codes in Form_Load event as a Sub then call it like this:

Private Sub Form_Load()
    Call mySub
End Sub

Private Sub mySub()
//Do the stuff here
End Sub

'You can put the Sub in a Module by change 'Private' to 'Public so that it can be called by another forms
0
 

Expert Comment

by:HeN_da_MaN
ID: 6995947
You could use Form_Activate, I don't know what U want to do.
Form_Load gets called whenever you call a function in the codemodule and your form is not active.
You could also call Form.Show to call Form_Load (this works only one time until you Unload the form again)
Maybe I am able to help you if you give more info.

Hen
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 6995948
To be more clear on my comment, create a Sub so that we can remember the specific name of the function to call (rather than Form_Load). Of course you still can Call Form_Load within your form, like this:

Private Sub Command1_Click()
    Form_Load
End Sub

Private Sub Form_Load()
    MsgBox "Hello World"
End Sub
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 6995955
Well there are basically three ways to call the form_load procedure code:

1) Load the form; Load frmMyForm
2) Call it explicitly (requires that the form is already loaded and is being called from a procedure within the form's scope); Call Form_Load()
3) Reference a public method or object in the form; this is a tricky one as if you have not yet explicitly loaded a form, any direct reference to an object or method of the form will cause it to be implicitly loaded. For example if you have a textbox on the form called text1 then any reference in any other part of the project such as frmMyForm.Text1.Text = "Hello" will actually cause the form to be loaded (if it isn't already) and the Form_Load event code will be executed.

From what you say (and what you don't) my assumption is that because you have seen the form_unload code running when the application shuts down as a result of windows shutting down then you wonder how the form was ever opened. My suggestion is that this is caused by 3) above and that somewhere in your code you are referencing an object or method in that form which is causing it to be implicitly loaded but never shown.
0
 
LVL 3

Expert Comment

by:Bahnass
ID: 6995964
Bravo Super Tim !!!!
0
 
LVL 6

Expert Comment

by:pierrecampe
ID: 6996005
Hi Tim just a remark
*********************
Reference a public method or object in the form; this is a tricky one as if you have not yet explicitly
loaded a form, any direct reference to an object or method of the form will cause it to be implicitly loaded
*********************
calling a public method in a form that is not loaded will NOT load the form
but the form object will be initialized
and thus its initialize event will be called
and on ending the program its terminate event will be called
but if you reference any of the VISIBLE propertys of the form it will be loaded
say you set its caption or a control in it, then it will be loaded
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Expert Comment

by:ngomte
ID: 6996051
Form Load is called whenever a refereence is made to a object to this form, other then the property procedure.
0
 
LVL 15

Expert Comment

by:ameba
ID: 6996175
pierrecampe> if you reference any of the VISIBLE propertys of the form it will be loaded
or maybe we can say "if you reference any of the *built-in* properties"

     MsgBox Form2.Tag    ' this will load Form2
     
     MsgBox Form2.uhoh   ' this will not load Form2
0
 

Author Comment

by:simonslr
ID: 6996182
I don't think I've made my question very clear. I know that the Form_Load function is being called at start-up - we hide the form when running in non-manual mode ...

... but are there any circumstances that might cause Form_Load to be called again (for example in the same way that form_unload is called when a user logs off - in our case the program carries on running even when a user logs off).
0
 

Author Comment

by:simonslr
ID: 6996183
I don't think I've made my question very clear. I know that the Form_Load function is being called at start-up - we hide the form when running in non-manual mode ...

... but are there any circumstances that might cause Form_Load to be called again (for example in the same way that form_unload is called when a user logs off - in our case the program carries on running even when a user logs off).
0
 
LVL 43

Accepted Solution

by:
TimCottee earned 50 total points
ID: 6996245
It should never be called again if it hasn't been unloaded. Unless called explicitly for some reason.
0
 
LVL 15

Expert Comment

by:ameba
ID: 6996261
>are there any circumstances that might cause Form_Load
Without calling it in your code or referencing something on Form, no.
Are you sure there is an extra Form_Load executed?

>program carries on running even when a user logs off

Program can run without any form loaded.
Program can run without reexecuting Form_Load - e.g. form didn't unload for some reason:
- timer was active, some form code is stuck in a loop, circular references, noone called Unload in code, or someone called Unload, but that was cancelled in QueryUnload or Unload event....
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now