Closing a Bound Form without Saving to the underlying table?

I

Iain

Is it possible to close a form with fields bound to a table, without saving
any data that is in the fields on the Form?

I've tried using acSaveNo but the data still gets saved.
 
A

Arvin Meyer [MVP]

You can undo the form. Code is:

Me.Undo
Me.Undo

I do it twice to make sure that, it is done (and not being confused with
undoing the last control)

You will still lose any autonumber that has been used.
 
L

Linq Adams via AccessMonster.com

FYI, acSaveNo and acSave both refer to saving or not saving ***design
changes***
 
K

KenSheridan via AccessMonster.com

Iain:

You might find the following helps. It’s the code for the module of a simple
form with Save and Undo buttons, in which changes to data in the current
record can only be saved, with user confirmation, via the Save button:

''''module starts''''
' updates can only be saved via command button
Option Compare Database
Option Explicit

Dim blnSaved As Boolean

Private Sub cmdSave_Click()

Const MESSAGETEXT = "Save record?"

If Me.Dirty Then
' if user confirms set variable to True and attempt to save record
If MsgBox(MESSAGETEXT, vbQuestion + vbYesNo, "Confirm") = vbYes Then
blnSaved = True
On Error Resume Next
RunCommand acCmdSaveRecord
' if record cannot be saved set variable to False
If Err <> 0 Then
blnSaved = False
End If
Else
blnSaved = False
End If
End If

End Sub


Private Sub cmdUndo_Click()

' undo edits
Me.Undo

End Sub

Private Sub Form_AfterUpdate()

' reset variable to False
blnSaved = False

End Sub

Private Sub Form_BeforeUpdate(Cancel As Integer)

' cancel update if variable is False,
' i.e. save button has not been clicked
If Not blnSaved Then
Cancel = True
End If

End Sub

Private Sub Form_Current()

' reset variable to False
blnSaved = False

End Sub


Private Sub Form_Error(DataErr As Integer, Response As Integer)

Const IS_DIRTY = 2169

' suppress system error message if form
' is closed while record is unsaved,
' NB: changes to current record will be lost
If DataErr = IS_DIRTY Then
Response = acDataErrContinue
End If

End Sub
''''module ends''''

If the user close the form while current changes have not been saved, then
those changes are not saved; if the user attempts to move to another record
while current changes have not been saved then they cannot so without saving
or undoing the changes first.

Ken Sheridan
Stafford, England
 

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