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

    Global Variables (All)

    I'm doing some contract work on a DB that uses alot of globabal variables, which have gotten sort of unwieldy as the application has grown. Anyway, I'm in a situation where I need to get the value for a specific global variable, but which variable I need to look at depends on the value of control on a form. Actually, the global variable name is a concatenation of a base name plus the control value. Right now, we have to do something like a Select..Case(or IF statements) to get the value, something like this:

    Select Case myControl
    Case 1
    temp = gintField1
    Case 2
    temp = gintField2
    ... etc.
    End Select

    Is there a way to reference a global variable using a string name such as you can do with a form, like in Forms("myform")? I tried to use Eval(), but Access didn't seem to like that.
    Mark Liquorman
    See my website for Tips & Downloads and for my Liquorman Utilities.

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

    Re: Global Variables (All)

    Instead of using global variables, you might store values in a table, or in database properties. Both can be retrieved indirectly (concatenating strings to assemble a field name or a property name), and both have the added advantage that they don't get cleared if an unhandled error occurs.

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

    Re: Global Variables (All)

    Using the global variables was not my idea. It is a large and convoluted system, so I have to work with what is there. I'm just trying to make it a little easier.
    Mark Liquorman
    See my website for Tips & Downloads and for my Liquorman Utilities.

  4. #4
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Niagara Falls, New York, USA
    Posts
    1,878
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Global Variables (All)

    Extremely long code fragment (over 6000 characters) moved to attachment - Mod

    Hi Mark

    Don't know if this applies, the following is what I use for gv's

    May also want to check out 'Scott Machado showed how in "Abstract Your Query Parameters" in the July 2001 issue of
    'ACCESS VB- SQL

    HTH John
    Attached Files Attached Files

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

    Re: Global Variables (All)

    John,

    Thanks for the info. I'm going to look thru your attachment, plus I dug out my old copy of July 2001 edition of Access Advisor! I'll let you know how it works out.
    Mark Liquorman
    See my website for Tips & Downloads and for my Liquorman Utilities.

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

    Re: Global Variables (All)

    Mark,

    You might build a class or even just a standard function that would return the appropriate value based on an argument you passed in. A SELECT CASE statement could return the value from the appropriate global by using the value you passed to determine which variable value you needed. That is probably the fastest and dirtiest way to do it, particularly if you don't really have any control over those globals.
    Charlotte

  7. #7
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Global Variables (All)

    If the only thing that's going to vary at runtime is the final number, couldn't the gintField variables simply be combined into a single global array?

    temp = gaField(myControl)

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

    Re: Global Variables (All)

    >>If the only thing that's going to vary at runtime is the final number, couldn't the gintField variables simply be combined into a single global array?<<

    No. I can't change the names of the global variables. Again, this question was not about how to change the system, but rather how to deal with the variables as they are.
    Mark Liquorman
    See my website for Tips & Downloads and for my Liquorman Utilities.

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

    Re: Global Variables (All)

    Yes, but that would still required use of a SELECT ... CASE statement (or something like it) in the called function; which I may yet do. I was hoping something like this would work, but it won't:

    tem = eval("gintField"& myfield)
    Mark Liquorman
    See my website for Tips & Downloads and for my Liquorman Utilities.

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

    Re: Global Variables (All)

    As far as I know, there is no way to do that, Mark. Variable names are literals, and over the years I've never found a way to do an end run on that. <img src=/S/shrug.gif border=0 alt=shrug width=39 height=15> I think you're going to have to settle for:

    tem = GetGlobal("gintField"& myfield)
    Charlotte

  11. #11
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: Global Variables (All)

    About the only other way you could do this would be use the VB CallByName function, which allows you to call a method or property of an object using a text string. CallByName uses some of the hidden OLE Interface methods used by VB for interaction with COM objects to resolve text string to an actual function address, thus it only works with objects. So you'd have to define an object (ie, class) for this purpose. Example: Assume there are these 3 "global" variables declared in a standard module:

    <code>Public gintGlobal As Integer</code>
    <code>Public glngGlobal As Long</code>
    <code>Public gstrGlobal As String</code>

    Create class module with following read-only properties:

    <code>Public Property Get Property1() As Variant</code>
    <code> Property1 = gintGlobal</code>
    <code>End Property</code>

    <code>Public Property Get Property2() As Variant</code>
    <code> Property2 = glngGlobal</code>
    <code>End Property</code>

    <code>Public Property Get Property3() As Variant</code>
    <code> Property3 = gstrGlobal</code>
    <code>End Property</code>

    If you want the properties to be read/write, include Let procedures:

    <code>Public Property Let Property1(ByVal NewValue As Variant)</code>
    <code> gintGlobal = NewValue</code>
    <code>End Property</code>

    (Etc for the other properties.) Example of using CallByName in standard module:

    <code>Public Sub TestCallByName()</code>

    <code> Dim n As Long</code>
    <code> Dim obj As Class1</code>
    <code> Set obj = New Class1</code>

    <code> gintGlobal = 1</code>
    <code> glngGlobal = 111</code>
    <code> gstrGlobal = "ABC"</code>

    <code> For n = 1 To 3</code>
    <code> Debug.Print CallByName(obj, "Property" & n, VbGet)</code>
    <code> Next n</code>

    <code> ' If read/write, set values:</code>
    <code> CallByName obj, "Property1", VbLet, 9</code>
    <code> CallByName obj, "Property2", VbLet, 999</code>
    <code> CallByName obj, "Property3", VbLet, "XYZ"</code>

    <code> For n = 1 To 3</code>
    <code> Debug.Print CallByName(obj, "Property" & n, VbGet)</code>
    <code> Next n</code>

    <code> Set obj = Nothing</code>
    <code>End Sub</code>

    I don't know if this would be worth the trouble, you'd have to create a Property procedure for each "global" variable. CallByName is not the most efficient method for retrieving values, as it requires the extra overhead of instantiating objects, and by definition uses late binding to call object properties and methods. But it does allow you to reference property/method as a text string; not aware of any way to directly reference the variable as a string.

    HTH

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

    Re: Global Variables (All)

    <sigh> I think you are right. Well, it was worth a try!
    Mark Liquorman
    See my website for Tips & Downloads and for my Liquorman Utilities.

Posting Permissions

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