Solved

How do I trap the X button click event

Posted on 2004-09-14
5
263 Views
Last Modified: 2010-05-02
Hi all,

I want to ask users "Are you sure you want to exit?" when the click the X on the top right of a window.

In the form_Terminate() event I have the following code which asks the question:-

Private Sub Form_Terminate()

Dim strResponse As String

strResponse = MsgBox("Are you sure?", vbOKCancel)

If strResponse = vbOK Then
    End
Else
    ??
End If

End Sub

What can I put in the else clause to keep the form open?

Cheers,
Shivermetimbers
0
Comment
Question by:shivermetimbers
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 67

Accepted Solution

by:
sirbounty earned 50 total points
ID: 12053932
Cancel=True
0
 
LVL 44

Expert Comment

by:bruintje
ID: 12053964
you could try the query unload event

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
   If UnloadMode = vbFormControlMenu Then
      If MsgBox("The user used the Forms Close button" & vbCrLf & "Cancel Unload?", vbYesNo) = vbYes Then
         Cancel = True
      End If
   End If
End Sub

HTHAB
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 12053971
Private Sub Form_Unload(Cancel As Integer)
resp = MsgBox("Sure?", vbOKCancel)
If resp = vbOK Then
    Unload Me
Else
    Cancel = True
End If
End Sub
0
 
LVL 3

Expert Comment

by:aParser
ID: 12053984
Right, but Cancel = True needs to go in the Form_Unload event, not Form_Terminate.
0
 
LVL 44

Expert Comment

by:bruintje
ID: 12054035
lol :)
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question