Results 1 to 15 of 15
  1. #1
    5 Star Lounger
    Join Date
    Nov 2004
    Location
    Wilmington, North Carolina, USA
    Posts
    1,196
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Declaring Global Variables (2000)

    Just a general question, no purpose (yet). If I want to set and store a value (perhaps when a user logs in, clicks a button, etc) but do not want to use a table, how could I store the value in code, and call it in queries, etc? I figured I could create a module, set a variable as a string, and call the variable whenever required....I thought I'd seen this before, but I don't know where.
    ____________________________
    Jeremy
    "If you spend more on coffee than on IT security, then you will be hacked. What's more, you deserve to be hacked." -Richard Clarke

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Declaring Global Variables (2000)

    You can declare a variable as public, at the top of a standard module:

    Public strMyVar As String

    and create a function to retrieve its value:

    Public Function GetMyVar()
    GetMyVar = strMyVar
    End Function

    You can use this function in expressions in a query, or in the control source of a text box on a form/report, etc.

    Another method, using a collection object, is described in <post#=198,890>post 198,890</post: >.

  3. #3
    5 Star Lounger
    Join Date
    Nov 2004
    Location
    Wilmington, North Carolina, USA
    Posts
    1,196
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Declaring Global Variables (2000)

    Thanks Hans - I thought it was that easy. So to set it I would use something like:

    Set strMyVar = [forms]![frmMainForm]![txtWhatever]

    ?
    ____________________________
    Jeremy
    "If you spend more on coffee than on IT security, then you will be hacked. What&#39;s more, you deserve to be hacked." -Richard Clarke

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Declaring Global Variables (2000)

    Set is used to assign a value to an object variable. In this example, strMyVar is a string variable, so you'd simply use

    strMyVar = [Forms]![frmMainForm]![txtWhatever]

  5. #5
    5 Star Lounger
    Join Date
    Nov 2004
    Location
    Wilmington, North Carolina, USA
    Posts
    1,196
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Declaring Global Variables (2000)

    <img src=/S/thankyou.gif border=0 alt=thankyou width=40 height=15> <img src=/S/grin.gif border=0 alt=grin width=15 height=15>
    ____________________________
    Jeremy
    "If you spend more on coffee than on IT security, then you will be hacked. What&#39;s more, you deserve to be hacked." -Richard Clarke

  6. #6
    Gold Lounger
    Join Date
    Jun 2001
    Location
    Crystal Beach, FL, Florida, USA
    Posts
    3,436
    Thanks
    1
    Thanked 34 Times in 34 Posts

    Re: Declaring Global Variables (2000)

    Another method would be to store it in the Window's Registry. See Access VBA Help for SaveSetting and GetSetting.
    Mark Liquorman
    See my website for Tips & Downloads and for my Liquorman Utilities.

  7. #7
    Platinum Lounger
    Join Date
    Feb 2002
    Location
    A Magic Forest in Deepest, Darkest Kent
    Posts
    5,681
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Declaring Global Variables (2000)

    I have used the tag property in the past in Form Properties|Other before to get me out of trouble but that was because I was doing "design on the fly" when using multiple subforms on tab controls. It was messy, but it helped get me out of a fix. Go for the code if you can, but as I said, it got me out of a fix.
    Jerry

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

    Re: Declaring Global Variables (2000)

    There isn't anything wrong with using the tag property for this, Jerry, and it is more reliable than using variables since the tag property doesn't get reset when an unhandled error occurs. <img src=/S/shrug.gif border=0 alt=shrug width=39 height=15>
    Charlotte

  9. #9
    Platinum Lounger
    Join Date
    Feb 2002
    Location
    A Magic Forest in Deepest, Darkest Kent
    Posts
    5,681
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Declaring Global Variables (2000)

    Thanks for that Charlotte, it was a trick from Stan Leszynski's Access 97 -Expert Solutions book. I am just a bit wary of bringing it up but in the right place it does work very well
    Jerry

  10. #10
    5 Star Lounger
    Join Date
    Nov 2004
    Location
    Wilmington, North Carolina, USA
    Posts
    1,196
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Declaring Global Variables (2000)

    What happens when the form is closed? (Or is your dirty solution to hide the form?) <img src=/S/evilgrin.gif border=0 alt=evilgrin width=15 height=15>
    ____________________________
    Jeremy
    "If you spend more on coffee than on IT security, then you will be hacked. What&#39;s more, you deserve to be hacked." -Richard Clarke

  11. #11
    Platinum Lounger
    Join Date
    Feb 2002
    Location
    A Magic Forest in Deepest, Darkest Kent
    Posts
    5,681
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Declaring Global Variables (2000)

    The tag value is always there, it is a property of the form and therefore stored
    Jerry

  12. #12
    5 Star Lounger
    Join Date
    Nov 2004
    Location
    Wilmington, North Carolina, USA
    Posts
    1,196
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Declaring Global Variables (2000)

    ahhhh.....nice.
    ____________________________
    Jeremy
    "If you spend more on coffee than on IT security, then you will be hacked. What&#39;s more, you deserve to be hacked." -Richard Clarke

  13. #13
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Declaring Global Variables (2000)

    But if you set the Tag property in code, it is not stored when the form is closed.

  14. #14
    Platinum Lounger
    Join Date
    Feb 2002
    Location
    A Magic Forest in Deepest, Darkest Kent
    Posts
    5,681
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Declaring Global Variables (2000)

    Good point, so by definition, it is not a global variable but as the question said "If I want to set and store a value (perhaps when a user logs in, clicks a button, etc) but do not want to use a table, how could I store the value in code" It could be used for that form session

    As I said Hans it was a dirty fix <img src=/S/evilgrin.gif border=0 alt=evilgrin width=15 height=15>
    Jerry

  15. #15
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Declaring Global Variables (2000)

    It's very useful as such.

Posting Permissions

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