Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How do I trap the X button click event

Posted on 2004-09-14
5
Medium Priority
?
265 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 200 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

885 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