Create a Cancel Button

  • Thread starter Thread starter MArend
  • Start date Start date
M

MArend

I've got a Cancel button I created on a detail form. If
there were no changes, an error pops up stating "The
command or action Undo is not available at this time".

The wizard for creating command buttons doesn't offer a
Cancel. I want it to cancel any changes on the screen
and close the form. So to create it, I copied the code
from an Undo and the Close buttons.

Do I have to check for any changes before issuing the
Undo? Is there a better way to cancel changes and close
a form? I've looked at runcommand, but don't see anything
better.

Thanks for any help.

Mike A.
 
Yes, you do need to check for changes. Try:

If Me.Dirty Then DoCmd.DoMenuItem acFormBar, acEditMenu,
acUndo, , acMenuVer70
DoCmd.Close

A "better" way, depending on yor philosophy, is to use an
unbound form - this gives you much more control and
safety. The downside of this approach is that you have to
write the changes in a record to the table with code.
However, if your control names are exactly the same as
your field names, this code will save the record:

Dim newRecord As Object
Dim ctl As Control

Set newRecord = CurrentDb.OpenRecordset("your table") '
Set reference to the table
newRecord.AddNew '
add a new record

For Each ctl In Me.Controls ' cycle thru all form
fields and put them in the table
If ctl.ControlType = acTextBox Or _
ctl.ControlType = acComboBox Then 'only do text-
and comboboxes.
newRecord.Fields(ctl.Name) =
ctl.Value ' use the ctl name to refer to the
table fields.
End If
Next ctl

newRecord.Update
newRecord.Close
Set newRecord = Nothing

Hope this helps!
 
I've got a Cancel button I created on a detail form. If
there were no changes, an error pops up stating "The
command or action Undo is not available at this time".

The wizard for creating command buttons doesn't offer a
Cancel. I want it to cancel any changes on the screen
and close the form. So to create it, I copied the code
from an Undo and the Close buttons.

Do I have to check for any changes before issuing the
Undo? Is there a better way to cancel changes and close
a form? I've looked at runcommand, but don't see anything
better.

Try checking the "Dirty" property, first:

'***
'Undo if record has unsaved data
If Me.Dirty Then
Me.Undo
End If
'Close the form
DoCmd.Close acForm, Me.Name
'***
 
Scott, This is great.

I have this form bounded. I meant it for an update, the
user gets to it by double-clicking the list form. But I'll
need to have it Add too. In fact they'll probably be
mostly adding records, only using the update rarely.

Is there somewhere with more examples of this type of data
manipulation with vba code? All of my forms have been
bound (except a menu). And I feel limited in what I know
how to do.

Thanks!

MikeA.
(marend at ford.com)
 
Sadly, I don't know of a good source for Access code. I
have only one MS Access book, which is great: "Access
Cookbook" by Ken Getz, Paul Litwin, Andy Baron. It
doesn't attempt to give a big picture view, but rather
takes specific problems and solves them. I'm always
learning something from it.

I've found that as I understand the Access object model
better (and object-oriented programming better), I write
much better code. You can find the model at
http://msdn.microsoft.com/library/default.asp?
url=/library/en-us/modcore/html/deovrMicrosoftAccess.asp.
When I first looked at the Outlook object model, it made
little sense. But the more I worked with it, the better I
coded.

- Scott
 
Back
Top