About Cancel Button and Undo

G

Guest

Hi
please help me, access is giving a weared and hard time :-(
I have a form with 3 subforms in different tab pages. My form has a Cancel
button which simply use Undo to clear all forms and then set there
properties. The problem is that if I insert data in the main form and press
cancel, it works fine and clear and undo add data insertion. But if I have
any data in the form and in any of subforms, instead of doing Undo all data
insertion, it saves all information to the database. Following is my code:


Private Sub ClinicInfoCancel_Click()
On Error GoTo Err_ClinicInfoCancel_Click
Dim recNum As Integer
Me.VisitDate.SetFocus
recNum = Me.Form.Recordset.AbsolutePosition
Me.HistorySymptoms.Form.undo 'Used just to make sure all is undone
Me.OperationNotes.Form.undo
Me.Drug.Form.undo
Me.Form.undo

If recNum >= 0 Then
Me.Recordset.Requery
Me.Form.Requery
Me.HistorySymptoms.Form.Requery
Me.OperationNotes.Requery
Me.Drug.Form.Requery

DoCmd.GoToRecord , , acLast
DoCmd.GoToRecord , , acFirst
DoCmd.GoToRecord , , acGoTo, recNum + 1
Me.AllowAdditions = False
Me.AllowEdits = False
Me.ClinicInfoSave.Enabled = False
Me.ClinicInfoAdd.Enabled = True
Me.ClinicInfoEdit.Enabled = True
Me.ClinicInfoCancel.Enabled = False
Me.ClinicInfoClose.Enabled = True
Me.HistorySymptoms.Form.AllowAdditions = False
Me.HistorySymptoms.Form.AllowEdits = False
Me.OperationNotes.Form.AllowAdditions = False
Me.OperationNotes.Form.AllowEdits = False
Me.Drug.Form.AllowAdditions = False
Me.Drug.Form.AllowEdits = False
Else
''Similar code like above but with different button enabling options.
End if

Don't know why above code is saving when I am asking anywhere. Any help
would be really appreciated.

Rashid
 
S

Steve Schapel

Rashid,

The problem here is that the Undo method will only apply to unsaved
data. As soon as you move from the main form to a subform, the main
form record is saved, after which using Undo will not be applicable,
it's too late. Similarly, as sosn as you leave the subform (including
to click the Cancel button!) the subform record is saved, following
which using Undo will not be applicable, it's too late. Instead, you
will need to use code to delete the record. This may be fairly simple...
DoCmd.RunCommand acCmdDeleteRecord
.... and if the tables that the subforms are based on have beern set up
with Relationships to the table that the main form is based on, with
Cascade Deletes enabled, then you will only need to delete the main form
record and any subform records will automatically be deleted as well.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top