Link to home
Start Free TrialLog in
Avatar of neha7777
neha7777

asked on

undo saving subform data

Hi all
I have a main form(company_price_master) in which i have a subform (Company_Price_master subform)as a datasheet. now i want an exit button on main form which when clicked should undo any changes in subform. i have put the code like this on exit button :
Me![Company_Price_master subform].Form.Undo

but it does not work ie it saves rather than undo.

i have searched for this in all forums but have not got solution for the same.

also for your information : there is no link between 2 forms.

pls help its  urgent.

neha
Avatar of nico5038
nico5038
Flag of Netherlands image

The only solution will be to copy the subform's records into a temporary table.
When cancel is pressed you do nothing, when save is pressed you need to delete the original rows from the original table and append the updated ones.

Clear ?

nIC;O)
Avatar of neha7777
neha7777

ASKER

thanks nic but thats very difficult i guess, isn't there is something easy????
Not too difficult, just a few INSERT and DELETE statements.
When multiple rows can be edited (and/or inserted and deleted) there's no real other option.

What's the recordsource of your subform and why isn't it linked to the main form ?

Nic;o)
Try this

DoCmd.RunCommand acCmdUndo

I used this on an old db.  I now have all my forms unbound.  Only when the user clicks an update button, and the data is validated does it update the tables.  So exiting any time before that makes no changes.

Iain
Avatar of Scott McDaniel (EE MVE )
You can undo the save from the Subform, but not the Mainform ... as soon as you move focus off the subform (by clicking the "Undo" button on your subform, for example), Access will save the data on the subform ... you can't get around it, and the only option is as Nico and Iain suggest. You can force the user to accept the changes as they move off the subform by using code in the BeforeUpdate event of the subform, but I suspect this isn't what you're looking for
You could save a backup of the sub at the OnCurrent event of the mainform:

Set rstBackup = Me!subChild.Form.RecordsetClone.Clone

Then, if the user presses Cancel, loop through this and the current RecordsetClone of the subform and reset field values by field. Then delete appended (new) records and finally add deleted old records.
You may, however, get into trouble with AutoNumbers, thus I think I would stick with the temp table and a couple of queries as suggested by Nico.

/gustav
As soon as you leave the subform and go to the parent form (or visa versa), the record is saved and can't be undone. However if the record is still displayed, you can delete it with:
Me.[Company_Price_master subform].SetFocus
DoCmd.RunCommand acCmdDeleteRecord

Although you may have a problem with the SetFocus command with the underscores and space in the subform name.
Actually
company_price master has this structure
starting range   ending_range  amount      change_date
1                        4                  666             2.12.05
5                        6                  8888           2.12.05
7                        10                76868         2.12.05

as u can see against single change_date multiple records can be enterd. so i do not want the user to enter the change_date again and again. HEnce  i want this as a subform (datasheet view) with only starting range ,  ending_range,  amount      and change_date in main form. So that when user choose a change_date , he can enter multiple records in datasheet of subform which has (starting range ,  ending_range,  amount only). but the problem as i wrote above is that incase user do not want to save this and cllicks Exit button , then the recors should not be saved. but currently it is saving. pls help..its very urgent.
Well, saved is saved and those records _are_ saved when you exit the subform.

However, if initially no records have been entered in the subform datasheet and you user types in one or more records but then wish to cancel these (all) entries, all you need is to delete those records when clicking the Exit button:

Private Sub btnExit_Click()

  Dim rst As DAO.RecordSet

  Set rst = Me![Company_Price_master subform].Form.RecordsetClone

  While Not rst.EOF
    rst.Delete
    rst.MoveNext
  Wend
  rst.Close

  Set rst = Nothing

End Sub

A reference (Tools, References) to "Microsoft DAO 3.xx Object Library" is needed.

If records may exist in the subform when you users makes some changes or add or delete some records, then you will have somehow to store the initial records so you later can throw away the new/edited records and replace them with the original (saved) records. You can save them as a temp table, a recordset or in an array but I guess for you the simplest method is to use a temp table as suggested by Nico.

/gustav
ASKER CERTIFIED SOLUTION
Avatar of nico5038
nico5038
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Nic..that was perfect..and thanks anyways gustav..
Glad I could help, success with the application!

Nic;o)