Confirmation on change when exit form or hit the X in corner

Thinh

Registered User.
Local time
Today, 11:34
Joined
Dec 20, 2006
Messages
114
I need to create a code that will pop a message box asking if the user want to save the change or not after they have edited the form when they are leaving the forms either by X in top corner or by going to a different forms. I try to use the after update and before update but no luck here. I create a global variable that is in before update and after update. The flag is check in the deactivation event but still close it down when i exit the form. any suggestion or code would be greatly appreciated.

thanks in advance
 
You need to do the check in the Unload event of the form and if the check fails then set Cancel=True.
 
Can you give me small exemple

I think my problems lies in that the forms is not tied to anything so i dont know if that makes any sense. I have a save form. Nothing get save until they hit the save button. Since the form is not tied to any records the before and after update is not working because it says if a record has been changed.
What i wanted is if any modification was made to the form on exit form just pop up a confirmation box to exit without savings.

Thanks
 
Bob is correct. You need to write some code in the On_Unload event of your form to check if the form was modified.
 
here is the code i wrote

Code:
Private Sub Form_AfterUpdate()
Flag = True
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer)
Flag = True
End Sub
Private Sub Form_Dirty(Cancel As Integer)
Flag = True
End Sub
Private Sub Form_Unload(Cancel As Integer)
Dim answer As Integer
If Flag = True Then
    If MsgBox("Exit without Saving?", vbYesNo, "Confirmation") = vbNo Then Cancel = True
End If
End Sub

Hopefully this makes more sense
 
Are you making sure to declare Flag in the general declarations section of a STANDARD (not Form) module, so it is available throughout your project?

Public Flag as Boolean
 
Right now it only use with this form

I have a dummy forms that user fill in the information nothing get save until the save button is click. I will only having one form so i wont need to be share with any other forms.

i have declare a Private flag as boolen on top decration of the form section.
 
I have a solution to that but i dont really want to try it

I can always create a dummy table that the dummy forms is tied to in that way it will be bounded to that table. once the the user decide to save or not save i just run a delete query for that dummy table. I really dont wanna delete and tables all the time because this will cause the db to grow. Another solution is to go into each controls on the forms on event change set the flag which in this case would be a standard global variable. I have up 25ish control that are inputtable. Those are my possibilities but i just wonder if there is an easier way to do this. A little bit lazy
 
I don't think that any of your events will fire on an unbound form.
Make the form pop-up and Modal.
Give them Save and Cancel buttons and disable close form options.
Run code from the buttons to either validate the data and then save and close form or just close form.

HTH

Peter
 
Or, go the easier route and bind the form to the table and in the Unload event use
Code:
If Me.Dirty Then
  If MsgBox("Exit without Saving?", vbYesNo, "Confirmation") = vbYes Then
      Me.Undo
  Else
      Cancel = True
  End if
End If
End Sub
 

Users who are viewing this thread

Back
Top Bottom