Cancel code not working (1 Viewer)

magster06

Registered User.
Local time
Today, 14:53
Joined
Sep 22, 2012
Messages
235
Hello all!

The following code was working, but I was experimenting with some different ideas and decided to go back to my original setup. Unfortunately, the code does not want to cooperate anymore.

When it would ask if I want to "Save" and I click "No", then it would clear the data and not save, but now it does not clear the data fields and it will Save the data anyways.

No matter what I try, it saves the data input when I click "No."

The form is used to add a new record.
Here is the code for the form and controls:

Code:
Option Compare Database
Option Explicit
Private blnSave As Boolean
 
Private Sub cmdCancel_Click()
    On Error GoTo cmdCancel_Click_Err
    On Error Resume Next
    DoCmd.RunCommand acCmdUndo
    If (MacroError <> 0) Then
        Beep
        MsgBox MacroError.Description, vbOKOnly, ""
    End If
 
cmdCancel_Click_Exit:
    Exit Sub
cmdCancel_Click_Err:
    MsgBox Error$
    Resume cmdCancel_Click_Exit
End Sub
Private Sub cmdExitAddNewEMS12_Click()
    On Error GoTo cmdExitAddNewEMS12_Click_Err
            DoCmd.Close , ""
            DoCmd.OpenForm "frmEMS22Alt"
cmdExitAddNewEMS12_Click_Exit:
    Exit Sub
cmdExitAddNewEMS12_Click_Err:
    MsgBox Error$
    Resume cmdExitAddNewEMS12_Click_Exit
End Sub
Private Sub cmdSaveEMS22_Click()
    blnSave = MsgBox("Are you sure you want to save this record?", vbQuestion + vbYesNo, "Save Confirmation")
    DoCmd.RunCommand acCmdSaveRecord
 
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer)
    If Not blnSave Then
        Cancel = True
        Me.Undo
    End If
End Sub

I am hoping someone can see what is askew (besides me).

Thanks ahead of time.
 
Last edited:

boerbende

Ben
Local time
Today, 20:53
Joined
Feb 10, 2013
Messages
339
blnSave = MsgBox("Are you sure you want to save this record?", vbQuestion + vbYesNo, "Save Confirmation")
DoCmd.RunCommand acCmdSaveRecord

Why don't you use an
if blnsave = vbyes then docmd...?
in your case it saves always, doesn't matter what you answer
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 11:53
Joined
Aug 30, 2003
Messages
36,134
Check what the numeric return values are from the message box and whether a Boolean variable is appropriate to store them (hint...no).
 

magster06

Registered User.
Local time
Today, 14:53
Joined
Sep 22, 2012
Messages
235
Thanks for the replies!

Check what the numeric return values are from the message box and whether a Boolean variable is appropriate to store them (hint...no).

I'll try a different approach.
 

magster06

Registered User.
Local time
Today, 14:53
Joined
Sep 22, 2012
Messages
235
Is the following code ok, or do I need some code in the BeforeUpdate Event?

Code:
Private Sub cmdSaveEMS22_Click()
    intAnswer = MsgBox("Are you sure you want to save this record?", vbQuestion + vbYesNo, "Save Confirmation")
    If intAnswer = vbYes Then
        DoCmd.RunCommand acCmdSaveRecord
    Else
        DoCmd.RunCommand acCmdUndo
    End If
 
End Sub

Everything seems to work as it should. I don't see anything being saved in the table when I click "No", but I just want to be sure.

Thanks
 
Last edited:

pbaldy

Wino Moderator
Staff member
Local time
Today, 11:53
Joined
Aug 30, 2003
Messages
36,134
That looks fine, as long as they click on your button. ;)

The before update event is typically used because it will fire either way.
 

Users who are viewing this thread

Top Bottom