Results 1 to 6 of 6
  1. #1
    Chris033115 is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Mar 2015
    Posts
    22

    Lost in loop preventing close from "X" on form

    I have a form for which I want to control the exit/close. At the beginning of data entry the user provides the number of transactions he/she is inputting. I've placed an <EXIT> button on the form. If they select the <EXIT> button before all the transactions have been input, a warning pops up. They can choose to continue with closing the form OR they can return to it and complete data entry. If all transactions have been input the form can just close.



    I don't want them to be able to select the "X" in the upper right corner to close the form.

    I have half this problem solved. I've established the Boolean variable AllowClose and set it to False when the form loads.

    Code:
    Public Sub Form_Load()
    Dim AllowClose As Boolean
    AllowClose = False
    End Sub
    On the Unload event I've added the following code:

    Code:
    Private Sub Form_Unload(Cancel As Integer)
    If AllowClose = False Then
    MsgBox "Click the EXIT button to close this form"
    If Me.Visible = False Then Me.Visible = True
    Cancel = True
    Else
    Cancel = False
    End If
    End Sub
    This works. I click the X and the message pops up directing me to the EXIT button. As described above, the EXIT button either closes the data entry form or opens a summary (form) with the option to return to data entry or truly EXIT. (This was working before I started altering it to control the "X" close.)

    I'm caught in a loop and can't close from anywhere now. If I select the "X" I get the message to use the Exit button. When I press the Exit button, I'm told to use the Exit button and then a second message appears saying the "Close action was cancelled".

    I've stripped down the code behind the Exit button to the following but I'm still caught in the loop and can't close the form without removing all the code referenced.

    Code:
    Private Sub btnExit_Click()
    AllowClose = True
    DoCmd.Close
    End Sub
    What am I missing? Why doesn't it recognize that AllowClose has been changed to True?

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,549
    Since you declared ALLOWCLOSE inside the FORM LOAD sub, it is only valid in that 1 sub.

    Public Sub Form_Load()
    Dim AllowClose As Boolean
    AllowClose = False
    end sub

    if you want it available to ALL subs it must be declared in the module level at the top of the form

    Code:
    option explicit
    private mAllowClose As Boolean   ' put m in front of variable to show its a module level variable
    
    Private Sub Form_Load()
    mAllowClose = False
    End Sub

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,625
    Why don't you disable the X close button in form properties? Set Close Button to No and/or Border Style to Thin.

    Disabling the application X close is another issue.
    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.

  4. #4
    Chris033115 is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Mar 2015
    Posts
    22
    Thanks so much! That works. Took a while to find its proper place as it was dropping in below other Subs. Finally realized it had to be at the TOP, above them all. You said that... I just didn't put it in perspective.

  5. #5
    Chris033115 is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Mar 2015
    Posts
    22
    I'm new to Access and all the options and different ways of doing the same thing are somewhat overwhelming. I'll look at disabling the X close button, but for this I really wanted a message directing users to the form's EXIT button and not just forcing them to become aware that X doesn't close the form. I now have the message to users working with the other solution provided.

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,625
    Correction, just setting to Thin does not disable or hide the X (setting to None does). Now trying to remember how I got the form X to not show.

    I have code that disables the application X close as well as the File Exit and Close Database.
    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. "Close form" action does not release table
    By TFisher in forum Programming
    Replies: 8
    Last Post: 10-23-2022, 11:54 AM
  2. Replies: 2
    Last Post: 10-01-2014, 12:38 PM
  3. Replies: 5
    Last Post: 09-01-2014, 12:11 PM
  4. Replies: 15
    Last Post: 02-17-2012, 09:20 AM
  5. Replies: 2
    Last Post: 11-10-2011, 07:45 PM

Tags for this Thread

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