function vs private sub

1jet

Registered User.
Local time
Tomorrow, 03:49
Joined
Sep 15, 2008
Messages
117
Hi all, smee again.

I've picked up from this forum some naming conventions for form controls.
Also some habits with data types behind the scenes in VBA.

I'd like to know more about how you all decide when to have functions as a "general" function (hopefully you know what i mean...) or as an "event procedure" (private sub).

Cheers all
 
It's all about scope for me .. what other function or variable does my event need to see to do it's job effectively? I also think in terms of reusability. How many times will I use this scrap of code in this db?

I use a private subroutine when I just need a little something that only pertains to that event and doesn't apply anywhere else.

I use a form function when I am doing something specifically for that form and that form only (e.g., form validation) because it isn't applicable any where else. In this manner events for that form can have access to the form function and use it without abandon.

I use a module function when I am doing something database wide meaning it has application all over the database.

Lets say I have a little sub that does something and I notice that I am using that little something on all of my forms. I will move that little something into a module and then when I call the sub, I point it to the module. This just makes the code a little cleaner and saves me time because I don't have to rewrite and rewrite - just write once and use a gabillion times.

Sometimes when I have alot of subs and I am thinking, 'Wow, those are really similar!' I will spend time making a module that uses generic variables to handle all of the similar aspects between the little subs. In my little pretend made up world, I call that code normalization.:rolleyes:

Last, if something is going to take 100 lines to write, I will shove it into a module just to get it out of my form code because I don't wanna be scrolling through it trying to scan through all of the 6-line events.

-dK
 
I think you may be a little mixed up about the differences/similarities:
1. a sub is a piece of code that does not return a value, a function returns a value.
2. private means that only other code within the same module can call the code, public means that any other code anywhere else can call the code
3. an event procedure is code that VB calls when windows receives an event and passes it to VB. You can call a sub that is not an event procedure

So you can have:
Private Sub NonEventHandlerSub()
Private Function FunctionThatReturnsAValue() As Long
Public Sub IWantToCallThisFromAnywhere()
Public Function IWantThisToReturnAValueFromAnywhere() As String

With that said, I use a public sub/function when I want to share the code between modules or even outside of the program/database.

I use a function when I want to return a single value and want to make it easier to read the code calling the function.

I use a subroutine (sub) when I don't want to return a value or need to return more than one value, or when forced to use one because I'm programming an event handler.
 

Users who are viewing this thread

Back
Top Bottom