Results 1 to 10 of 10
  1. #1
    ped's Avatar
    ped is offline Advanced Beginner
    Windows Vista Access 2010 64bit
    Join Date
    Aug 2011
    Posts
    51

    Prevent ACCESS 2007 Form from Closing Though X but only through Exit button.


    Hi please help me with vba to prevent ACCESS 2007 Form from Closing Though X but only through Exit button.
    Thanks

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,914
    How about just disabling the "X" when you start?
    http://www.calvinsmithsoftware.com/D...ControlBox.htm
    (RG for short) aka Allan Bunch Previous MS Access MVP - WinXP Pro, Win7 Pro Win10 Pro - acXP, ac07, ac10, ac13
    How to mark the thread as Solved!
    Teaching is not filling a bucket but lighting a fire.
    Borrowed quote..."Docendo discimus"

  3. #3
    ped's Avatar
    ped is offline Advanced Beginner
    Windows Vista Access 2010 64bit
    Join Date
    Aug 2011
    Posts
    51
    I exactly tried this way nothing happend when i close my form...
    It did not disable my form from being closed/by clicking on X....

    Thanks again

    Code:
    Private Declare Function apiEnableMenuItem Lib "user32" Alias _
        "EnableMenuItem" (ByVal hMenu As Long, ByVal wIDEnableMenuItem As Long, _
         ByVal wEnable As Long) As Long
         
    Private Declare Function apiGetSystemMenu Lib "user32" Alias _
             "GetSystemMenu" (ByVal hWnd As Long, ByVal flag As Long) _
             As Long
    Function EnableDisableControlBox(bEnable As Boolean, _
        Optional ByVal lhWndTarget As Long = 0) As Long
    On Error GoTo ErrorHandling_Err
    Const MF_BYCOMMAND = &H0&
    Const MF_DISABLED = &H2&
    Const MF_ENABLED = &H0&
    Const MF_GRAYED = &H1&
    Const SC_CLOSE = &HF060&
    Dim lhWndMenu As Long
    Dim lReturnVal As Long
    Dim lAction As Long
    lhWndMenu = apiGetSystemMenu(IIf(lhWndTarget = 0, Application.hWndAccessApp, lhWndTarget), False)
    If lhWndMenu <> 0 Then
         If bEnable Then
            lAction = MF_BYCOMMAND Or MF_ENABLED
         Else
            lAction = MF_BYCOMMAND Or MF_DISABLED Or MF_GRAYED
         End If
         lReturnVal = apiEnableMenuItem(lhWndMenu, SC_CLOSE, lAction)
    End If
    EnableDisableControlBox = lReturnVal
    ErrorHandling_Err:
        If Err Then
            'Trap your error(s) here, if any!
        End If
    End Function
    

  4. #4
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,914
    I see you answered the same way in AWF. Did you call this code from your opening form? EnableDisableControlBox(False)
    (RG for short) aka Allan Bunch Previous MS Access MVP - WinXP Pro, Win7 Pro Win10 Pro - acXP, ac07, ac10, ac13
    How to mark the thread as Solved!
    Teaching is not filling a bucket but lighting a fire.
    Borrowed quote..."Docendo discimus"

  5. #5
    ped's Avatar
    ped is offline Advanced Beginner
    Windows Vista Access 2010 64bit
    Join Date
    Aug 2011
    Posts
    51
    RuralGuy, my apologies I tried my luck in anoother forum too...

    I enter the whole code in standard module
    and then in the load event of the form i enterd

    Code:
    Private Sub Form_Load()
    EnableDisableControlBox (False)
    Me.Username2.Value = UCase(Environ("Username"))
    End Sub

    This is the form where i do not want user to close the form from X...


    Thanks again, appriciate your help.

    Quote Originally Posted by RuralGuy View Post
    I see you answered the same way in AWF. Did you call this code from your opening form? EnableDisableControlBox(False)


  6. #6
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,914
    To start, there is no space before the open paren: EnableDisableControlBox(False)
    This disables the X for Access but *not* the form. Each form has a property for that and you can set it in design view of the form.
    (RG for short) aka Allan Bunch Previous MS Access MVP - WinXP Pro, Win7 Pro Win10 Pro - acXP, ac07, ac10, ac13
    How to mark the thread as Solved!
    Teaching is not filling a bucket but lighting a fire.
    Borrowed quote..."Docendo discimus"

  7. #7
    ped's Avatar
    ped is offline Advanced Beginner
    Windows Vista Access 2010 64bit
    Join Date
    Aug 2011
    Posts
    51
    RuralGuy, thanks alot.... i really appriciate your help...
    I got it wrong..i thought this is for access form and not application.

    How can i prevent the form from closing? Please

    Thanks again


    Quote Originally Posted by RuralGuy View Post
    To start, there is no space before the open paren: EnableDisableControlBox(False)
    Quote Originally Posted by RuralGuy View Post
    This disables the X for Access but *not* the form. Each form has a property for that and you can set it in design view of the form.


  8. #8
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,914
    Create a public Boolean variable in your form (before any procedures). Set it true behind your Exit button and also DoCmd.Close acForm, Me.Name, acSaveNo. Then test the Boolean value in the UnLoad event and Cancel = True if the Boolean value is still False.
    (RG for short) aka Allan Bunch Previous MS Access MVP - WinXP Pro, Win7 Pro Win10 Pro - acXP, ac07, ac10, ac13
    How to mark the thread as Solved!
    Teaching is not filling a bucket but lighting a fire.
    Borrowed quote..."Docendo discimus"

  9. #9
    ped's Avatar
    ped is offline Advanced Beginner
    Windows Vista Access 2010 64bit
    Join Date
    Aug 2011
    Posts
    51
    Quote Originally Posted by RuralGuy View Post
    Create a public Boolean variable in your form (before any procedures). Set it true behind your Exit button and also DoCmd.Close acForm, Me.Name, acSaveNo. Then test the Boolean value in the UnLoad event and Cancel = True if the Boolean value is still False.


    Thanks again...that solves it...
    Thank you very much!

  10. #10
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,914
    Glad we could help.
    (RG for short) aka Allan Bunch Previous MS Access MVP - WinXP Pro, Win7 Pro Win10 Pro - acXP, ac07, ac10, ac13
    How to mark the thread as Solved!
    Teaching is not filling a bucket but lighting a fire.
    Borrowed quote..."Docendo discimus"

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

Similar Threads

  1. Replies: 3
    Last Post: 08-08-2010, 05:33 PM
  2. Prevent a form from closing
    By ksmithson in forum Forms
    Replies: 0
    Last Post: 07-15-2010, 12:49 PM
  3. Replies: 1
    Last Post: 06-25-2010, 09:56 AM
  4. Replies: 5
    Last Post: 01-14-2010, 03:37 PM
  5. Replies: 9
    Last Post: 12-28-2009, 04:01 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 - Senior Forums