Close Button pressed, how to check it ?

  • Thread starter Thread starter Rob Hofkens
  • Start date Start date
R

Rob Hofkens

Hello people :)

When I close a form via the close button I notice that the form is updated
automaticaly when the record is dirty.
I would like the more common way when someone presses the close button while
data has been changed.
I want a dialog box that asks the user to confirm the canclation of the
things he/she has done.
If the users continues then the record is saved if the user canceled then
the record isn't saved.

I tried to use the BeforeUpdate event of the form but I don't know how to
check if the close button has been pressed.
Only in that case the dialog should appear.

Any solution would but apreciated :)

Thanks in advance,

Rob.
 
In the Unload event of the form:

If Me.Dirty Then
If MsgBox("Click Yes to Save Data Before Closing", vbYesNo +
vbQuestion, _
"UnSaved Data") = vbNo Then
Me.Undo
Else
Me.Dirty = False
End If
End If
 
Hi Dave, I just tried this code of yours and it didn't work out.
When I press the red close button on the form the record is saved before
entering the Unload event.
Or is there something else I can do ?

Thanx in advance !

Rob.
 
That is strange. This is not an uncommon way to handle this. Try running
the code in debug mode and see what the Diry property is after each line of
code is executed in the UnLoad event.
 
Dave, I removed all code in the forms module except the Form_Unload sub to
avoid any conflicts or whatever.
The only code in the Form_Unload sub is the code you provided.
I placed a Break on the first line of code ( If Me.Dirty Then) and executed
the form.
When I change the fields and close the form with the red close button the VB
editor jumps to the first line of code in the Unload event.
Before I step through it I see that the Me.Dirty property is alread set to
false !
I also checked the table at that point and noticed a new record already
added.

When you are sure this code should work then I presume that the problem must
lie in the fact that I am using a ODBC connection to a SQL2000 server ?
If this is the case then I wonder if there is an event that triggers before
the Form_BeforeUpdate event when closing the form?

Thanks again for your help !

Rob.
 
Since you are using an ODBC connection to SQL Server, I am at a bit of a
loss. It has been several years since I used that.

Before you tried closing the form, did you make any changes to the record
that would cause the form to go Dirty?
 
Yes I made changes before I closed the window and to check if the form goes
dirty I added a simple sub:

Private Sub Form_Dirty(Cancel As Integer)
MsgBox ("Dirty!!")
End Sub

The Dirty sub triggered nicely when I changed the textboxes on the form.

Rob.
 
Rob, I will have to do some testing on this. Problem is I have a crunch
project and may not get to it until tomorrow.
 
That's no problem at all...I am glad that we get such great help from MVP's
at the first place.
Without the help you guy's provide I would have given up a long time ago :)

Rob.
 
I just tried the same test with a new database without a ODBC connection to
the SQL Server.
Made a table with 2 fields and created quickly a form with the wizard.
I did some testing and came to the same conclusion.
The table is already updated before entering the Form_Unload sub.

The only way I could prevent an update is using the BeforeUpdate event.
The problem is that I need to know in what way the update is caused.
Did the user press PgUp, PgDn or Enter key? Or was it the red close button ?
Or is there some event that triggers only when the red close button is
pressed ?

Rob.
 
Rob,
I apologize for being so long getting back. It has been a tough week here.
I put in over 50 hours getting ready for month end closing with a major
change to our database structure. (I need a nap)

I did do some testing, and was not able to recreate the problem you are
having. The form's Unload event can be canceled, so I would try using that.

I would try putting a message box in the Before Update event of the form to
see when the update is occuring. There are so many different ways an update
is triggered, it is hard to track down.
 
Back
Top