close all forms

What is the easiest way to make VB close all open forms.  Essentially when a user clicks on btnExit it should loop thru the app and close all the forms.

thanks
sopheakAsked:
Who is Participating?
 
AzraSoundConnect With a Mentor Commented:
put this in the forms unload event

Dim frm As Form
For Each frm in Forms
If frm.hWnd <> Me.hwnd Then Unload frm
Next
Unload Me
0
 
Erick37Commented:
Private Sub btnExit_Click()
    Unload Me
End Sub



Private Sub Form_QueryUnload(...

Dim frm as Form

For Each frm In Forms
    If Not frm Is Me Then
        Unload frm
        Set frm = Nothing
    End If
Next

End Sub
0
 
Erick37Commented:
Oops, the code should be in the Unload event...

Private Sub Form_Unload(...
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Glen RichmondCustomer Reporting Programmer.Commented:
Public Sub UnloadAllForms()
   Dim Form As Form
   For Each Form In Forms
      Unload Form
      Set Form = Nothing
   Next Form
End Sub

just pop this in a module and call it when ever you want to close all forms..


0
 
ArkCommented:
Though MS is not recommended, but End works in most cases
Cheers
0
 
shideyiCommented:
use the following code
Private Sub btnExit_Click()
  'define aForm as Form Object
  Dim aForm As Form
 
  'Forms refer to all loaded forms
  For Each aForm In Forms
    'loop through all loaded forms and unload them
    Unload aForm
    'use this statement to release the memory allocated for this form
    Set aForm = Nothing
  Next aForm

End Sub
0
 
AzraSoundCommented:
hey lets see how many times we can post this  =)
0
 
sopheakAuthor Commented:
Sorry for the delay, but I want to test a few of the answers

Thanks much
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.