How can disable window close button for an MDB file?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • moonrb
    New Member
    • Nov 2013
    • 23

    How can disable window close button for an MDB file?

    hi

    How can disable window close button for an MDB file. because when a user closes application by clicking on window close button (right top corner), unsaved & incomplete data on the form gets saved in respective table. i want to prevent this from happening.

    plz give me a good suggestion

    thanx a lot in advance.
  • zmbd
    Recognized Expert Moderator Expert
    • Mar 2012
    • 5501

    #2
    without a lot of work, you don't.

    instead you prevent a form from closing as I explained to you here: http://bytes.com/topic/access/answer...on#post3763718

    These two questions are basically the same and such threads are occationally deleted on the grounds of double posting.

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32662

      #3
      Sometimes the rules about double-posting and having a single topic per thread can be confusing.

      However, whether or not you realise it, these questions do overlap (This one and the one linked in Z's post). The answer is also in that linked thread. Simply have a form that is open and that has Cancel = True in the Form_Unload() event procedure. Handle with care though, of course, as if it is always True then the database will never close!

      Comment

      • neelsfer
        Contributor
        • Oct 2010
        • 547

        #4
        i use this "call" in the "load" event of the startup form and it seems to work fine. Hope it helps?

        Code:
        Call SetEnabledState(False)
        The "Disable X" module code to save is:
        Code:
        Option Compare Database
        Option Explicit
        
        Private Declare Function GetSystemMenu Lib "user32" (ByVal hWnd As Long, _
            ByVal bRevert As Long) As Long
        
        Private Declare Function EnableMenuItem Lib "user32" (ByVal hMenu As _
            Long, ByVal wIDEnableItem As Long, ByVal wEnable As Long) As Long
        
        Const MF_GRAYED = &H1&
        Const MF_BYCOMMAND = &H0&
        Const SC_CLOSE = &HF060&
        
        Public Function SetEnabledState(blnState As Boolean)
            Call CloseButtonState(blnState)
            'Call ExitMenuState(blnState)
        End Function
        
        'Disable the Menu Option
        'Sub ExitMenuState(blnExitState As Boolean)
           ' Application.CommandBars("File").Controls("Exit").Enabled = blnExitState
        'End Sub
        
        'Disable the Close Button Option
        Sub CloseButtonState(boolClose As Boolean)
            Dim hWnd As Long
            Dim wFlags As Long
            Dim hMenu As Long
            Dim result As Long
               
            hWnd = Application.hWndAccessApp
            hMenu = GetSystemMenu(hWnd, 0)
            If Not boolClose Then
                wFlags = MF_BYCOMMAND Or MF_GRAYED
            Else
                wFlags = MF_BYCOMMAND And Not MF_GRAYED
            End If
            
            result = EnableMenuItem(hMenu, SC_CLOSE, wFlags)
        End Sub

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32662

          #5
          Neels, This is a dead thread (I'll close it now as possibly I should have earlier).

          The linked thread has a perfectly adequate and appropriate solution that is a single line of code within an event procedure.

          Comment

          Working...