Re: Working with SUBs




"Rick Charnes" <rickxyz--nospam.zyxbadcharnes@xxxxxxxxxxxxxxx> wrote in message news:MPG.2148925e5a0696ce9899ad@xxxxxxxxxxxxxxxxxxxxxxx
Kinda new to writing subroutines within VBA. I didn't realize that all
the module-level variables declared in the body of the procedure are not
available to you in the SUB.

Before you start confusing yourself and everyone else here, let's get some terminology straight.

Module-level variables are declared *before* the first Sub or Function in the module. They are declared using the keyword Private rather than Dim. A module level variable can be used by any routine within the same module.

Global variables are also declared before the first Sub or Function of a module, but using the Public keyword. A global variable can be used by any routine in any module of the project.

Procedure-level variables are declared after the Sub or Function statement at the start of a procedure, and are only available within that procedure.

I guess I have to pass any that are needed
in the SUB, as arguments?

That is usually the best way.

Is there another technique?

Yes, you can use global or module level variables, but I would recommend being very sparing with their use. If you declare a variable locally and pass it as a parameter to another routine, you know that there is only one point at which the two routines interact. With a module-level or global variable, any routine can affect the operation of any other simply by changing the value of a common variable, and as there is no limitation as to how and where this can happen, bug-hunting can take on needle-in-a-haystack dimensions

You might be interested in this article which describes some additional techniques for writing maintainable code.

The art of defensive programming
http://www.word.mvps.org/FAQs/MacrosVBA/MaintainableCode.htm


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup

.



Relevant Pages

  • sundial program
    ... It designs sundials. ... DECLARE SUB KeyPause ... PRINT "Should screens be saved to disk, ...
    (comp.lang.basic.misc)
  • Re: OnTimer event-Form does not load:Here is the code
    ... global variables which are declared in another module. ... you might want to do declare and populate them ... >Private Sub Form_Timer ... > Dim strErrorMsg As String ...
    (microsoft.public.access.forms)
  • Re: Declarations variables, Dim, some guidance please
    ... I have a sub called zRM_Values which is called at the begining of larger ... it's kinda a good thing for me that other macros ... > By preference, I try to declare only ... > You didn't get a duplicate Dim error because one variable was declared ...
    (microsoft.public.excel.programming)
  • Re: Carry a varible to a new routine
    ... If the other routine is in the same userform module, you just declare it at ... Sub UserForm_Click ...
    (microsoft.public.excel.programming)
  • Re: Function versus Procedure
    ... David, we are in the realm of stylistic choices, so I hope you can respect that others may make choices for reasons other than the ones that make sense to you. ... There's also an inconsistency in the way VBA calls subs and functions, so if everything I create is a function, I don't have to remember whether it was a sub or a function in order to call it. ... it doesn't declare a return type, ... I've got forms with 300 controls on them where each control calls ...
    (microsoft.public.access.tablesdbdesign)