Results 1 to 8 of 8
  1. #1
    Jen0dorf is offline Competent Performer
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    UK
    Posts
    453

    global versus local versu temporary variable

    Hi

    am changing my database to use new knowledge gained - thanks one and all.

    Can I just confirm that a global variable remains as long as the database is open.

    A local variable remains as long as the form is open

    A temporary variable is used for temporary items within a set sequence of actions

    To explain my project is now becoming for me rather large.

    At the top of each form is one of two text sequences and I though I could create a form to hold the text in fields and then on database load create global variables to hold the values. The call these values into each form.

    Then when the secretary or phone number or anything changes I only have to change one line.



    I though a global variable was the way to go????

    thanks

    Ian

  2. #2
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,741
    A local variable remains as long as the form is open
    Not quite that broad. A variable dimmed in a procedure (a sub or a function) is valid only within that procedure WHILE THAT PROCEDURE IS EXECUTING.
    You might also look into Tempvars for holding global values. Tempvars can be defined on the fly in code, while global constants have hard coded values.
    Last edited by davegri; 07-29-2017 at 08:51 AM. Reason: clarif

  3. #3
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,933
    A global variable - that is one declared as Public at the top of a module

    - will remain available to the entire code whilst the code is running if declared in a standard or class module
    - will remain available to the entire code whilst the form is open if declared in a form module
    - you cannot declare a public variable in a function or sub
    - it will lose any values if you have an unhandled error

    A tempVar is similar but will retain its value if you do get an unhandled error
    - it also has the benefit it can be referenced in a query

    A local variable - one that is declared with Dim or Const
    - will remain available to the code for the life of where it is declared, be it a function/sub/form or report
    - it will lose any values if you have an unhandled error

    A Static variable - one that is declared with Static
    - can only be declared in a function or sub
    - so is only available to the code in that function or sub
    - like tempvars it retains its value if you get an unhandled error

    For functions and subs you can declare them as public (the default if not declared) or private
    in terms of availability to other code, public is the equivalent of a global variable and private a local variable


    you might find this link useful

    https://www.youtube.com/watch?v=KxzoDFxX9As

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Quote Originally Posted by davegri View Post
    while global constants have hard coded values.
    Variables can be reset, constants cannot. Examples:

    Global gstrArrayTestMethods() As Variant

    Global Const gstrBasePath = "C:\Crm\Lab\Database\"

    Public Const PI As Double = 3.14159265358979
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Then when the secretary or phone number or anything changes I only have to change one line.
    You might consider using a table, maybe name it "GlobalConstants" or "GlobalData", rather than put it (secretary or phone number or anything) in code. In a table you can use a form to easily change the data; in code you have to go into design view. You can still use code to fill global variables, tempVars, whatever.

    It works for me......




    My $0.02 ..........

  6. #6
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,933
    My $0.02 ..........
    and worth every cent. Had only focused on the definitions, not the way it is intended to be used

  7. #7
    Jen0dorf is offline Competent Performer
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    UK
    Posts
    453
    Quote Originally Posted by ssanfu View Post
    You might consider using a table, maybe name it "GlobalConstants" or "GlobalData", rather than put it (secretary or phone number or anything) in code. In a table you can use a form to easily change the data; in code you have to go into design view. You can still use code to fill global variables, tempVars, whatever.

    It works for me......




    My $0.02 ..........
    Thanks think thats the way I'll go

    cheers

    Ian

  8. #8
    CodeLiftSleep is offline Advanced Beginner
    Windows 10 Access 2013 32bit
    Join Date
    May 2017
    Posts
    48
    I create globals for use as Singleton's---for instance if I have a loggedInUser Class with various properties like UserID and UserName, etc, I'll create a "GlobalFunctions" module and create a single instance of this class as something like:

    Public loggedUser as New LoggedInUser

    And since we only have one of these at any time in a program, this object can be referenced from anywhere in code...this is similar to creating a Public Static Class in C# to use as a Singleton, since you don't want to actually create an instance of it, simply be able to use it...in this case we have to create a single instance of it due to VBA limitations, but then use the instantiated object like its a Singleton.

    I also use it for creating FilePath strings to reference from anywhere.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Global Variable
    By gtg430i in forum Programming
    Replies: 11
    Last Post: 03-10-2014, 05:00 PM
  2. Can't See Global Variable
    By CementCarver in forum Programming
    Replies: 12
    Last Post: 09-19-2013, 12:28 PM
  3. Global variable
    By ramdandi in forum Queries
    Replies: 3
    Last Post: 12-18-2011, 01:01 AM
  4. Replies: 4
    Last Post: 07-14-2011, 12:55 PM
  5. Setting Temporary Variable
    By KEVWB in forum Access
    Replies: 1
    Last Post: 02-07-2011, 01:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums