Solved

Disallow using red X to close Access from form

Posted on 2014-02-07
6
911 Views
Last Modified: 2014-02-10
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
0
Comment
Question by:gbnorton
6 Comments
 
LVL 4

Expert Comment

by:Jack Leach
Comment Utility
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
0
 
LVL 10

Expert Comment

by:Gozreh
Comment Utility
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

0
 
LVL 75

Accepted Solution

by:
DatabaseMX (Joe Anderson - Access MVP) earned 500 total points
Comment Utility
Consider this:
Windows users *expect* a Red X to close whatever (Form, Database, etc).  So, I suggest you find a workaround such that ... users can still use the Red X (or the Close button).

mx
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 84
Comment Utility
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.
0
 

Author Comment

by:gbnorton
Comment Utility
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
0
 
LVL 10

Expert Comment

by:Gozreh
Comment Utility
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.......
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now