Results 1 to 11 of 11
  1. #1
    gammaman is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2013
    Posts
    56

    detect when 'X' clicked on form window to close form

    On my userform if they click the 'X' in the corner, I want to display a MsgBox and then call a function to do something else. I have some code but when the 'X' is clicked the code does not seem to fire.



    Code:
    Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
       If CloseMode = VbQueryClose.vbFormControlMenu Then
          cmdCancel.SetFocus
          MsgBox “Cannot close the userform !!”
          Cancel = True
       End If
    End Sub

  2. #2
    cyanidem's Avatar
    cyanidem is offline Competent Performer
    Windows 10 Access 2010 64bit
    Join Date
    Nov 2015
    Location
    Consett, UK
    Posts
    270
    Wouldn't be easier to just disable close button?
    In form parameters window set Close Button to No.

    Edit:
    Or set Control Box property to No, that will make all buttons (minimize, maximize, close) invisible, not disabled.

  3. #3
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2013
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    I believe you will find that you cannot cancel the Close event but you can cancel the Unload event.

  4. #4
    JeroenMioch's Avatar
    JeroenMioch is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2012
    Location
    Den Haag, Netherlands
    Posts
    368
    I have this module to disable the cross :

    Code:
    Option Compare Database
    
    Private Const GWL_EXSTYLE = (-20)
    Private Const GWL_STYLE = (-16)
    
    
    Private Const WS_MAXIMIZEBOX = &H10000
    Private Const WS_MINIMIZEBOX = &H20000
    Private Const WS_SYSMENU = &H80000
    
    
    Private Const HWND_TOP = 0
    Private Const SWP_NOMOVE = &H2
    Private Const SWP_NOSIZE = &H1
    Private Const SWP_FRAMECHANGED = &H20
    Private Const SWP_DRAWFRAME = SWP_FRAMECHANGED
    
    
    Private Declare Function SetWindowLong Lib "User32" _
    Alias "SetWindowLongA" (ByVal hWnd As Long, _
    ByVal nIndex As Long, ByVal dwNewLong As Long) _
    As Long
    Private Declare Function GetWindowLong Lib "User32" _
    Alias "GetWindowLongA" (ByVal hWnd As Long, _
    ByVal nIndex As Long) As Long
    Private Declare Function SetWindowPos Lib "User32" _
    (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, _
    ByVal X As Long, ByVal Y As Long, ByVal cx As Long, _
    ByVal cy As Long, ByVal wFlags As Long) As Long
    
    
    Sub HideAccessCloseButton()
    
    
        Dim lngStyle As Long
    
    
        lngStyle = GetWindowLong(hWndAccessApp, GWL_STYLE)
        lngStyle = lngStyle And Not WS_SYSMENU
        Call SetWindowLong(hWndAccessApp, GWL_STYLE, lngStyle)
        Call SetWindowPos(hWndAccessApp, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE Or SWP_DRAWFRAME)
    
    
    End Sub
    Activate it by calling HideAccessCloseButton
    Code:
    Call HideAccessCloseButton
    Works great for me.

  5. #5
    Micron is online now Very Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,372
    Doesn't that hide the application close button (they only want to affect the form window button)?
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,646
    What is QueryClose?

    I am not finding such an event.

    I am not aware of any way to detect if the X button is clicked, unless it produces some sort of code that can be captured in the form click or mouse events.

    I also disable the X close on all forms as well as the app X close and File > Exit.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    JeroenMioch's Avatar
    JeroenMioch is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2012
    Location
    Den Haag, Netherlands
    Posts
    368
    Might be, I locked my users out from both because i want to force them to exit through my custom close buttons.
    Some can have doubts about this approach, but since i did it, i never ever found a record that was unfinished again

  8. #8
    Micron is online now Very Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,372
    Quote Originally Posted by June7 View Post
    What is QueryClose?

    I am not finding such an event.

    I am not aware of any way to detect if the X button is clicked, unless it produces some sort of code that can be captured in the form click or mouse events.
    I think it's the name of the form?
    UserForm_QueryClose

    But it looks like Excel vba.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  9. #9
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,646
    Can't be the form name. There would be no event specified. Customarily see:

    Private Sub Form_Close()

    So the posted code is not Access VBA?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  10. #10
    Micron is online now Very Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,372
    Quote Originally Posted by JeroenMioch View Post
    Might be, I locked my users out from both because i want to force them to exit through my custom close buttons.
    Some can have doubts about this approach, but since i did it, i never ever found a record that was unfinished again
    I was once concerned about that also, but my recollection is that the effect applies to every Access db the user would open (i.e. it is application specific), and if not yours, there is no button to close the app. That's why I removed the code. Although I had reset the button on application close, this would not run if the db crashed or if it hung up and the user used Task Manager to shut it down. Perhaps your dbs are the only ones they ever open, but that is why I removed it.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  11. #11
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,646
    The code I use is project specific, not application-wide.

    There is button on main menu form which never closes by which users exit the db.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. How to disable save/close window in form
    By Kivan in forum Access
    Replies: 2
    Last Post: 08-16-2012, 02:52 AM
  2. Close window error on certain client computer
    By DavidLP in forum SharePoint
    Replies: 0
    Last Post: 05-23-2012, 11:53 AM
  3. Replies: 1
    Last Post: 11-28-2011, 04:06 PM
  4. Replies: 1
    Last Post: 02-17-2011, 06:23 AM
  5. Replies: 1
    Last Post: 11-09-2010, 03:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums