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?