Function vs. Subroutine

P

PeterM

Is it better for performance to have a set of code that is called from all
forms be created in a module as a function or subroutine? Every one of our
100+ forms calls this code to set the forecolor, backcolor, etc appearance
for each form.

Thanks in advance for your help!
 
D

Douglas J Steele

I don't think there'll be a noticable difference between the two.

If you create a function (even if you don't have the function return a
value), you can set the form's Open property to the name of the function,
rather than having to have a Sub Form_Open for the form. On the other hand,
I never do that...
 
J

John Vinson

Is it better for performance to have a set of code that is called from all
forms be created in a module as a function or subroutine? Every one of our
100+ forms calls this code to set the forecolor, backcolor, etc appearance
for each form.

Thanks in advance for your help!

Performance of a Sub or a Function should make very little if any
difference. However, a Function is handy in one way - you don't need
to actually create an [Event Procedure] in each form to call it.
Instead simply put

=MyFunction([Form])

on the form's Open (or other appropriate) event; within the function,
use

Public Function MyFunction(frm as Form)

to pass the identity of the form as an argument.

Obviously if you already have an Event Procedure, you can simply call
the function within that procedure.

John W. Vinson[MVP]
 
R

Ron Hinds

PeterM said:
Is it better for performance to have a set of code that is called from all
forms be created in a module as a function or subroutine? Every one of our
100+ forms calls this code to set the forecolor, backcolor, etc appearance
for each form.

Thanks in advance for your help!

More important than performance in this case is maintainability. If there is
a bug in the function, right now you have to make the fix (*if* you remember
to ;-) in 100+ forms, vs. ONCE if it is modularized!
 
Top