Exit Function vs Exit Sub

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am new to Functions and changing some Subs to Functions to clean up code.

Where I have an Exit Sub and want to quit the code at that point, I'm ok
with Subs. The code stops and I can set focus to a control.

However, if I substitute Exit Function at that same point, the code
continues. No stopping, no set focus to a control. Not what I want.

Is there another way to stop the code at that point? These are private
functions; I've not attempted to change an Exit Sub for a public function.
Can I assume the remedy is the same private/public?
 
Exit Function should perform exactly the same way as Exit Sub. Which you use
is determined strictly by whether it's in a Function or a Sub.

How are you using the functions? What makes you think focus should be
getting set?
 
Thanks, Douglas. I thought they should behave the same, but somehow the code
continues.

I am testing for missing/invalid input, when it is missing I message the
user, set focus to the control that's empty or invalid, then quit the routine
-- abandoning remaining code.

As I said this works in sub, but in a function I change to Exit Function.

Example of code in private function:

If MyControl = 0 Or isnull(MyControl.Value) Then
response = MsgBox(msg, style, title)
MyControl.SetFocus
Exit Function
End If

The only difference I see is Exit Function/Exit Sub. Anything you can think
of for me to look for?

--
Thanks for your help,
Chris


Douglas J. Steele said:
Exit Function should perform exactly the same way as Exit Sub. Which you use
is determined strictly by whether it's in a Function or a Sub.

How are you using the functions? What makes you think focus should be
getting set?
 
Where is the function? Is in a module associated with a form, or is it in a
stand-alone module?

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Chris said:
Thanks, Douglas. I thought they should behave the same, but somehow the
code
continues.

I am testing for missing/invalid input, when it is missing I message the
user, set focus to the control that's empty or invalid, then quit the
routine
-- abandoning remaining code.

As I said this works in sub, but in a function I change to Exit Function.

Example of code in private function:

If MyControl = 0 Or isnull(MyControl.Value) Then
response = MsgBox(msg, style, title)
MyControl.SetFocus
Exit Function
End If

The only difference I see is Exit Function/Exit Sub. Anything you can
think
of for me to look for?
 
I'm at a loss as to why the behaviour should be different.

What's the actual code? How are you invoking the function? I'll likely be
away for a couple of days, but someone else may be following this, and be
able to offer some insight.
 
Thanks for trying. I will infer it is something that isn't right in my code
somewhere. I'll revisit every character! At least now I know there isn't
magic to it.

Thanks again -- enjoy the time away.
 
I didn't see where a RESULT was being set... so what's the purpose of making it
a Function vs Sub?
 
I am new to Functions and changing some Subs to Functions to clean
up code.

Er, why? Do you need to use the sub as the value of a property
(instead of an Event Procedure, or do you need to use it in a macro?
Those are the only sreason I can think of to convert a sub to a
function.

A Function returns a value and that's why it's different from a Sub,
which only performs an action. If you are not changing your code to
return something and are not needing to use these subs in the two
contexts listed above, then I fail to see why you'd want to change
them from subs to functions.
 
I will infer it is something that isn't right in my code
somewhere. I'll revisit every character! At least now I know
there isn't magic to it.

Does it compile?
 
Back
Top