• Global Variables (All)

    Author
    Topic
    #407865

    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.

    Viewing 7 reply threads
    Author
    Replies
    • #856447

      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.

      • #856461

        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.

      • #856462

        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.

    • #856448

      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.

    • #856485

      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

      • #856514

        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.

      • #856515

        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.

    • #856486

      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

    • #856534

      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.

      • #856697

        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)

        • #856851

          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. shrug I think you’re going to have to settle for:

          tem = GetGlobal(“gintField”& myfield)

        • #856852

          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. shrug I think you’re going to have to settle for:

          tem = GetGlobal(“gintField”& myfield)

        • #856906

          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:

          Public gintGlobal As Integer
          Public glngGlobal As Long
          Public gstrGlobal As String

          Create class module with following read-only properties:

          Public Property Get Property1() As Variant
          Property1 = gintGlobal
          End Property

          Public Property Get Property2() As Variant
          Property2 = glngGlobal
          End Property

          Public Property Get Property3() As Variant
          Property3 = gstrGlobal
          End Property

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

          Public Property Let Property1(ByVal NewValue As Variant)
          gintGlobal = NewValue
          End Property

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

          Public Sub TestCallByName()

          Dim n As Long
          Dim obj As Class1
          Set obj = New Class1

          gintGlobal = 1
          glngGlobal = 111
          gstrGlobal = "ABC"

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

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

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

          Set obj = Nothing
          End Sub

          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

        • #856907

          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:

          Public gintGlobal As Integer
          Public glngGlobal As Long
          Public gstrGlobal As String

          Create class module with following read-only properties:

          Public Property Get Property1() As Variant
          Property1 = gintGlobal
          End Property

          Public Property Get Property2() As Variant
          Property2 = glngGlobal
          End Property

          Public Property Get Property3() As Variant
          Property3 = gstrGlobal
          End Property

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

          Public Property Let Property1(ByVal NewValue As Variant)
          gintGlobal = NewValue
          End Property

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

          Public Sub TestCallByName()

          Dim n As Long
          Dim obj As Class1
          Set obj = New Class1

          gintGlobal = 1
          glngGlobal = 111
          gstrGlobal = "ABC"

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

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

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

          Set obj = Nothing
          End Sub

          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

      • #856698

        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)

    • #856535

      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.

    • #856589

      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)

      • #856693

        >>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.

      • #856694

        >>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.

    • #856590

      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)

    Viewing 7 reply threads
    Reply To: Global Variables (All)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: