Close form without Updating

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm trying to create forms for adding data to tables, trouble is when you
close the form, if there is anything in the text boxes it tries to update the
table. I don't want closing the form to run an update, it would be useful if
it asked first maybe, but not without confirmation but can't see how to stop
it. I have tried adding a button to update, and another to close, but even my
own close button is trying to update the table. I have also tried using code
(below) as posted by someone else prior to an update to confirm whether or
not data should be saved, but the form still then goes on to try to save even
if you say no.

If MsgBox("Do you want to save the changes?", vbYesNo,"Confirm Change")
= vbNo Then
Cancel = True
Me.Undo
End If
 
Thanks, unfortunately it is still trying to update. I'm only using the red
cross at the top of the form and I just want it to discard everything and
close, this only works if there are no entries on the form, as soon as there
is anything on the form it tries to use it to update the table. Any other
ideas?! :o)
 
It is not your close button trying to do the update, it is the Close event of
the form.
Svetlana's suggestion will do the same thing. You need to move the code to
the Unload event of the form.
 
Hmmmmm, it's now producing more errors than before. It says the Undo command
isn't available. I've put the code under the unload event. In fact, I can't
even close the form when there's no data entered at all now.
 
I forgot to mention, you first need to check to see if it needs updating:

If Me.Dirty Then
If MsgBox("Do you want to save the changes?", vbYesNo,"Confirm Change") _
= vbNo Then
Cancel = True
Me.Undo
End If
 
Hi again... Ok, I've put that in the unload, (and added another End If!), it
now closes if there's nothing in the form again, but if there is any text,
still tries to validate what's in the boxes against what should be in the
table, complains if it fails the validation tests and says it cannot be saved
do you still want to close, or updates it if validation succeeds. If someone
starts making an entry and then wants to close the form and abandon what they
were doing that doesn't seem unreasonable to me?
 
I missed that End If, thanks.

Where is the other validation occuring? How are you handling it?
That could easily be the problem.
 
The validation is on the table, if you try to enter a value which is not in
the correct format it won't allow the entry. The point is, it shouldn't even
be doing anything if you just want to close the form, if the data does look
valid it saves it, I want it to just throw any values away, not start
checking if they pass validation on the table field.
 
This is exactly why I never use field or table level validation. You have
very little control over it. I do all my validation in the form, either at
the control or the form level. That way, I have control over whether it does
it, how it does it, and when it does it. The only advantage to table/field
validation, is that it is always consistent throughout the application.
 
Oh well... i've removed the validation from the table, and it still wants to
save before closing the form. Why can't it just discard what's been put on
the form? It shouldn't even matter if it's valid data or Martian. I'm
confused!
 
That is what Me.Undo does. Here is a copy/paste directly from VBA Help on
the Undo Method:

If the Undo method is applied to a form, all changes to the current record
are lost. If the Undo method is applied to a control, only the control itself
is affected.

There must be something else going on we are not seeing.
 
Back
Top