Search code examples
ms-accessvbams-access-2010

Disable the close key


I would like to ensure that the user is forced to use my close key embedded on the form in the database when wishing to exit to ensure that a routine is executed. I believe i currently have issues because the close key (marked by an 'x') in the top right hand corner of the database offers the user to bypass my exit key. Is it possible to remove that key so the user is 'forced' to exit using my way?


Solution

  • You can setup a TempVar like this:

    Application.TempVars.Add "blnEnableClose", False
    

    ... but ensure it runs as soon as possible when your Access project is opened. E.g. if you've got a form that's set to open automatically when your Access project is opened, then put it on that form's Open event.

    You can then run the following IF statement off every form's Unload event:

    Private Sub Form_Unload(Cancel As Integer)
    
        If _
            Application.TempVars!blnEnableClose = False _
        Then
    
            Cancel = True
    
        Else
    
            ' optional:
            ' DoCmd.Quit 
    
        End If
    
    End Sub
    

    You can also have the option to set Application.TempVars!blnEnableClose = True somewhere else in your project should you want the user to be able to use the standard Access quit button.

    Source: https://www.youtube.com/watch?v=YXDi8r0AMfU