Solved

Disallow using red X to close Access from form

Posted on 2014-02-07
6
924 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
ID: 39842301
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
ID: 39842376
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
ID: 39842663
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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 84
ID: 39843128
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
ID: 39843640
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
ID: 39846414
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

911 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

20 Experts available now in Live!

Get 1:1 Help Now