Cancel an event from a second subroutine...

T

ThriftyFinanceGirl

I have an onclick event that goes to a global sub routine. Within the
subroutine I need to cancel if some things are true. However, I can only get
it to cancel THAT subroutine, it still goes back to the Onclick event and
continues to run the rest of that event, which I don't want it to do. How do
I handle this?
 
K

Klatuu

By Global sub routine, I assume you mean a Public Function in a standard
module.
If so, it will have no Cancel argument. Your cancel has to be in the Event
routine. To do that, your Public function needs to return True if you want
to cancel and False if not. If would be something like this in your event:

Cancel = MyPublicFunction
 
D

Dale_Fye via AccessMonster.com

Change your global subroutine to a function, or pass it a variable with the
By Ref prefix

Private Sub cmd_Something_Click

Dim Cancel as boolean

Call yourSub(Cancel)
if Cancel = True then Exit Sub

'add other code here

End Sub

Public Sub yourSub(By Ref Cancel as boolean)

if SomeCondition then
Cancel = True
Exit Sub
Endif

'other code here

End Sub

HTH
Dale
 
J

JimBurke via AccessMonster.com

When you say cancel the click event, I'm assuming you mean you just don't
want the code executed under certain situations. On Click events don't have
cancel options as far as I know. Assuming that's what you want, here's what I
would do:

1) Change the global sub to a function. Declare the function as returning a
Boolean value, e.g. Public Function whatever() As Boolean. Set the value of
the Function to true in the situations where you want to cancel, Set it to
False otherwise. make sure you have all possible situations covered so that
the correct value will always be returned.

2) In your onclick event, at whatever point you may want to exit the event
sub, instead of calling the sub,. e.g. Call MySub, do this:

If MyFunction then
exit sub
End If

This invokes the function. If your 'cancel' conditions are met the function
returns True and you will exit the OnClick event sub, otherwise you will
continue on to the next statement. Give the Function a meaningful name.
 
B

BruceM

I always thought it a curious thing that the double click event can be
canceled, but the single click event cannot. That being said, you should be
able to add:
Exit Sub
after the subroutine runs if some condition is met or not met. Perhaps you
can pass a value from the subroutine back to the Click event:

If SubroutineName = False Then
Exit Sub
Else
' Do something else
End If

If the subroutine changes does something like change a value in the form's
record source you could check for that instead of the subroutine value.
Without details it is difficult to offer anything specific. If you post the
subroutine code (or a shortened version of it) it may be possible to suggest
something more specific.
 
T

ThriftyFinanceGirl

Okay, so I'm running my function and setting it to either true or false (as a
variant right?) but when I send it back to the EventProcedure (on click) as a
false, "Cancel = MyFunction" it DOESN"T cancel... it just keeps running that
Onclick procedure.
 
D

Dale_Fye via AccessMonster.com

Instead of:

Cancel = MyFunction

try:

If myFunction = True then Exit Sub

Dale
Okay, so I'm running my function and setting it to either true or false (as a
variant right?) but when I send it back to the EventProcedure (on click) as a
false, "Cancel = MyFunction" it DOESN"T cancel... it just keeps running that
Onclick procedure.
By Global sub routine, I assume you mean a Public Function in a standard
module.
[quoted text clipped - 9 lines]
 
T

ThriftyFinanceGirl

Thanks Guys! I got it with a little combo of what a couple of you said. And
I agree that it is weird that the double-click event has a cancle and the
click event does not. :)
 
J

John Spencer

Should not that be

IF MyFunction() = True then Exit Sub

Or even

IF MyFunction() then Exit Sub

John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County

Dale_Fye via AccessMonster.com said:
Instead of:

Cancel = MyFunction

try:

If myFunction = True then Exit Sub

Dale
Okay, so I'm running my function and setting it to either true or false (as a
variant right?) but when I send it back to the EventProcedure (on click) as a
false, "Cancel = MyFunction" it DOESN"T cancel... it just keeps running that
Onclick procedure.
By Global sub routine, I assume you mean a Public Function in a standard
module.
[quoted text clipped - 9 lines]
continues to run the rest of that event, which I don't want it to do. How do
I handle this?
 
D

Dale_Fye via AccessMonster.com

John,

What is your point?

Although you can add the () to the end of the function call, it isn't really
necessary.

Dale


John said:
Should not that be

IF MyFunction() = True then Exit Sub

Or even

IF MyFunction() then Exit Sub

John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County
Instead of:
[quoted text clipped - 16 lines]
 
J

John Spencer

Dale,

Hmm. I always thought it was. Guess I will have to test that and see why I
thought it was necessary to have the () at the end to actually get something
to return.

I guess I learned something.

John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County

Dale_Fye via AccessMonster.com said:
John,

What is your point?

Although you can add the () to the end of the function call, it isn't really
necessary.

Dale


John said:
Should not that be

IF MyFunction() = True then Exit Sub

Or even

IF MyFunction() then Exit Sub

John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County
Instead of:
[quoted text clipped - 16 lines]
continues to run the rest of that event, which I don't want it to do. How do
I handle this?
 
D

Dale_Fye via AccessMonster.com

John,

May be a hold-over from an earlier version.

Dale

John said:
Dale,

Hmm. I always thought it was. Guess I will have to test that and see why I
thought it was necessary to have the () at the end to actually get something
to return.

I guess I learned something.

John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County
[quoted text clipped - 23 lines]
 
V

vanderghast

It is still the case, but somehow 'fuzzy'.


As example, have a public function in a standard module:

-------------------------------
Public Function MyPopUp()

MsgBox "hello"

MyPopUp = True
End Function
------------------------------


Then, in the Debug Immediate Window, you can try:


MyPopUp

it works and return nothing as a SUBROUTINE does.

MyPopUp( )

a runtime ERROR occurs since it is a FUNCTION call and nothing is done
to capture the result.

Call MyPopUp( )

returns nothing, but does not produce an error either. The CALL make it
clear it is a SUBROUTINE behavior that you want, and is equivalent to the
first case.


? MyPopup

and

? MyPopup()


returns true, both working as FUNCTION.


You can also play a little with MsgBox "Hello". Add parenthesis:

MsgBox("hello")

and VBA will re-write the line as:

MsgBox ("hello")


to indicate you that MsgBox is used as SUBROUTINE.


If you preceed it with Call. VBA re-write the line by sticking the ( to the
procedure name:

Call MsgBox( "hello" )

and it is used as subroutine.

Finally, you can use:

Dim i As Long
i = MsgBox("hello")

and observe that here again, VBA sticks the ( to the procedure name, and
behave as a FUNCTION rather than a SUBROUTINE.


So, in some context, the ( ) or their absence may make a difference, and can
even generate a runtime error! So, they are important.



Vanderghast, Access MVP


Dale_Fye via AccessMonster.com said:
John,

May be a hold-over from an earlier version.

Dale

John said:
Dale,

Hmm. I always thought it was. Guess I will have to test that and see why
I
thought it was necessary to have the () at the end to actually get
something
to return.

I guess I learned something.

John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County
[quoted text clipped - 23 lines]
continues to run the rest of that event, which I don't want it to
do. How do
I handle this?
 
J

JimBurke via AccessMonster.com

The () is needed if you have a macro that uses RunCode to run a function -
that may be what you're thinking of.

John said:
Dale,

Hmm. I always thought it was. Guess I will have to test that and see why I
thought it was necessary to have the () at the end to actually get something
to return.

I guess I learned something.

John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County
[quoted text clipped - 23 lines]
 

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