?
Solved

Disallow using red X to close Access from form

Posted on 2014-02-07
6
Medium Priority
?
1,021 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 2000 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 85
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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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 …
Suggested Courses

771 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