• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1074
  • Last Modified:

Unload a form and Load again

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
al4629740
Asked:
al4629740
  • 10
  • 8
1 Solution
 
Martin LissOlder than dirtCommented:
Please attach your project.
0
 
al4629740Author Commented:
The project is simply too big and contains sensitive material.  

what do you need to see?
0
 
Martin LissOlder than dirtCommented:
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
al4629740Author Commented:
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
 
Martin LissOlder than dirtCommented:
Here's a demo project.
Q-28357714.zip
0
 
Martin LissOlder than dirtCommented:
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
 
al4629740Author Commented:
I would like to unload the Form2 with and then load it again to execute the Load event
0
 
Martin LissOlder than dirtCommented:
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
 
al4629740Author Commented:
Nope.  That's the thing I don't get.  There are other forms that are hidden.  Does another form need to be visible?
0
 
Martin LissOlder than dirtCommented:
Please describe in detail what it is that you want to do.
0
 
al4629740Author Commented:
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
 
Martin LissOlder than dirtCommented:
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
 
al4629740Author Commented:
            YAT2.Show
 
              
            'YAT3 Form-------------------------------------------
            
            YAT3.Show
            
            YAT.PrintForm
            YAT2.PrintForm
            YAT3.PrintForm
            

            Unload YAT3

Open in new window

0
 
Martin LissOlder than dirtCommented:
Is there code in YAT3's  Form_Unload event?
0
 
al4629740Author Commented:
Ah yes

Private Sub Form_Unload(Cancel As Integer)
Unload Me
End
End Sub
0
 
Martin LissOlder than dirtCommented:
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
 
al4629740Author Commented:
why wouldn't END clean up properly.  Doesn't it just simply shut it down?
0
 
Martin LissOlder than dirtCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 10
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now