cancelling an unsaved change in VB code

P

Paul Ponzelli

I've got a command label on a form captioned "Return to Main Menu."

In the click event of that label, I check to see if an unsaved change has
been made to the data, and if so, I ask the user whether to save or abandon
the changes. The portion that saves the changes works fine, but I can't
seem to get the code to cancel the change if the user doesn't want to save
them.

Here's the code I'm using:

If Me.Dirty Then
If MsgBox("Do you want to save the change(s) you made to this
application?" & vbCr & vbCr & _
"Click 'Yes' to save your changes, 'No' to close without saving.", _
vbYesNo, "Save Changes?") = vbYes Then
'Do this if user clicks "Yes" to save the record.
DoCmd.RunCommand acCmdSaveRecord
Me!cmdSaveRecord.Visible = False
Else
'Do this if user clicks "No" to cancel the changes.
SendKeys "{esc}{esc}"
End If
End If

When this code executes and the user clicks "No," the record saves anyway.

Any suggestions on how to modify this code so that the changes are cancelled
when the user clicks "No?"

Thanks in advance,

Paul
 
M

Marshall Barton

Paul said:
I've got a command label on a form captioned "Return to Main Menu."

In the click event of that label, I check to see if an unsaved change has
been made to the data, and if so, I ask the user whether to save or abandon
the changes. The portion that saves the changes works fine, but I can't
seem to get the code to cancel the change if the user doesn't want to save
them.

Here's the code I'm using:

If Me.Dirty Then
If MsgBox("Do you want to save the change(s) you made to this
application?" & vbCr & vbCr & _
"Click 'Yes' to save your changes, 'No' to close without saving.", _
vbYesNo, "Save Changes?") = vbYes Then
'Do this if user clicks "Yes" to save the record.
DoCmd.RunCommand acCmdSaveRecord
Me!cmdSaveRecord.Visible = False
Else
'Do this if user clicks "No" to cancel the changes.
SendKeys "{esc}{esc}"
End If
End If

When this code executes and the user clicks "No," the record saves anyway.

Any suggestions on how to modify this code so that the changes are cancelled
when the user clicks "No?"


Your logic appears to be ok, but SendKeys is notoriously
flawed. You should also consider us alternatives to DoCmd
when they exist. See if this code makes a difference:


If Me.Dirty Then
If MsgBox(. . .) = vbYes Then
'Do this if user clicks "Yes" to save the record.
Me.Dirty = False
Me!cmdSaveRecord.Visible = False
Else
'Do this if user clicks "No" to cancel the changes.
Me.Undo
End If
End If
 
P

Paul Ponzelli

Me.Dirty = False
and
Me.Undo

I've wrestled with SendKeys on a number of occasions before, and you've
given me a much better way to handle it.

Thank you, gentlemen.

Paul
 
J

John Vinson

Me.Dirty = False
and
Me.Undo

I've wrestled with SendKeys on a number of occasions before, and you've
given me a much better way to handle it.

Thank you, gentlemen.

Paul

You're welcome - glad to have been of assistance!

John W. Vinson[MVP]
 

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