Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unload project

Status
Not open for further replies.

SkennyR

Programmer
Mar 7, 2004
157
US
Hi all..
I have a small program with 4 forms and 1 module.
When I exit the program (from form1), I use this method:
Set Form4 = Nothing: Unload Form4
Set Form3 = Nothing: Unload Form3
Set Form2 = Nothing: Unload Form2
Set Form1 = Nothing: Unload Me
Is this sufficient to unload everything from memory, or do I need to use one of the methods previously posted?
It wouldn't let me set module1 to nothing, and I have a couple of global variables in there.
Thanks...
 
I've seen different versions of this but esentially, if you cycle through the forms collect and close all the forms you should be good. (This is off the top of my head so it's not tested)

Code:
'In the mainForm unload event
Dim frm as Form

For each frm in Forms

  If frm.Name <> "mainForm"
    Unload frm
  End If

Next

'Finally unload mainForm
  Unload mainForm

Others have posted similer code so you can also search for other examples
 
Issuing an Unload command for a form that is not currently loaded will cause the loading of the form because no action associated with a form can be processed until that form is loaded.

A module isn't an object in the sense that a form or an instance of a class are objects so you can't set it to Nothing (nor to any other value for that matter.)

The usual way to do this is
Code:
Private Sub Form_Unload(Cancel As Integer)
   Dim f As Form
   For Each f In Forms
      If f.Name <> Me.Name Then Unload f
   Next
End Sub
That gets rid of your other loaded forms before the current form is closed by the system in the Form_Unload event.
 
Thanks to both of you.
I am using Golom's example now.
But one more question:
does this method clear up all memory?
Thanks again!
 
I dont think it is necessary to unload all your forms when closing the app.

We have many projects with 50+ forms and dont unload all the forms when closing the main one.

You will know when closing Form1 if you need to unload any of the forms because the program will continue to run and not close. If you deal with all yours forms correctly you shouldnt need to do what you are doing.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top