Solved

How to REALLY unload a VB form from memory

Posted on 2003-11-15
11
2,140 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBS file using code from 2nd file (txt or vbs) 4 36
Run code from text file in vb 1 64
fso.FolderExists("\\server\HiddenFolder$") 4 64
Excel Automation VBA 19 67
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

815 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now