Solved

How to REALLY unload a VB form from memory

Posted on 2003-11-15
11
2,126 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

743 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

15 Experts available now in Live!

Get 1:1 Help Now