My Cancel Button Is Not Working

  • Thread starter Thread starter Steve Norris via AccessMonster.com
  • Start date Start date
S

Steve Norris via AccessMonster.com

My cancel button is not working. It's opening my add form even if I click
on the cancel button. Below is my code. Thanks in advance for any
suggestions......

Private Sub AddNewRcd_Click()
On Error GoTo Err_AddNewRcd_Click
If MsgBox("Are you sure you want to ADD A New Record? ", vbOKCancel) <>
vbOK Then
Cancel = True
End If
DoCmd.OpenForm "frmProfileAdd2"
DoCmd.GoToRecord , , acNewRec


Exit_AddNewRcd_Click:
Exit Sub

Err_AddNewRcd_Click:
MsgBox "ADD Canceled" 'Err.DescriptionErr.Description
Resume Exit_AddNewRcd_Click
End Sub
 
Ok, I got it to work. Here's the revised code.....

Private Sub AddNewRcd_Click()
On Error GoTo Err_AddNewRcd_Click
If MsgBox("Are you sure you want to ADD A New Record? ", vbOKCancel) <>
vbOK Then
Cancel = True
'Me.Undo
DoCmd.CancelEvent
MsgBox "ADD Record Canceled"
Else
DoCmd.OpenForm "frmProfileAdd2"
DoCmd.GoToRecord , , acNewRec

End If

Exit_AddNewRcd_Click:
Exit Sub

Err_AddNewRcd_Click:
MsgBox "ADD Canceled" 'Err.DescriptionErr.Description
Resume Exit_AddNewRcd_Click
End Sub
 
Steve Norris via AccessMonster.com said:
Ok, I got it to work. Here's the revised code.....

Private Sub AddNewRcd_Click()
On Error GoTo Err_AddNewRcd_Click
If MsgBox("Are you sure you want to ADD A New Record? ", vbOKCancel)
<> vbOK Then
Cancel = True
'Me.Undo
DoCmd.CancelEvent
MsgBox "ADD Record Canceled"
Else
DoCmd.OpenForm "frmProfileAdd2"
DoCmd.GoToRecord , , acNewRec

End If

Exit_AddNewRcd_Click:
Exit Sub

Err_AddNewRcd_Click:
MsgBox "ADD Canceled" 'Err.DescriptionErr.Description
Resume Exit_AddNewRcd_Click
End Sub

No need for *both*
Cancel = True
and

DoCmd.CancelEvent

Delete the "DoCmd.CancelEvent" line, since it's not doing anything.
 
Steve Norris via AccessMonster.com said:
My cancel button is not working. It's opening my add form even if I click
on the cancel button. Below is my code. Thanks in advance for any
suggestions......

Private Sub AddNewRcd_Click()
On Error GoTo Err_AddNewRcd_Click
If MsgBox("Are you sure you want to ADD A New Record? ", vbOKCancel) <>
vbOK Then
Cancel = True
End If
DoCmd.OpenForm "frmProfileAdd2"
DoCmd.GoToRecord , , acNewRec


Exit_AddNewRcd_Click:
Exit Sub

Err_AddNewRcd_Click:
MsgBox "ADD Canceled" 'Err.DescriptionErr.Description
Resume Exit_AddNewRcd_Click
End Sub


I cannot see where Cancel comes from. Where is it defined? While looking
at your code, select Compile from the Debug menu. Does it compile? It
should not if you use 'Option Explicit' which forces you to declare
variables. If you don't, then you could write whatever you like Cancel=True
or Banana=True or Silly=True. They all do the same thing: nothing.
If you just need a quick fix, then simply changing the line
Cancel=True
to
Exit Sub
would solve this immediate problem.
 
Dirk Goldgar said:
No need for *both*


Delete the "DoCmd.CancelEvent" line, since it's not doing anything.

Isn't it the Cancel = True that he should delete? It's not like this is a
BeforeUpdate event, where setting Cancel = True will undo the update. (For
that matter, I have to assume that he's either declared Cancel as a
module-level variable, or else he's running without Option Explicit turned
on, since Cancel isn't declared anywhere in the module)
 
Douglas J. Steele said:
Isn't it the Cancel = True that he should delete? It's not like this
is a BeforeUpdate event, where setting Cancel = True will undo the
update. (For that matter, I have to assume that he's either declared
Cancel as a module-level variable, or else he's running without
Option Explicit turned on, since Cancel isn't declared anywhere in
the module)

Duh. You're right, of course. Probably neither of those "cancel"
statements accomplishes anything.
 
You are both correct. I removed both cancels from my routine and it still
works.

Thanks a bunch for a better understanding of the option explicit too!

Steve
 
Back
Top