How to create a global variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ncsthbell
    New Member
    • May 2007
    • 167

    #1

    How to create a global variable

    At a form level I believe I can create a variable as"
    Public v_name as string and this variable would be available to all events on the form. (I think!)

    I would like to create a 'global' variable that would be available to every form in my access database. How can I do this?

    Thanks
  • Stewart Ross
    Recognized Expert Moderator Specialist
    • Feb 2008
    • 2545

    #2
    It's the same principle as that for the form module you mention, except you place the definition in any global code module - one that is available to you from the Modules tab in Access. If there isn't one then you need to add a new one.

    If you were to place a variable declaration such as

    Code:
    Public SomeVar as Long
    In a named code public code module you can access its value from any other code module regardless of its scope. I would caution that the use of global variables is frowned upon, as their location is hidden away from their use, and they are accessed without being passed through argument lists to subroutines and functions (by what is known as a 'side effect' in structured programming terms - not a good thing).

    Nowadays I tend to use static variables within subs and functions where I would previously have used a global variable. Static variables are locally-defined variables internal to a subroutine or function which retain their values between calls, doing away with the need for side-effect based coding.

    -Stewart

    Comment

    • ncsthbell
      New Member
      • May 2007
      • 167

      #3
      thanks for the reply and suggestion.. I will take a look at the 'static' variables.

      Comment

      Working...