automate undo unless saved

ktrasler

Registered User.
Local time
Today, 22:40
Joined
Jan 9, 2007
Messages
39
Hi

I have a form that is has record source set to a table.

if the user does not click the save button I want any changes that have been made to the record to be undone. i.e if the user makes a change but then clicks next/prev record.

I only want the record to save when the user clicks on a save button so that errors/mistypes are reduced.

I hope this makes sense.

Cheers

Kev
 
if the user does not click the save button I want any changes that have been made to the record to be undone. i.e if the user makes a change but then clicks next/prev record.
You will have to write a Me.Dirty statement. It checks to see if the form has been altered in any way. An example of notifying a user that changes will be discarded when they push a "next record" button would be something like this:
Code:
Sub NextRecordButton_Click()

dim message, buttons, choice

  if me.dirty = true then

Message = "This record has been altered." & vbcr & _
   "Moving to the next record will discard any changes that have been made to this form." & vbcr & _
   "To save the changes, cancel this and push the save button.  Do you want to move to the next record?" 

Buttons = vbyesnoCancel

Choice = msgbox(message, buttons)

  if choice = vbyes then
    [COLOR="Red"][B]me.undo[/B][/COLOR] [COLOR="Red"][B](this cancels the changes)[/B][/COLOR]
    docmd.gotorecord me.name, acdataform, acnext
  end if

End sub
One thing to note here is that Me.Dirty only applies to controls that are part of form they are on. In other words, unbound controls on a bound form do not qualify under the Me.Dirty conditional check.
 
Adam's code is one standard way of handling this sort of thing, and is, in fact, similar to the way that I usually handle it, but

  1. It assumes that the OP has a custom Next button
  2. Would require the same code in a custom Previous button and Close Button
  3. Would require removing the native Access Navigation buttons and the Quit button
  4. Doesn't perform in the manner the OP wishes, which is to simply dump the record if it isn't explicitly saved with a Save button

The code below declares a variable of the type Boolean and sets it to true when the Save button is clicked.

At the top of your form’s code module, immediately below the Option statement (Option Compare Database, Option Explicit, etc) declare the Boolean variable, SavedFlag:

Dim SavedFlag As Boolean

Then place this code in the code module:
Code:
Private Sub SaveButton_Click()
  SavedFlag = True
  If RecordsetClone.RecordCount > 1 Then
    DoCmd.GoToRecord , , acNext
    DoCmd.GoToRecord , , acPrevious
  End If
End Sub

Private Sub Form_BeforeUpdate(Cancel As Integer)
If SavedFlag = False Then
  Me.Undo
Else
  SavedFlag = False
End If
End Sub

Private Sub Form_Current()
  SavedFlag = False
End Sub

I think this will do exactly what you want.

Linq
 
I would make some changes to missinglinq's suggestion. My changes use the save command to save the current record rather than moving back and forth in the recordset and I would also give the user the option to save rather than simply discarding his changes without warning.
Private Sub SaveButton_Click()
SavedFlag = True
DoCmd.RunCommand acCmdSaveRecord
End Sub

Private Sub Form_BeforeUpdate(Cancel As Integer)
If SavedFlag = False Then
If MsgBox("Do you want to save the changes?",vbYesNo) = vbNo Then
Me.Undo
End If
Else
SavedFlag = False
End If
End Sub

Private Sub Form_Current()
SavedFlag = False
End Sub
 
I like your code Linq. That's pretty good, and it's a useful consolidation of potential redundancy. :)
 
Adam's code already covered making the user choose whether to dump or not, and as I said, that's usually how I handle it, but that's not what the OP requested! Forcing a save with DoCmd.RunCommand acCmdSaveRecord works fine, unless the user made a change, Saved it, then made a another change, without leaving the record, but elected not to save this second change. Since the SavedFlag was set to True from the first click on Save, this second, unwanted change, would still be saved! Moving back and forth in the recordset not only saves the record, it also resets SavedFlag back to False so that a second change will only be saved if the Save button is once again clicked.

Linq
 

Users who are viewing this thread

Back
Top Bottom