Solved

Unload a form and Load again

Posted on 2014-02-05
18
532 Views
Last Modified: 2014-02-27
I am using VB6.   When my forms come up, the Load event is triggered, which loads all the values into the form.  I would like to Unload the form so that I can Load it again and load the values again.

I assumed that
Unload Form
would work, but all it does is stop the entire executable file.

Any help please.  I would simply like to rerun the Load event OR Unload the form and load it again.
0
Comment
Question by:al4629740
  • 10
  • 8
18 Comments
 
LVL 45

Expert Comment

by:Martin Liss
ID: 39837795
Please attach your project.
0
 

Author Comment

by:al4629740
ID: 39837812
The project is simply too big and contains sensitive material.  

what do you need to see?
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 39837866
OK, you imply that it's a problem that closing form doesn't close the exe. Do you want to close the exe? If do then do something like this.

Dim frm As Form

For Each frm In Forms
    Unload frm
Next

If you don"t want to close the exe but just "reload" the form then create a routine in a module that initializes all the form's variables clears controls, etc and instead of unloading the form, hide it and when you want to see it again run the code in the module and Show the form.
0
 

Author Comment

by:al4629740
ID: 39837872
How do I run the Private Sub Form_Load() of Form1 from Form2?

Also, can you provide an example of clearing all the controls in a form?
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 39838027
Here's a demo project.
Q-28357714.zip
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 39839293
Thinking about what I posted yesterday I realize that except for the "Initialize" routine which provides the answer to your "can you provide an example of clearing all the controls in a form" question, that the example is pretty silly since you can do the same thing by just unloading and reloading Form2. So...

if you want a better answer please describe to me more fully what the situation is and exactly what you want to have happen.
0
 

Author Comment

by:al4629740
ID: 39874484
I would like to unload the Form2 with and then load it again to execute the Load event
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 39874592
Please help clarify what it is that you are trying to do. In your original post you say the following (the bolding is mine)
I assumed that
Unload Form
would work, but all it does is stop the entire executable file.
Is the form that you want to unload the only form that's loaded? If so then it is normal for the app to close when the form is Unload.

It would help if you posted the code from the Load event but usually you can Cut the code out of the Load event and place the code in a Sub in a module. After that, Hide the form instead of unloading it and Show it when you want to see it, executing the sub just before you do that.
0
 

Author Comment

by:al4629740
ID: 39884534
Nope.  That's the thing I don't get.  There are other forms that are hidden.  Does another form need to be visible?
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 45

Expert Comment

by:Martin Liss
ID: 39884630
Please describe in detail what it is that you want to do.
0
 

Author Comment

by:al4629740
ID: 39892352
I have Form1, Form2 and Form3 open.  When I run the command Unload Me in Form3, it closes the whole project.  I only want to unload Form3 and then leave the other two forms open.
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 39892381
I have Form1, Form2 and Form3 open.  When I run the command Unload Me in Form3, it closes the whole project.  I only want to unload Form3 and then leave the other two forms open.
It shouldn't do that.

Can you show me the code for the Sub in Form3 that contains the "Unload Me" line?

Are you sure that the whole project is closed? I ask because if both Form1 and Form2 are hidden when when you close Form3, it may only look like the whole project is closed. If they are hidden then try something like

Form1.Show
Unload Me

Open in new window

0
 

Author Comment

by:al4629740
ID: 39892393
            YAT2.Show
 
              
            'YAT3 Form-------------------------------------------
            
            YAT3.Show
            
            YAT.PrintForm
            YAT2.PrintForm
            YAT3.PrintForm
            

            Unload YAT3

Open in new window

0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 39892402
Is there code in YAT3's  Form_Unload event?
0
 

Author Comment

by:al4629740
ID: 39892419
Ah yes

Private Sub Form_Unload(Cancel As Integer)
Unload Me
End
End Sub
0
 
LVL 45

Accepted Solution

by:
Martin Liss earned 500 total points
ID: 39892462
Private Sub Form_Unload(Cancel As Integer)
Unload Me
End
End Sub

Open in new window

Remove line 3. "End" should almost never be used since (as you have experienced) it shuts down the whole project and worse it does it in such a way that VB does not get a chance to clean up first and so parts of the app may be left "running" in the background.
0
 

Author Comment

by:al4629740
ID: 39892468
why wouldn't END clean up properly.  Doesn't it just simply shut it down?
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 39892569
why wouldn't END clean up properly.  Doesn't it just simply shut it down?
If you or I had written VB6 then it would do that ;) but here is what Microsoft has to say. This is from VB.Net documentation but it is similar.

The End statement stops code execution abruptly, and does not invoke the ... Finalize method, or any other Visual Basic code. Object references held by other programs are invalidated.
The Stop statement suspends execution, but unlike End, it does not close any files or clear any variables, unless it is encountered in a compiled executable (.exe) file.
Because End terminates your application without attending to any resources that might be open, you should try to close down cleanly before using it. For example, if your application has any forms open, you should close them before control reaches the End statement.
You should use End sparingly, and only when you need to stop immediately. The normal ways to terminate a procedure ... Exit Statement (Visual Basic)) not only close down the procedure cleanly but also give the calling code the opportunity to close down cleanly.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

747 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

13 Experts available now in Live!

Get 1:1 Help Now