Help: detecting a CancelEvent in an event which occurs later

  • Thread starter Thread starter Andrew Coyle
  • Start date Start date
A

Andrew Coyle

Hi

I have a form with an exit button on it, with the simple command
docmd.CloseForm.
The form also has a BeforeUpdate event, which as part of the code pops a
message box and gets a response from the user.
I would like to use the response from the BeforeUpdate event to determine
whether I should also cancel a later event.

For example, if the user clicks the close form button without saving the
current record, I want to pop a box from the BeforeUpdate event saying do
they want to save the current record Yes No Cancel, and if they click cancel
I want to also cancel the close form event.

I notice that Access puts (cancel as Integer) into the event subroutine
headers, so I was wondering if perhaps that is what I need to use somehow.
Does the DoCmd.CancelEvent set these? and if so, how do I use them?

All help greatly appreciated
A
 
Cancel = True will cancel the update. Closing the form will automatically
cause the record to be saved, unless you cancel using BeforeUpdate or if
there are required fields incomplete. So, you should assume that the user
wants to save the record and only prompt if required entries are incomplete.
Otherwise, you just annoy the user.
 
Thanks Paul

I now have another problem. In BeforeUpdate I pop a
message asking the user if they want to save the record.
If they select NO then I set cancel = true

However, if the user closed the form by clicking on the
close ( looks like an x) button top right of the form, and
they reply NO to my dialog (which means I set cancel =
true), Access pops a message on the Exit Sub saying:
"You can't save this record at this time"
"Microsoft Access may have encountered an error while
trying to save a record. If you close this object now,
the data changes you made will be lost."
"Do you want to close the database object anyway?"

How do I stop this happening???

I tried setting warnings off, but cannot seem to get them
back on again. I had code under the On Got Focus for the
form under this one (ie the one it goes back to when this
form is closed) but it never seems to execute.

All help greatlly appreciated
A
 
If you did what I said, you wouldn't have that problem. :o)

See Tools|Option|Edit/Find|Confirm options to return SetWarnings to its
original state

Your BeforeUpdate should look something like this:

Sub Form_BeforeUpdate(Cancel As Integer)

Cancel = IsValid()=False

End Sub

Function IsValid() As Boolean

If IsNull(Me.somefield) Then
Me.Somefield.SetFocus
IsValid = False
Exit Sub
End If

'Do next check

IsValid = True

End Function
 
Thanks for the help Paul, its been excellent.

Your point is taken on how the interface works, however the particular
system has to match an existing one, so the functionality is emulating their
existing system.

regards
A
 
Back
Top