HelloWorld
HelloWorld

Reputation: 283

Access 2010 Form Close button

I was wondering if there is a way to know if the windows form button is clicked. I know of the CloseButton property but it seems you can only enable or disable it. I have forms closing when the user navigates through my app but would like to close the database only if the windows close button is pushed. So i need the close event to distinguish when I close it via programming or the user close via windows button. I could use a boolean check but was wondering if there is a more elegant way.

Also if there's a way to completely remove the button and not just have it greyed out looking ugly that would be amazing but I read that this may be unwise since this is a windows setting.

Upvotes: 1

Views: 7459

Answers (2)

Crazyd
Crazyd

Reputation: 416

In short I don't think their is a way, but the code HansUp provided is a work around. I disable the close button at top to ensure data is saved.

Upvotes: 0

HansUp
HansUp

Reputation: 97111

Here is the code module for a form which includes a command button named cmdClose. The form can not be closed except by the cmdClose click event. Even attempting to close Access itself will not allow the form to close ... and will abort Access shutdown.

Option Compare Database
Option Explicit

Private Sub cmdClose_Click()
    AllowClose "close"
    DoCmd.Close acForm, Me.Name
End Sub

Private Sub Form_Unload(Cancel As Integer)
    If AllowClose = False Then
        Cancel = True
        MsgBox "close aborted"
    End If
End Sub

Private Function AllowClose(Optional ByVal pClose As String) As Boolean
    Static blnClosedFromCode As Boolean
    If pClose = "close" Then
        blnClosedFromCode = True
    End If
    AllowClose = blnClosedFromCode
End Function

Upvotes: 1

Related Questions