Cancel = True "variable not defined"

0

0 1

I have this code behind a label called "Cancel":

Private Sub lblCancel_Click()
If Me.Dirty = True Then
If MsgBox("This will undo all of the changes you made to this
record. " & _
Chr(13) & Chr(13) & "Continue?", vbYesNo + vbQuestion, "Undo
Changes") = vbNo Then
Cancel = True
Else
Me.Undo
End If
End If
End Sub

It works fine. But I'd like to reuse this for several forms, so I
tried to move it to a Global Module and call it from each form with
"CancelEventRecord." As in:

Public Sub CancelEventRecord()

Dim MyForm As Form
Set MyForm = Screen.ActiveForm

If MyForm.Dirty = True Then
If MsgBox("This will undo all of the changes you made to this
record. " & _
Chr(13) & Chr(13) & "Continue?", vbYesNo + vbQuestion, "Undo
Changes") = vbNo Then
Cancel = True
Else
MyForm.Undo
End If
End If

But this gives me a "variable not defined error," and highlights the
Cancel = True line. Any suggestions?

Thank you.
 
J

John W. Vinson

I have this code behind a label called "Cancel":

Private Sub lblCancel_Click()
If Me.Dirty = True Then
If MsgBox("This will undo all of the changes you made to this
record. " & _
Chr(13) & Chr(13) & "Continue?", vbYesNo + vbQuestion, "Undo
Changes") = vbNo Then
Cancel = True
Else
Me.Undo
End If
End If
End Sub

It works fine. But I'd like to reuse this for several forms, so I
tried to move it to a Global Module and call it from each form with
"CancelEventRecord." As in:

Public Sub CancelEventRecord()

Dim MyForm As Form
Set MyForm = Screen.ActiveForm

If MyForm.Dirty = True Then
If MsgBox("This will undo all of the changes you made to this
record. " & _
Chr(13) & Chr(13) & "Continue?", vbYesNo + vbQuestion, "Undo
Changes") = vbNo Then
Cancel = True
Else
MyForm.Undo
End If
End If

But this gives me a "variable not defined error," and highlights the
Cancel = True line. Any suggestions?

Thank you.

Well, you don't have a Dim statement for Cancel, nor is it an argument to the
function. The message is exactly correct - there is no definition for Cancel.
The label's Click event would have the same problem, unless there is a bound
control on the form (very badly) named Cancel.

Since you're explicitly doing an Undo on the form object. there really is no
need for a variable named Cancel, I think you could just omit the line
Cancel=True. It may have made sense in a context of a form's BeforeUpdate
event (which DOES have a Cancel argument) but it's not needed here; if the
user doesn't accept the Continue offer, just do nothing and exit the function.

Note also that you can make this a Function instead of a Sub, and put

=CancelEventRecord()

in place of [Event Procedure] in the label's Click event, or any other
appropriate Event property box.

--

John W. Vinson [MVP]
Microsoft's replacements for these newsgroups:
http://social.msdn.microsoft.com/Forums/en-US/accessdev/
http://social.answers.microsoft.com/Forums/en-US/addbuz/
and see also http://www.utteraccess.com
 
0

0 1

Since you're explicitly doing an Undo on the form object. there really is no
need for a variable named Cancel, I think you could just omit the line
Cancel=True.

Got it. That did it. And thanks for the tip about making it a
function.
 
M

Mike Painter

0 1 wrote:

I believe in the "teach a man to fish" concept, maybe because I'm lazy.

If the user finds out that escape will do what you are trying to do in code
it can be applied universally and saves a lot of time coding.

Same for deleting a record, adding a new one and record navagation. About
the only button I do use is a close button
 

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