1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: Declaring Global Variables in Access 2007

Discussion in 'Business Applications' started by SimonB, Sep 30, 2008.

Thread Status:
Not open for further replies.
Advertisement
  1. SimonB

    SimonB Thread Starter

    Joined:
    Sep 21, 2004
    Messages:
    24
    Access 2007 is supposed to handle global variables much better than previous versions, but I can't seem to find any instruction on how and where to declare them. I am working in multiple form modules, and I want a variable that will stay active, with a set value that can be changed from time to time as needed, throughout the Access session, from one form to another. Can someone help me with this?
    Thanks, SimonB
     
  2. OBP

    OBP

    Joined:
    Mar 8, 2005
    Messages:
    19,895
    Simon, isn't it the same as previous versions, i.i. in a module?
     
  3. Rollin_Again

    Rollin_Again

    Joined:
    Sep 4, 2003
    Messages:
    4,912
    Declare the variable in a stand alone module and use the following syntax.

    Code:
    Public myGlobalVar As String
    Regards,
    Rollin
     
  4. SimonB

    SimonB Thread Starter

    Joined:
    Sep 21, 2004
    Messages:
    24
    In my digging over the last hour or so there seems to be a new collection (my symantics may be wrong here) of "Temporary Variables" that remain in memory until you close the database. It's a collection of variables that can be used application-wide and survive code resets, unlike previous Access versions. Example: In AutoExec macro, SetTempVar named LicensedTo to "Acme Corporation", and add this text box to each report header: =[TempVars]![LicensedTo]. I've been able to declare the TempVar, but can't find how to change its value.

    Here's what I have found. It's interesting.
    http://blogs.msdn.com/thirdoffive/archive/2006/04/13/570539.aspx
    http://www.allenbrowne.com/Access2007.html (search for "TempVar")
     
  5. SimonB

    SimonB Thread Starter

    Joined:
    Sep 21, 2004
    Messages:
    24
    OK, looks like I may have got it. This is the syntax:
    This declares the global(TempValue) variable:
    TempVars("tempvariablename").Value = initialvalue (the ".Value" is optional)
    (you have to give it a value, and do not declare a type)
    This is the syntax for changing that value anywhere you need to:
    [TempVar]![tempvariablename].Value = new-value
    (it seems you need to specify the parameter ".Value" here)
    And this is how to use the TempVariable elsewhere:
    variablename = [TempVar]![tempvariablename].Value
    Thanks, anyway, gang.
    Best, SimonB
     
  6. SimonB

    SimonB Thread Starter

    Joined:
    Sep 21, 2004
    Messages:
    24
    I thought I had it, but it seems now I don't. Here's what I am doing:
    I declare the TempVars this way:
    TempVars("LoginAttemptCounter").Value = Val("0")
    TempVars("MaxAllowedLoginAttempts").Value = Val("3")
    Then I loop through a process of validating login credentials, with this check in it:
    If [TempVar]![LoginAttemptCounter] < [TempVar]![MaxAllowedLoginAttempts] Then...
    The compiler doesn'y like this statement. My syntax is bad, somehow. Does anyone see the problem?
    SimonB
     
  7. OBP

    OBP

    Joined:
    Mar 8, 2005
    Messages:
    19,895
    Simon, wouldn't it be better to assign the TempVar to a declared Variable in you current procedure and use that for comparisons.
     
  8. SimonB

    SimonB Thread Starter

    Joined:
    Sep 21, 2004
    Messages:
    24
    OBP,
    Someone sent me the solution. Instead of this to refer to the TempVar:
    [TempVar]![tempvariablename].Value
    ...use this:
    TempVars("tempvariablename").Value
    It works.
    Thanks for your help.
     
  9. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/754833

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice