Link to home
Start Free TrialLog in
Avatar of gbnorton
gbnortonFlag for United States of America

asked on

Disallow using red X to close Access from form

What I want is if the red X in the far upper right corner is pressed, the user is presented the message "You must use one of the Close buttons..." and returned to the form.  Here is the code:
Form_BeforeUpdate event:

Private Sub Form_BeforeUpdate(cancel As Integer)
If MsgBox("You must use one of the Close buttons...", vbOKOnly) = vbOK Then
cancel = True
Exit Sub
End If
End Sub

Problem 1: If no change has been made on the form, this event is ignored.  Access is closed.  

Problem 2: If a change has been made, the "You must use one of the Close buttons..." is displayed, but it is immediately followed by an Access message:
You can't save this record at this time.  Do you want to close the database object anyway?  Yes.  No.

Again, I want to force the user to use one of the Close buttons on the form.

Thanks,
Brooks
Avatar of Jack Leach
Jack Leach

http://support.microsoft.com/kb/300688

You can disable the close button, forcing them to use the form close button (see link above), but capturing when they click it will be extremely difficult - not feasible.

You can use the Close event of a form, but by the time it runs, there's so much else that has already happened, the application is in an unstable state and is difficult to save.  I wrote about that here:

[link removed]

hth
Avatar of Gozreh
if you want just to disable the close button on the form you can set the property on form CloseButton to No.
or you can use the forms Unload event
Private Sub Form_Unload(Cancel As Integer)
   Dim setExit As VbMsgBoxResult
   setExit = MsgBox("Exit the application?", vbQuestion + vbYesNo, "Exiting application")
   If setExit = vbNo Then
      Cancel = True
      Exit Sub
   ElseIf setExit = vbYes Then
       DoCmd.Quit acQuitSaveAll
   End If
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Windows users *expect* a Red X
Agreed 100%. MSFT spends a TON of money each year doing user testing, and if they determined the red x shouldn't be there, it'd be gone.
Avatar of gbnorton

ASKER

I appreciate all comments.

The dilemma:  Create a query based on a table.  Create a form bound to the query.  User opens form.  Makes a change to a textbox.  Decides to abandon and hits the red X.  Now I have a record with erroneous data.  

My work around has been to not use bound forms at all.  I use unbound forms and save using recordsets.    It works great.  I don't get bad records.  But it is a lot of work.  Recently I was advised to go back to bound forms and use code similar to that posted above to avoid the red X problem.  

Using bound forms, how can you avoid bad records when the red X is used?  I'll post that as another question.

Thank you,
Brooks
You can make a public Boolean called RecordNotSaved, then by the Form_Dirty event set RecordNotSaved to true, so now you can check by Form_Unload if RecordNotSaved = True give a msg to user if he wants to save the record and if not undo his changes.......