Close form without saving (form loads dirty) (1 Viewer)

tt1611

Registered User.
Local time
Yesterday, 20:56
Joined
Jul 17, 2009
Messages
132
Hi All
I have a situation that has been driving me absolutely mental that I hope someone can help me with.

This is a simple procedure. I have a form that loads pre-filled with some information on it like a textbox with the current date and 3 other text boxes that load disabled and have contact information in them that cannot be changed.

I am aware how access forms are and I know that the system does not create a new record till the user starts typing in the form.

I want the user to be able to click the close button and get prompted that they are about to lose inforrmation if the proceed to click yes. If the user clicks no, nothing happens and the user is left with the form still opening.

My forms are linked to SQL server tables.

I have tried inputting the following codes for the following scenarios.

Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.Dirty = True Or Me.NewRecord Then
If MsgBox("All unsaved data will be lost, are you sure you wish to close", vbExclamation + vbYesNo, "Form Close Warning") = vbYes Then
Me.Undo
Else

End If
End Sub

Private Sub Form_Unload(Cancel As Integer)
If MsgBox("All unsaved data will be lost, are you sure you wish to close", vbExclamation + vbYesNo, "Form Close Warning") = vbYes Then
If Me.Dirty = True Then
Me.Undo
Cancel = False
End If
End If
End Sub

me.dirty doesn't seem to call properly i guess because my form opens preloaded with information on it.

the unload event is not calling right either and I am finding that these "empty" records (ie records with no new information on them apart from the pre exsisting data is being written to the table which is completely against what i want

can some please help with this.

Thanks
 
You do not need to test for dirty in the before update event (the event you need to use) because it won't fire unless the form is dirty. However if you want to use Me.Undo you would need to set

Cancel = True

to stop the update.

The form's unload event is too late as the update has already occurred.
 
you form is opening "dirty" - because you have some code setting a particular value differently to that currently in the table (or maybe the same) - maybe it's what you call as pre-filled - in a bound form, the act of setting certain fields (controls) will make the record dirty.

if this behaviour isnt exactly what you want, then you need to pin down what is happening, because you may be inadvertently modfiying the data without realising
 
Hi Gemma
I am not sure what your post is trying to say. My form is loading "dirty" as this is how I do want it to load. With pre-filled textboxes that are bound to fields in the underlying table, eg current date.

Bob
Thanks for your help as always. Just trying to understand what you're saying. I cancelled using the form unload event as suggested but with the before_update event, heres the order of things

-form loads with me.dirty = true (information in prefilled boxes)
-user starts typing and decides he wants to close the form out
-message box pops up advicing loss of data and to confirm closure
-user hits yes. All data (commited/uncommited) is deleted from memory
-form closes
-user hits no, form closure is cancelled.

At what stage is data commited to the table? Is it when the user starts typing on the form? Then this would render the before_update event as the right on as you suggested.

My code now looks like below
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.Dirty = True Or Me.NewRecord Then
If MsgBox("All unsaved data will be lost, are you sure you wish to close", vbExclamation + vbYesNo, "Form Close Warning") = vbYes Then
Me.Undo
Cancel = True

Else

End If
End If

unfortunately this is not firing right and i am getting a pop up when the user starts typing. (bizzare)
 
what i mean is

a form wouldnt load "dirty", per se. Being bound to a query or table, just means that a record from the table/query appears in the form when it is opened, and you can navigate to other records in an assortment of ways.

------------
if you show the record selector (a vertical bar at the left of the form) this should show with a black triangle

now when a record is changed/edited the balack triangle changes to a pencil, and the record is now dirty.

------------
so if your record is immediately "dirty" when it first appears it MUST be that you have some code SETTING or FORCING a control(field) to a particular value.


Now you may or may not be actually changing data - I THINK that if you modify a value with code, but dont change it from the original value, this will STILL cause the record to be "dirty" - but you pught to understand what exactly is causing the record to be edited, as you may or may not actually need this change to be recorded

... and it is also this dirty status that is causing you to need special code to exit the form.
 

Users who are viewing this thread

Back
Top Bottom