PC Review


Reply
Thread Tools Rate Thread

Cancel = True "variable not defined"

 
 
0 1
Guest
Posts: n/a
 
      11th Feb 2011
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.
 
Reply With Quote
 
 
 
 
John W. Vinson
Guest
Posts: n/a
 
      11th Feb 2011
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
 
Reply With Quote
 
0 1
Guest
Posts: n/a
 
      11th Feb 2011
> 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.
 
Reply With Quote
 
Mike Painter
Guest
Posts: n/a
 
      11th Feb 2011
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


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"Variable not defined" error for form that does not initially exist... Alex Microsoft Excel Programming 1 17th Feb 2007 06:44 PM
why is it saying sheetcnt is "variable not defined" how to do a global variable to share over multiple functions in vba for excel? Daniel Microsoft Excel Worksheet Functions 1 9th Jul 2005 03:05 AM
why is it saying sheetcnt is "variable not defined" how to do a global variable to share over multiple functions in vba for excel? Daniel Microsoft Excel Discussion 1 9th Jul 2005 01:04 AM
Close workbook with "Cancel=TRUE" in the BeforeClose()" Wellie Microsoft Excel Programming 1 16th Oct 2004 09:46 PM
Cancel event "not in list" if before update is not true =?Utf-8?B?RXJpaw==?= Microsoft Access VBA Modules 5 26th Feb 2004 06:33 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:46 AM.