Solved

How do I trap the X button click event

Posted on 2004-09-14
5
262 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
  • 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

778 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