Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with saving a form

Status
Not open for further replies.

gianina

Technical User
Jul 4, 2006
56
CA
Hi All,

Here's my application:
I created a form through which the user will enter data into the table. Once the user entered data in some of the fields on the form and eventually decides NOT to save any changes and closes the form --> how can the user have that option ???
I mean, when data is entered into a field (from the form) it is automatically saved without asking the user if wants to save the changes or not.

My question is: how can I "force" Access to NOT save any changes unless the user decides otherwise (by promting for save or not) ???

Thanks.
 
Place a command button on your form then the Wizard will pop up. Choose Records Operations-> UnDo Record and it's done!

The Missinglinq

There's ALWAYS more than one way to skin a cat!
 
Missinglinq,

Can I make that same button to also close the form ?

Thanks.
 
You can also put this code in before update event of your form.
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim iAns As Integer
iAns = MsgBox("Do you want to save " , vbYesNo)
If iAns = 7 Then

Me.Undo

End If
End Sub
Remember iAns=7 means 'NO'in msgbox
 
Code:
If iAns = MsgBox("Do you want to save?", vbYesNo) = vbNo Then


Randy
 
Sure! In Design View, select your command button, right click and select Properties. Select Events. Go down to On Click. You should see [Event Proceedure] in the box to the right of On Click. With your mouse click to the right of the box (just outside the box) and the ellipsis (...) should appear. Now click on the ellipsis. You should be taken to the VBA code window, hopefully to the code behind your command button. It should look something like this:

Private Sub YourCommandButton_Click()
On Error GoTo Err_YourCommandButton_Click

DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, , acMenuVer70

Exit_YourCommandButton_Click:
Exit Sub

Err_YourCommandButton_Click:
MsgBox Err.Description
Resume Exit_Command588_Click

End Sub

If you named your button your chosen name will be in place of the YourCommandButton, else it'll be whatever name Access assigned (usually Command folowed by a number like Command123)

Under the line
DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, , acMenuVer70

enter this:

DoCmd.Close acForm, "YourFormName", acSaveNo

replacing YourFormName with the actual name of your form.

As my signature says, ther's always more than one way to skin a cat! The other suggestions will work, too!

The Missinglinq

There's ALWAYS more than one way to skin a cat!
 
Missinglinq,

I entered your code and it works.
However, this works only when a change occurred in one of the fields. If no changes have been made and press the button the following error message pops-up:

"The command or action "Undo" isn't avaialble now"

Through this button I want to be able to close the form and NOT save any changes made.

Any ideas how to do that ?

Million thanks.
 
Let me correct my last sentence:

Through this button I want to close the form without saving it, even if changes were not made.

Thanks.
 
Check the Dirty property of the form.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
PHV,

What exactly should I look for ?

Thanks.
 
A starting point:
If Me.Dirty Then
Me.Undo
End If

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
PHV,

Can you be more speicific, please ?

Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top