Results 1 to 3 of 3
  1. #1
    Lounger
    Join Date
    Aug 2001
    Location
    Toronto, Ontario, Canada
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    public functions - quick question (Access 97)

    Just scanning through this bulletin board and picking up tips and stuff to help me. On the issue of Global functions and variables : is there a recommended limit to the amount of stuff you hold globally versus privately (attached to specific forms for example). In other words - is it considered better practice to have as much stuff globally as possible?

    One more quickie - I understand there is some importance in clearing global variables once you have finished with them. Where is it best to do this? At the end of the function that calls it or within the global function itself.

    If this question makes sense I may finally be making headway in understanding stuff. If it doesn't and you read it and think "what the heck is she on?" then I may have to lock myself in my office for the weekend with yet another Access book. TIA for any guidance.

    Thanks for any tips or comments.

  2. #2
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: public functions - quick question (Access 97)

    Don't confuse functions and variables in this question because the differences are significant.

    Functions and subs are made available to any object that needs them simply by putting them in a standard module and declaring them as Public. The advantage to writing your code this way is that you can call it from as many places as you need to instead of writing the same code over and over as private routines in various forms, etc. This is referred to a reusability and is generally encouraged. It doesn't apply to class modules, but they're something you probably don't need to worry about yet.

    Global variables are variables declared Public in a standard module. These take up resources whether or not you ever use them and tend to reset themselves when your code hits an unhandled error, so they have to be used with caution. Plus, they can be reset by other bits of code with unexpected consequences.

    The rule of thumb is that scope (where the thing can be used) should be as narrow as possible. So if you need a variable to hold an accumulated value in a report, you would declare the variable at the module level in the report. That way various routines in the report's module could address the same variable, adding or subtracting from its value as appropriate. On the other hand, if you just need a variable to hold a temporary value, you declare it at the local level (within a sub or function).

    There are only 3 levels of scope you'll deal with in an Access database: local, modular and global. Variables declared inside a routine are local and are destroyed (unless you declare them using the Static keyword) when you exit the routine. The exception to this is object variables (those you populate using the Set keyword), which can persist if you don't specifically close and destroy them at the end of the routine.

    Variables declared as private in the declarations section of any module are only availabe to the routines within that module. Variables declared as public in a standard module can be used anywhere within the application, although they get stepped on easily. Variables declared as public in a class module (including forms and reports) can be seen and even changed from other routines, but they aren't really variables, they're properties of the class where they were declared.

    Every object variable should be set to nothing when you're through with it. Other variables go out of scope automatically, but you can't count on that with object variables. If you leave an undestroyed object variable hanging around in your code, you can get surprising results ... like trying to exit the application and having Access minimize to the taskbar instead.
    Charlotte

  3. #3
    Lounger
    Join Date
    Aug 2001
    Location
    Toronto, Ontario, Canada
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: public functions - quick question (Access 97)

    oofff! <img src=/S/dizzy.gif border=0 alt=dizzy width=15 height=15> right then - where's that book store?

    thanks charlotte for taking time to put all down in black and white for me. i've taken some decent strides on my learning curve the past few weeks and it's been fun. i'm looking forward to learning much more in the coming months and advice and web-sites like this are invaluable. cheers.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •