Solved

How to REALLY unload a VB form from memory

Posted on 2003-11-15
11
2,142 Views
Last Modified: 2010-05-01
I'm using VB 4 but I assume form unloading is the same for all versions.

My question is simple: my program loads another form for "preferences" setup and, when the user presses OK, I save changes and unload the form but, of course, it DOESN'T upload since, for some reason, Visual Basic forms never really unload unless you do "Set [myform] = Nothing"

But even when I do set to nothing, it DOES NOT unload since whenever I try to load the form again, the "Form_Load" procedure doesn't fire. It only fires the very first time I load the form. This is driving me crazy. Here is the code I'm using in the unload procedure:

Private Sub Form_Unload(Cancel As Integer)

t = WrapASetup
Unload ASetup
Set ASetup = Nothing

End Sub

What am I doing wrong?

Thanks!
0
Comment
Question by:GizmoBlaster
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 3

Assisted Solution

by:NBrownoh
NBrownoh earned 50 total points
ID: 9757033
you dont need to tell the form to unload on the unload event, its allready unloading.

you can move your code from the _Load event to the _Activate event and that should fix your problem.
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 9757234
You need to put the
unload ASetup in another subroutine.  Do you have a close or unload form button.

If you are closing the form by hitting the X then simply remove the unload line.

mlmcc
0
 

Author Comment

by:GizmoBlaster
ID: 9757311
Thanks for your comments so far but I would really prefer a solution that would allow me to REALLY unload that damn form. For all sorts of reasons... and one of the problem is I want to load new controls in the form_load event and if the form still shows a loaded status, it returns a control already loaded error.

I can't believe there is no way to really unload a form in VB, is there?
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 100

Expert Comment

by:mlmcc
ID: 9757363
In one of the events (not Form_UNLOAD) put

Unload ME

In the Unload event

Set ASetup = Nothing

How are you telling the form to unload?

mlmcc
0
 

Author Comment

by:GizmoBlaster
ID: 9757437
The form unloads from various sources. Users click the X, they click OK (which spawns an unload event) and so on.

By the way, please guys stop telling me to not put the Unload command in the Unload procedure, I know it doesn't make much sense, it's just something desperate that I tried at the last minute.

The fundamental problem is, when my code does "Load Asetup" for the second time, it just doesn't show the Asetup form, it doesnt even trigger the "Activate" event. It does nothing...

0
 
LVL 11

Assisted Solution

by:bingie
bingie earned 25 total points
ID: 9757541
Could it be hidden?

try Asetup.show

or

ASetup.SetFocus
0
 
LVL 3

Accepted Solution

by:
NBrownoh earned 50 total points
ID: 9757830
for all of my tests if you use Unload ASetup then it WILL unload the form from memory completely, calling the ASetup.Show WILL trigger the _Load event again.  You DONT need to set the form = nothing since it is allready unloaded and that IS part of vb's _Unload event.
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 9758473
The unload statement in the unload event reloads the form.

Setting the form to nothing ensures the memory used by the form is freed up.

mlmcc
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 9758476
How do you know the form load event isn't firing?  What code do you have behind the event?

mlmcc
0
 
LVL 9

Expert Comment

by:dancebert
ID: 9758590
All you have to do is treat a form as if it were a class (which, behind the scenes, it is):

Check out http://www.geocities.com/r_larabee/class_idioms.html
0
 

Author Comment

by:GizmoBlaster
ID: 9759306
Thanks for all those who answered. It didn't "really" allow me to do what I wanted to do, but at least it helped me understand a few things that I had overlooked about how to best load a form.

I split the points between the 3 most helpful answers.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB6 / DAO 3.6 / run time error: Couldn't find installable ISAM 7 102
How to debug this code 7 64
Best way to parse out a json string in VB6? 10 218
Problem to With line 4 63
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

808 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