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.
 

Some videos you may like

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
6,772
Office Version
  1. 365
Platform
  1. Windows
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
 

JONeill

Board Regular
Joined
Sep 2, 2018
Messages
58
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
 

Watch MrExcel Video

Forum statistics

Threads
1,109,359
Messages
5,528,228
Members
409,809
Latest member
VICKRAM

This Week's Hot Topics

  • Change military grades into rank
    Afternoon all Need help with formula that will change military rank (i.e. 1, 2, 3 into Amn, A1C, SrA). Running IF formula that does not work...
  • VBA COUNTIF SOLUTION
    Hi The following are the errors spread across the several columns from E to Q ie. 13 columns across several sheets with more than 500 rows per...
  • INSERT ROW WITH SPECIFIS TEXT IN A COLUMN
    Hi All! How can identify that that the row to be inserted has to be inserted before 1st row with specific text in column F. If I record the...
  • Auto-Create a monthly Sign in sheet for preschool students
    The image below is what each page looks like. Above is space for the "Child Name" "Month" "Class" School days are obviously Monday-Friday but...
  • VBA vlookup multiple results
    Hi folks, Hopefully someone out there can help. I have a list to vlookup which works (ish). the lookup only picks up the first instance of the...
  • Extract values for earliest/latest times
    I am trying to put together a formula to get the earliest start time, the latest end time from column A for each person in Column B-F without the...
Top