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

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