Cancel form Close() event

GJT

Registered User.
Local time
Today, 19:02
Joined
Nov 5, 2002
Messages
116
I have a Closeform() function......when the user clicks this button - the Form_BeforeUpdate event gets called. Here I have a call to a SaveChanges() where the user gets the chance to Save / Cancel changes. The dialog box is Yes/No/Cancel. Hence :

Yes - default action i.e. Save
No / Cancel - do not save changes

However, when the SaveChanges function returns, for both No and Cancel the form closes. The Cancel event should cancel the CloseForm event and keep the form open. How can I do this?

Similarly, when changes are made to a subform - if the user pages to the next record, BeforeUpdate()/Savechanges() gets called. If the user selects Cancel / No, although changes are not saved (using acCmdUndo), the record does not page to the next as required.

Thanks in advance for any help.
 
I am not sure about it, but I think that the call to SaveChanges should be in the Form_Unload event instead of the Form_BeforeUpdate event.
 
Why? When control has passed to Form_Unload - the opportunity to save changes has passed. SaveChanges() needs to be called in BeforeUpdate() to enable the user to rollback before changes are committed to the database.

Unless there is another way of rolling back changes in the Form_Unload()event that I do not know about????? Is there?
 
Public Function SaveChanges(theForm As Form, strDlgTitle As String) As Integer
Dim nResponse As Integer

If (theForm.NewRecord = -1) Then
nResponse = QuestionMessage("Do you wish to save changes?", strDlgTitle)
Else
nResponse = QuestionMessage("Amend record details?", strDlgTitle)
End If

Select Case nResponse
Case vbYes:
' do nothing - defaults to save action
Case vbNo:
' override default
DoCmd.RunCommand acCmdUndo
Case vbCancel:
SaveChanges = -1
Exit Function
End Select
SaveChanges = 0
'
End Function

The return value is used to set the Cancel flag in BeforeUpdate()
and hence cancel the update.

The problem is that BeforeUpdate() is initiated by the call to CloseForm()

i.e.

Public Sub Closeform(frmName As String)
DoCmd.Close acForm, frmName
End Sub

and when this line gets called the form_updates event sequence begins.

Nett effect, being that when control returns to closeform - the forms closes!How can I prevent this from happening?
 

Users who are viewing this thread

Back
Top Bottom