Trapping the close box

rockman

Senior Member
Local time
Today, 06:14
Joined
May 29, 2002
Messages
190
How does one trap the clicking of the close box (x)?

I would like to present the user with a "Save Changes?" msgbox before the window closes.

thx,
Jeff
 
Rich,

I'm referring to the close box on an individual form.

Thx for any help,
Jeff
 
Easiest way is to get rid of it and add a custom close button, put the validation code there
 
Excuse my frankness but --- "YuK"

I don't like the suggestion of getting rid of the close box. It is a standard operating system control and users are accustom to it's use.

Any other thoughts?
Jeff
 
Then you'll have to rely on the before update event, or you could replace the "standard" close button with one that looks exactly the same.
 
I'm nut sure what you mean by "trap". Anyway....

Private Sub Form_Unload(Cancel As Integer)
Dim intReturn As Integer
intReturn = MsgBox("Do you want to quit application ?", vbYesNo, "Quit ?")
If (intReturn = vbYes) Then
Else
Cancel = True
End If
End Sub

this will pop a message box when a form is closed. If the user clicks "No" then the form remains open.

RichM
 
Rich,

The BeforeUpdate call maybe what I am in need of here. I'll work on it, or if you have some code that would be good too. The one problem that I foresee is that the subroutine will not know if I'm updating the record by closing the window (via the close box) or if I'm updating the record becuase the user pressed my "Save" button.


RichMorrison,

I've tried using the Form_Unload call but by the time Access sends Form_Unload the current record is already saved, therefore my "Save changes?" message box would be too little-too late. Do you understand my dilema?


Thanks for your thoughts,
Jeff
 
You wrote
<<
I've tried using the Form_Unload call but by the time Access sends Form_Unload the current record is already saved, therefore my "Save changes?" message box would be too little-too late. Do you understand my dilema?
>>

What, you expect me to read the entire question ??:)

Have you tried a message box in the "Close" event of the form? I don't know if the current record is saved before the Close event but I would guess not.

HTH,
RichM
 
rockman said:
Rich,

The BeforeUpdate call maybe what I am in need of here. I'll work on it, or if you have some code that would be good too. The one problem that I foresee is that the subroutine will not know if I'm updating the record by closing the window (via the close box) or if I'm updating the record becuase the user pressed my "Save" button.

Your Save button can set a global variable to True as its first action. Then have the BeforeUpdate event check if that variable is true to see why we are changing the record.

If your form allows such things, remember that moving to another record will also trigger the BeforeUpdate event if the record is Dirty.
 
Thanks for your help all.

RichMorrison,

Unfortunately the Close event occurs even farther down the line:

Unload Þ Deactivate Þ Close

David,

It looks like the global variable idea is the only way to go. Thanks for the suggestion.
 
Well if you want any more useless advice, just let me know.

RichM
 
RichMorrison,

LOL on your last post.

Thanks for your input.

Sincerely,
Jeff
 
Jeff,

Thanks :)

Seriously, have you come upon a solution ?

RichM
 
I have not come up with a slick solution. The idea about trapping the BeforeUpdate event and then using a global variable to determine why the BeforeUpdate was called (e.g. "Save" button was clicked vs. close box was clicked vs. etc...) seems to be my best bet. It is a bit of a clug... I have to make sure I cover all possibilities with the global variable (such as Saving the record from the menu).

I should learn to accept "work-arounds" but they still leave a bitter taste in my mouth :( .

Jeff

As an aside: Does VBA have the feature to list the events that are constantly being generated? Apple's Hypercard would let you see the string of events in a window as they were being issued. Example:

MouseEnter
MouseOver
MouseOver
MouseDown
MouseUp
MouseClick
...

This would be particularly nice in my situation to see if I could trap for an event that is generated by clicking the close box that I don't even know is being sent.
 
dynamictiger,

If I understand your post, one could use RunCommand with acCmdUndo to undo changes (presumably in a BeforeUpdate event). This is true, but does not solve the problem of determining if the BeforeUpdate was generated by the close box clicked or by a different method (e.g. a menu item "Save").

If I've misunderstood your post please let me know.

Jeff
 
Jeff,

It seems like we are going in circles here. If you just want to ask the user if they want to save a record when the form is closed, then remove the Close option from the form's caption and substitute your own Close button on the form.

The code behind the Close command can test for "dirty" and ask the user if they want to save. Then respond appropriately to the user's reply.

Unless I am missing something fundamental, this should work. Of course I have missed a fundamental or two in my time :)

RichM
 

Users who are viewing this thread

Back
Top Bottom