destroy Form

Posted on 2003-03-20
Medium Priority
Last Modified: 2011-10-03
I'm pretty new with VB and need some help from Experts. How can I correctly destroy a Form that has been created at runtime with "new" operator? Here's an example...

maincode.bas module contains:
Sub Main()
    Dim oForm As frmFirst
    Set oForm = New frmFirst
    oForm.Show 1
    Set oForm = Nothing
End Sub

frmFirst has a button:
private sub butExit_onClick()
end sub

When I closed application with a cross at top-right, everything seems to be ok. When I just press "butExit" button, some memory is still held by an application and I must click "stop" in VB IDE or kill process with task manager. WHat am I doing wrong please?

Thanks in advance,

Question by:gilipolla
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 8178087
unload oForm
Set oForm = Nothing


Expert Comment

ID: 8178164
Any Form You load using the .show method when u want to destroy it you have to use unload to clear the memory held by it so the hide method just clear it from the screen and u can reshow the form again using the .show method and u also will notice that if this form has a text file and the user wrote anything on it when u reshow it it will appear with the the string entered be the user on the textfield and u can apply that on the optionbox and check box also. So I agree with johnMcCann
LVL 11

Expert Comment

ID: 8178171
when you exit use the option END.  this will clean any open windows and end the program.  Put the End option anywhere in the program.  But when you want to close a form use "Unload Me" option rather than hiding the form.

Unload Me ' unlad this form
End ' end Program

Good Luck!
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Accepted Solution

JohnMcCann earned 200 total points
ID: 8178188
Just one last thought

Sub Main()
   Dim oForm As frmFirst
   Set oForm = New frmFirst
   Load oForm
   oForm.Show vbModal
   UnLoad oForm
   Set oForm = Nothing
End Sub


Expert Comment

ID: 8178687
Put at top of form:

Private Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long

In code:
                      I.E: destroywindow(form2.hwnd)

replace form2 with the name of the form
LVL 44

Expert Comment

ID: 8178918
supnur---NEVER EVER use the End statement to terminate a program.  That is a VERY VERY bad habit.

It is like smashing a car into a brick wall to stop it, rather than applying the brakes.  Yes, END stops the program (RIGHT THEN AND THERE, with NO cleanup), but then so does smahing the car into the wall stop the car...but I would not want to be riding in the car when that happens.

If the program does not stop when all of the forms re unloaded and set to nothing, then there is soemthng else that needs to be cleaned up, and that MUST be addressed, to PRPOERLY terminate the program.  END is a VERY bad way to do it.


Expert Comment

ID: 8179656
End is bad - unless you have to stop an activeX.exe with circular references to another activeX.exe (the only circumstance where Microsoft suggests you  to use End statement). But this is very rare...

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
Suggested Courses

741 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