RE: Confirm data change on close
- From: Klatuu <Klatuu@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 9 Jun 2008 10:44:13 -0700
We are both missing something here.
The reason I am forgetting it is I never use it. I don't see the point and
as you are discovering, it is hard to control.
I went back and reread your original code. First, there is no such thing as
DataChanged, unless you have a control with that name.
Also, the Close event has no Cancel parameter.
Try moving your code to the Unload event.
--
Dave Hargis, Microsoft Access MVP
"RickW" wrote:
Thanks Dave, but I still think the method of using the Form_Dirty property in.
the Form_Unload event will never work as a means of ascertaining whether the
record is saved.
I have tested this in a new mdb file and on two PCs, one with 2003 installed
and the other with 2007. Everytime the record is saved between
Form_BeforeUpdate and Form_AfterUpdate (which sounds logical), and therefore
before Form_Unload.
The form module code is:
Option Compare Database
Option Explicit
Private Sub cmdCloseForm_Click()
Debug.Print "Me.Dirty = " & Me.Dirty & " before DoCmd.Close"
DoCmd.Close
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer)
Debug.Print "Me.Dirty = " & Me.Dirty & " at Form_BeforeUpdate"
End Sub
Private Sub Form_AfterUpdate()
Debug.Print "Me.Dirty = " & Me.Dirty & " at Form_AfterUpdate"
End Sub
Private Sub Form_Unload(Cancel As Integer)
Debug.Print "Me.Dirty = " & Me.Dirty & " at Form_Unload"
End Sub
When there is an unsaved record and cmdCloseForm is clicked the immediate
window looks like this:
Me.Dirty = True before DoCmd.Close
Me.Dirty = True at Form_BeforeUpdate
Me.Dirty = False at Form_AfterUpdate
Me.Dirty = False at Form_Unload
Am I missing something?
Regards
Rick
"Klatuu" wrote:
I don't know what is going on in your form, but the record should not be
saved as you say it is being done. In fact, if you notice the definition of
the Unload event, it has a Cancel parameter, so you can cancel the closing of
the form if you want to. One thing you can do, is put a line of code in the
form's Before Update event to see exactly when it record is about to update.
Then run in debug mode to find out exactly when things are happening.
--
Dave Hargis, Microsoft Access MVP
"RickW" wrote:
Thanks - sorry for the lack of info.
What I'm trying to do is use the Form_Unload event to check for unsaved
records before closing the form.
If the record is unsaved, I want to ask the user:
1. if they want to save it,
2. if they want to close without saving, or
3. if they want to cancel, and stop the form closing
I have a Close button with the code:
DoCmd.Close acForm, Me.Name, acSaveNo
and I also have enabled the standard form close button at top right corner,
but it makes no difference. Using both methods results in an the record
being saved by the time Form_UnLoad is reached. In other words, DoCmd.Close
saves the record. I cannot therefore see how the code discussed previously
can ever work as I think it is intended.
Surely this is a pretty standard requirement in coding any kind of form?
I just wish I knew how to do it in Access 2003.
By the way, I don't have any code in the Form Before Update, and After
Update events, and I have tried all combinations of arguements for
Docmd.Close. Basically I'm at the end of my tether and looking forward to
Friday!
Hope you can help now
Regards
Rick
"Klatuu" wrote:
I'm not sure what you are saying.
Docmd.Close should fire the Form Before Update, After Update, Unload, and
Close events in that order.
--
Dave Hargis, Microsoft Access MVP
"RickW" wrote:
This method isn't working for me!
I am finding that the forms Dirty property is changed to False by
DoCmd.Close so the code in Form_UnLoad never recognises an unsaved record.
I feel I getting no where! Hope you can help!
Regards
Rick
"Klatuu" wrote:
For your save button:
If Me.Dirty Then
Me.Dirty = False
End If
For your Exit Button, just use Docmd.Close in the click event. Put the code
to check for changes in the Form's Unload event. That way, if the user
closes the form either using the exit button or by some other means, it will
stilll do the check.
Put this in the Form Unload event:
Dim str Msg As String
If Me.Dirty Then
strMsg = "Data has changed."
strMsg = strMsg & " Do you wish to save the changes?"
strMsg = strMsg & " Click Yes to Save or No to Discard changes."
If MsgBox(strMsg, vbQuestion + vbYesNo, "Save Record?") = vbYes Then
Me.Dirty = False
Else
Me.Undo
End If
End If
--
Dave Hargis, Microsoft Access MVP
"Travis (New User)" wrote:
Alright guys, thanks for all the help. All of your answers put me in the
right direction. I didn't do a very good job explaining exactly what I
wanted, but here is the code that I came up with after all of your
suggestions that works:
If Me.Dirty Then
Dim strMsg As String
strMsg = "Data has changed."
strMsg = strMsg & " Do you wish to save the changes?"
strMsg = strMsg & " Click Yes to Save or No to Discard changes."
If MsgBox(strMsg, vbQuestion + vbYesNo, "Save Record?") = vbYes
Then
'do nothing
DoCmd.Close
Else
Me.Undo
DoCmd.Close
End If
Else
'do nothing
DoCmd.Close
End If
On a form I have a close button, a save button, text boxes, drop down lists,
etc etc. I have taken away the microsoft given buttons at the top. The only
way for the user to interact with the form is by the objects I have given
them.
I wanted the save button to save the record automatically with no prompting,
but I wanted the exit button to check if there were any accidental changes
and ask the user to confirm what they wanted.
Again, thanks for all the help guys. You all got me on the right path!
- Travis
"Travis (New User)" wrote:
I want a data editing form to check for changes when the close button is
pressed by the user. The code I have entered reads "Run time error 13. Type
Mismatch" Any help would be greatly appreciated!
Private Sub Form_Close()
If Me.DataChange Then
Dim strMsg As String
Dim Cancel As Integer
strMsg = "Data has changed."
strMsg = strMsg & " Do you wish to save the changes?"
strMsg = strMsg & " Click Yes to Save or No to Discard changes."
If MsgBox(strMsg, vbQuestion + vbYesNo, "Save Record?") = vbYes
Then
'do nothing
Else
Me.Undo
Cancel = True
End If
Else
'do nothing
End If
End Sub
- Follow-Ups:
- RE: Confirm data change on close
- From: RickW
- RE: Confirm data change on close
- References:
- RE: Confirm data change on close
- From: RickW
- RE: Confirm data change on close
- From: Klatuu
- RE: Confirm data change on close
- From: RickW
- RE: Confirm data change on close
- From: Klatuu
- RE: Confirm data change on close
- From: RickW
- RE: Confirm data change on close
- Prev by Date: RE: application.wait
- Next by Date: Re: shortcut menu and popup modal form
- Previous by thread: RE: Confirm data change on close
- Next by thread: RE: Confirm data change on close
- Index(es):