Solved

Disallow using red X to close Access from form

Posted on 2014-02-07
6
983 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 - Microsoft MVP, Access and Data Platform) 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: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

739 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