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 Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Aborting the End command when Cancel is pressed in the Dialog 2

Status
Not open for further replies.

james0264

Programmer
Joined
Mar 1, 2005
Messages
52
Location
GB
How can I abort the End command when Cancel is pressed in the Dialog that is presented in the Form_Unload Sub? Many thanks.

My code I am currently using is as follows. The code at the moment ends the program even though the Cancel button was pressed.

Code:
Private Sub Form_Unload(Cancel As Integer)
  If MsgBox("Are you sure?", vbOKCancel) = vbOK Then
    End
  Else
    'Cancel the unloading
  End If
End Sub
 
Try:
Code:
Cancel = 1
Hope that helps

HarleyQuinn
---------------------------------
Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
Sorry, forgot to say that the above code goes in place of your current
Code:
'Cancel the unloading
Hope this helps

HarleyQuinn
---------------------------------
Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
Hey! Nice one HarleyQuinn. Here's a star.
 
Thanks, glad to help [smile]

HarleyQuinn
---------------------------------
Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
james0264,

don't wish to be rude, but did you actually check the Vb documentation for Unload? It is pretty specific ...
 
This is a handy peice of code:

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

    Select Case UnloadMode
        Case vbFormControlMenu
            MsgBox "The user chose the Close command (the ""X"") from the Control menu on the form."
        Case vbFormCode
            MsgBox "The Unload statement is invoked from code."
        Case vbAppWindows
            MsgBox "The current Microsoft Windows operating environment session is ending."
        Case vbAppTaskManager
            MsgBox "The Microsoft Windows Task Manager is closing the application."
        Case vbFormMDIForm
            MsgBox "An MDI child form is closing because the MDI form is closing."
        Case vbFormOwner
            MsgBox "A form is closing because its owner is closing."
    End Select

End Sub
 
Thanks Tyson. Very useful.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top