My Cancel Button Is Not Working

  • Thread starter Steve Norris via AccessMonster.com
  • 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
 
S

Steve Norris via AccessMonster.com

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
 
D

Dirk Goldgar

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.
 
J

Justin Hoffman

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.
 
D

Douglas J. Steele

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)
 
D

Dirk Goldgar

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.
 
S

Steve Norris via AccessMonster.com

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
 

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