How to detect if user clicked the close button on a form...

JONeill

Board Regular
Joined
Sep 2, 2018
Messages
58
New to Access programming and I'm trying to find an equivalent Query_Close event to detect if the user hit the x in the upper right corner of a form. Found out how to disable it but not how to detect if it has been clicked. I'm sure it's pretty simple but I'm just not finding it out there.
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
EXCEL - I know you do not want this, but someone may find it useful
Code:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
'detect if clicked
    If CloseMode = 0 Then MsgBox "User clicked the X"""
'disable
    If CloseMode <> 1 Then Cancel = 1
    Me.Caption = "Click on button to close"
End Sub


ACCESS - I have not tested it but there appears to be a solution here
https://social.msdn.microsoft.com/F...-quotxquot-clicked-on-a-form-?forum=accessdev
 
Upvote 0
I think this does the trick...

Code:
Private closeFlag as Boolean


Private Sub cmdClose_Click()
      closeFlag = True
      DoCmd.Close
End Sub



Private Sub Form_Unload(Cancel as Integer)
 
 If closeFlag then
  MsgBox "Close button was pressed."
 Else
  MsgBox "Something else closed the form."
 End If



End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top