Solved

How to REALLY unload a VB form from memory

Posted on 2003-11-15
11
2,136 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
vba find the last empty column 10 88
VB error "Type mismatch" 2 49
VBA to copy paste columns form one file to other 20 85
VBA: Select SQL query based on a config Sheet v2 11 38
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

914 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