Whats the Difference between a Sub and a Function (1 Viewer)

Cowboy_BeBa

Registered User.
Local time
Today, 19:14
Joined
Nov 30, 2010
Messages
188
So im mostly self taught when it comes to VBA but i do have some programming experience

Usually when im coding a form ill define all my functions in Private Sub's (i know i can probably set them to Public, but ive never really needed em to be accessed by other forms (i think thats what the diff is between those but ive never really tested it)

And in Modules i usually use Functions and have all my code in there

Not sure why i do this, i guess i was using private subs, then when i started coding modules i mustve seen some code with Function in it on another module (when i was looking for examples) and just started using em

They both seem to work the same, ive never tried creating a function in a forms vba or a sub in a modules vba, but im assuming theyll work (i could be wrong), so it leads to the question, whats the difference between em?
 

Frothingslosh

Premier Pale Stale Ale
Local time
Today, 07:14
Joined
Oct 17, 2012
Messages
3,276
Functions return values, Subs don't. (If you don't specify a data type when declaring a function, the function returns a Variant.)

Functions can often be run as subs if you don't care about the return, but the reverse isn't true without ByRef games.
 

Cowboy_BeBa

Registered User.
Local time
Today, 19:14
Joined
Nov 30, 2010
Messages
188
Thanks Froth, that makes sense to me
 

Frothingslosh

Premier Pale Stale Ale
Local time
Today, 07:14
Joined
Oct 17, 2012
Messages
3,276
Oh, as to Private vs Public.

As a rule, anything declared as Private is visible only within the module where it is declared, while anything declared Public is available throughout the project.

The catch is with classes - public procedures inside class objects become visible as METHODS and are attached to that class, as opposed to public procedures inside, say, a regular module. This is important because forms and reports are actually handled by Access as class objects, meaning that anything you declare as public in a form's attached module is actually still attached to that form as a method. While you can, in theory, call the method by referencing the form and method (Call Forms!Switchboard.Login), that only works if the form is loaded and is generally a pain to keep straight.

Because of that, if you find yourself creating a function or sub that will be needed by multiple forms, you're going to be best off putting it in a module rather than making it public from one of the forms.
 

Cowboy_BeBa

Registered User.
Local time
Today, 19:14
Joined
Nov 30, 2010
Messages
188
actually, i think that could be the source of a problem im having now
I posted it on another thread
http://www.access-programmers.co.uk/forums/showthread.php?p=1499176#post1499176

Basically ive got a string (uStr), declared using "Public uStr as String"

its declared at the top of a module
but its only set within a function

the function is called in a login form, once logged in the user redirects to a menu form
at form load i need the menu form to read uStr, which i thought would be a global variable, however the menu form cannot read it, im not sure why

should i try declaring the function as public?
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 21:14
Joined
Jan 20, 2009
Messages
12,852
Subs and Functions are so closely related that treating one as the other can virtually change them into the other.

Read my posts here and follow the link to where I posted further o the subject at another site.
 

Cowboy_BeBa

Registered User.
Local time
Today, 19:14
Joined
Nov 30, 2010
Messages
188
Thanks Galaxiom, ill check that out now

Meanwhile im happy to report that i managed to solve the problem i had on the other thread by using what ive learned in this one, Thanks for that extra info Frothingslosh, really helped me out :)
 

Frothingslosh

Premier Pale Stale Ale
Local time
Today, 07:14
Joined
Oct 17, 2012
Messages
3,276
Huh, wasn't aware that enclosing a single parameter in parentheses forces a function to pass it ByVal. That's good to know - a few of my procedures use the aforementioned ByRef games to manipulate the passed parameters (typically some sort of validation summary when there are too many checks to make bitwise comparisons feasible for me).
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 21:14
Joined
Jan 20, 2009
Messages
12,852
Huh, wasn't aware that enclosing a single parameter in parentheses forces a function to pass it ByVal.

Read more closely. The situation is subtle and quite complex.

In a function with a single parameter, used as a function (ie retrieving the return value) the parentheses are part of the function. For Subs or Functions called as Subs (ie not returning the result) the parentheses force the parameter to be evaluated.

There are also situations where behaviour differs depending on whether the reference to an object in the parameter uses the bang or dot operator.
 

Users who are viewing this thread

Top Bottom