Form Save and Cancel Buttons

D

Diana Minks

I've created a Save and Cancel button on my form but they
are not working as they should. I want the save button to
save the data and close the form. I want the Cancel button
to NOT accept the entries and close the form. What do I
need to put in my Expression or do I need to do something
else.

Please help!
Diana Minks
Tech Writer, ALLTEL Communications
678-351-4282
 
A

Allen Browne

Private Sub cmdCancel_Click()
If Me.Dirty Then 'Undo any changes.
Me.Undo
End If
DoCmd.Close acForm, Me.Name
End Sub

Private Sub cmdSave_Click()
If Me.Dirty Then 'Save any changes
Me.Dirty = False
End If
DoCmd.Close acForm, Me.Name
End Sub
 
D

Diana Minks

My form has two buttons. One to Save the form and one to
Cancel out without saving anything.

I tried this with the Save button and now I get the
following error message.

Complie Error

Syntax Error
-----Original Message-----
Any button on an Access form is controlled by code (VBA -
Visual Basic for Applications). If you require a button
to do more than one action you need to update the code
behind the button.

For example if you create a button, using the wizard, that
saves a record the chances are Access will create this:

Private Sub Command3_Click()
On Error GoTo Err_Command3_Click


DoCmd.DoMenuItem acFormBar, acRecordsMenu,
acSaveRecord, , acMenuVer70

Exit_Command3_Click:
Exit Sub

Err_Command3_Click:
MsgBox Err.Description
Resume Exit_Command3_Click

End Sub

Don't be offput by all this this. In my example Command3
is just the name of the button.

In the form design view select your button and in the
Events tab of the properies dialog you should have {Event
Procedure] against the On Click property. Select this
property and 3 dots (elipsis) should appear to the right.
Click the elipsis and Access will open the code behind the
button.

After the line of code:
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, ,
acMenuVer70

enter a newline then then add:
DoCmd.Close

This extra line of code will close your form after the
record has been saved.



With regard to undoing a change add this line of code:
DoCmd.RunCommand acCmdUndo

before the Docmd.Close line of the close button on your
form code.

This subject can start to get involved, so I will leave
you at this pointm however contact me at
(e-mail address removed) for further assistance if
required.

Bish

-----Original Message-----
I've created a Save and Cancel button on my form but they
are not working as they should. I want the save button to
save the data and close the form. I want the Cancel button
to NOT accept the entries and close the form. What do I
need to put in my Expression or do I need to do something
else.

Please help!
Diana Minks
Tech Writer, ALLTEL Communications
678-351-4282
.
.
 
D

Diana Minks

Allen,

I'm not sure what you mean here. This is way over my head!

My save command is called "Save" and my cancel command is
called "Cancel." What does "if Me.Dirty" and "Me.Name"
mean?
 
A

Allen Browne

Okay, I've assumed you have two command buttons on your form.
I've named them cmdCancel and cmdSave.

1. Right-click the cmdCancel button, and choose Properties.

2. On the Event tab of the properties box, choose On Click.

3. Set this property to:
[Event Procedure]

4. Click the Build button (...) beside this. Access opens the code window.

5. Paste the code in between the "Private Sub ..." and "End Sub" lines.

6. Do the same for your cmdSave button.

7. Choose Compile from the Debug menu to ensure Access understands it.

To explain the code:
- Me is a reference to the form, a shorthand for Forms![MyForm]

- Dirty is true if the form has edits that have not been saved. Setting
Dirty to False forces the form to save the record.

- Undoing the form discards the edits. That's what you need to do for your
cancel button, so they don't get saved.
 
D

Diana Minks

Thanks! Worked like a charm!
-----Original Message-----
Okay, I've assumed you have two command buttons on your form.
I've named them cmdCancel and cmdSave.

1. Right-click the cmdCancel button, and choose Properties.

2. On the Event tab of the properties box, choose On Click.

3. Set this property to:
[Event Procedure]

4. Click the Build button (...) beside this. Access opens the code window.

5. Paste the code in between the "Private Sub ..." and "End Sub" lines.

6. Do the same for your cmdSave button.

7. Choose Compile from the Debug menu to ensure Access understands it.

To explain the code:
- Me is a reference to the form, a shorthand for Forms! [MyForm]

