Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to REALLY unload a VB form from memory

Posted on 2003-11-15
11
Medium Priority
?
2,155 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 150 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 101

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

 
LVL 101

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 75 total points
ID: 9757541
Could it be hidden?

try Asetup.show

or

ASetup.SetFocus
0
 
LVL 3

Accepted Solution

by:
NBrownoh earned 150 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 101

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 101

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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

824 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