Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Close an App

Status
Not open for further replies.

bont

Programmer
Sep 7, 2000
200
US
I would like the to know, what is the best way to switch between forms and how to end the entire application when the X in the top right is clicked, or from a command button. I see so many variances. I would like to stay in the VB code, and not deal with the OS query system. Any suggestions welcome?
 
The following code will give you what you want. It also queries the user to ensure the user wants to close the app.
The third subroutine can be called when the 'close' on a menu is selected or say a command button.
Note the code in the 2nd sub - it ensures all forms open are closed.
Please ask if any questions.

code

----
Private Sub Form_QueryUnload(Cancel As Integer, UnLoadMode As Integer)

Dim intResponse As Integer

' display a message and verify the user wants to end the application
intResponse = MsgBox("Are you sure you want to exit this application?", _
vbYesNo + vbDefaultButton1 + vbInformation, "Exit")

If intResponse = vbNo Then
Cancel = True
Else
Call Form_Unload(Cancel)
End If

End Sub
------
Private Sub Form_Unload(Cancel As Integer)

Dim i As Integer

' close all forms that are opened
For i = Forms.Count - 1 To 1 Step -1
Unload Forms(i)
Next

End

End Sub
------
Private Sub mnuFileClose_Click()

Dim Cancel As Integer
Dim UnLoadMode As Integer

' call the procedure 'form_unload' when 'exit' from the
' menu is selected
Call Form_QueryUnload(Cancel, UnLoadMode)

End Sub

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top