Solved

Unload a form and Load again

Posted on 2014-02-05
18
687 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
[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
  • 10
  • 8
18 Comments
 
LVL 48

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 48

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
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!

 

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 48

Expert Comment

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

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 48

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 48

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 48

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 48

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 48

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 48

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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 …
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
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…
Suggested Courses
Course of the Month4 days, 21 hours left to enroll

635 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