On Thu, 10 Feb 2011 17:36:02 -0800 (PST), 0 1 <(E-Mail Removed)> wrote:
>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/For...-US/accessdev/
http://social.answers.microsoft.com/.../en-US/addbuz/
and see also
http://www.utteraccess.com