There are too many threads related to this topic, but none of them are specific to my example. I know how to close a program, but I want to check if my method is the most efficient.

In all of my programs, I have this code in the main form:

----------------------------------------------------------------------

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)

Dim frm As Form

'Make sure everything is unloaded.
For Each frm In Forms
Unload frm
Next

End Sub

----------------------------------------------------------------------

In a specific program I'm working on, I also include this code in the Form_QueryUnload to clean up the tray icon display:

----------------------------------------------------------------------

t.cbSize = Len(t)
t.hWnd = pichook.hWnd
t.uId = 1&
Shell_NotifyIcon NIM_DELETE, t

----------------------------------------------------------------------

All of this works great. However, if I want to give the option to quit the program on another form (presented via frmMyform.Show vbModal), what is the best way to do that? I could just copy and paste all of the above into the frmMyForm form's Form_QueryUnload and change the Shell_Notify code to point to the primary form. Another option would be to have a global Boolean variable that gets set to True if the user chose the exit option from the frmMyForm form. For example:

----------------------------------------------------------------------

frmMyform.Show vbModal
if isItTimeToExit = True then Unload Me

----------------------------------------------------------------------

I think either method would work, but can someone think of a more efficient way to exit?