-
Basically I have an application that I have written in which I use a number of forms however when I no longer want to view the form I hide it as opposed to closing it. I do this because I need to retain the data that is on the form and so making it invisible is the easyest way of doing it.
If you close a form with the X is the top right the program ends only if there are no other objects active.
This is a problem in when I want to close the main form by clicking on the X in the top right because the other forms are still active the program is still running even though there are not visible forms on the screen.
My question is:
How can I either use the X in the top right to run some code that unloads all the forms that I have open or how can I disable the X in the top right whilst keeping the max and min buttons?
Thanks for your help.
Any further information required please ask
-
Try this on your main form
If using from Form_Unload of the Main Form, use:
UnloadAllForms Me.Name
If calling from another Sub, use:
UnloadAllForms ""
[code]
Public Sub UnloadAllForms(Optional frmName as string = "")
'frmName is the form name not to unload
Dim Form as Forms
For Each Form in Forms
If Form.Name <> frmName Then
Unload Form
Set Form = Nothing
End If
Next Form
End Sub
/[code]
-
Thanks for your reply
However you missunderstood what I am trying to achive.
Basically I have a button on my form that when you click unloads all the forms which then exits the program the problem is getting that code to be executed when I click on the 'X' in the top right of the screen.
At the moment clicking onthe 'X' closes the form and the program sits there waiting for input and won't close because there are other forms still active.
-
Hi Simon,
Use the Form_Unload event of the form you are closing to call the procedure attached to the Close button.
Watch out for recursive loops though...
Cheers,
P.
-
I think you might need to look at QueryUnload which occurs before a form or application closes. Try help files or ask me for more detail.
Code:
Private Sub Form_QueryUnload (Cancel As Integer, UnloadMode As Integer)
Dim Msg ' Declare variable.
If UnloadMode > 0 Then
' If exiting the application.
Msg = "Do you really want to exit the application?"
Else
' If just closing the form.
Msg = "Do you really want to close the form?"
End If
' If user clicks the No button, stop QueryUnload.
If MsgBox(Msg, vbQuestion + vbYesNo, Me.Caption) = vbNo Then Cancel = True
End Sub
-
Thanks guys!
I tried to find out what code was executed when you click the close button 'X' by putting debug.print 'Hello' in the form.unload and then putting a break point on the debug line but for some reason it ignoored it and so I assumed that those lines of code were not executed.
Sorted now thanks!
Simon