Solved

Unload a form and Load again

Posted on 2014-02-05
18
564 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 46

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 46

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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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 46

Expert Comment

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

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 46

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
 
LVL 46

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 46

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 46

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 46

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 46

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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no 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 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…
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…

821 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