- Dirty is true if the form has edits that have not been saved. Setting
Dirty to False forces the form to save the record.

- Undoing the form discards the edits. That's what you need to do for your
cancel button, so they don't get saved.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to the newsgroup. (Email address has spurious "_SpamTrap")
Diana Minks said:
Allen,

I'm not sure what you mean here. This is way over my head!

My save command is called "Save" and my cancel command is
called "Cancel." What does "if Me.Dirty" and "Me.Name"
mean?
button
to


.
 
D

Diana Minks

Allenn, I take that back. The Save button doesn't work.
I'm now getting a runtime error 3314. FYI - There are
required fields in this database, if that makes a
difference. What else do I need to fix.

Diana
-----Original Message-----
Okay, I've assumed you have two command buttons on your form.
I've named them cmdCancel and cmdSave.

1. Right-click the cmdCancel button, and choose Properties.

2. On the Event tab of the properties box, choose On Click.

3. Set this property to:
[Event Procedure]

4. Click the Build button (...) beside this. Access opens the code window.

5. Paste the code in between the "Private Sub ..." and "End Sub" lines.

6. Do the same for your cmdSave button.

7. Choose Compile from the Debug menu to ensure Access understands it.

To explain the code:
- Me is a reference to the form, a shorthand for Forms! [MyForm]

- Dirty is true if the form has edits that have not been saved. Setting
Dirty to False forces the form to save the record.

- Undoing the form discards the edits. That's what you need to do for your
cancel button, so they don't get saved.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to the newsgroup. (Email address has spurious "_SpamTrap")
Diana Minks said:
Allen,

I'm not sure what you mean here. This is way over my head!

My save command is called "Save" and my cancel command is
called "Cancel." What does "if Me.Dirty" and "Me.Name"
mean?
button
to


.
 
A

Allen Browne

3314 says a required field can't be null. The message makes sense if you are
trying to save record, and the user has not provided a value for a required
field.

The solution will be to include error handling in your code to notify the
user. If you need help with how to provide error handling, see:
http://users.bigpond.net.au/abrowne1/ser-23a.html

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to the newsgroup. (Email address has spurious "_SpamTrap")

Diana Minks said:
Allenn, I take that back. The Save button doesn't work.
I'm now getting a runtime error 3314. FYI - There are
required fields in this database, if that makes a
difference. What else do I need to fix.

Diana
-----Original Message-----
Okay, I've assumed you have two command buttons on your form.
I've named them cmdCancel and cmdSave.

1. Right-click the cmdCancel button, and choose Properties.

2. On the Event tab of the properties box, choose On Click.

3. Set this property to:
[Event Procedure]

4. Click the Build button (...) beside this. Access opens the code window.

5. Paste the code in between the "Private Sub ..." and "End Sub" lines.

6. Do the same for your cmdSave button.

7. Choose Compile from the Debug menu to ensure Access understands it.

To explain the code:
- Me is a reference to the form, a shorthand for Forms! [MyForm]

- Dirty is true if the form has edits that have not been saved. Setting
Dirty to False forces the form to save the record.

- Undoing the form discards the edits. That's what you need to do for your
cancel button, so they don't get saved.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to the newsgroup. (Email address has spurious "_SpamTrap")
Diana Minks said:
Allen,

I'm not sure what you mean here. This is way over my head!

My save command is called "Save" and my cancel command is
called "Cancel." What does "if Me.Dirty" and "Me.Name"
mean?

-----Original Message-----
Private Sub cmdCancel_Click()
If Me.Dirty Then 'Undo any changes.
Me.Undo
End If
DoCmd.Close acForm, Me.Name
End Sub

Private Sub cmdSave_Click()
If Me.Dirty Then 'Save any changes
Me.Dirty = False
End If
DoCmd.Close acForm, Me.Name
End Sub

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to the newsgroup. (Email address has
spurious "_SpamTrap")

I've created a Save and Cancel button on my form but
they
are not working as they should. I want the save button
to
save the data and close the form. I want the Cancel
button
to NOT accept the entries and close the form. What do I
need to put in my Expression or do I need to do
something
else.

Please help!
Diana Minks
Tech Writer, ALLTEL Communications
678-351-4282


.
 

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