How to access to a variable's value implicitly?!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • alinagoo
    New Member
    • Apr 2010
    • 53

    How to access to a variable's value implicitly?!

    Hello all.
    Please help me to know how i can access to the value of a variable implicitly.
    Assume i have many variables and i don't want to initialize them one by one with their name such as:
    CustomerJack=10 00
    I have a function PName() that returns the name of variables and i want to use this names to access to the related variables.
    For example: PName(Jack) returns "CustomerJa ck" and I'm looking for a method to initialize the variable named "CustomerJa ck" !I used this enhancement earlier in C++ with pointers and "*" operator but in VBA I don't know any way similar.
    Thanks in advanced
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32653

    #2
    I'm afraid VBA is a higher level language than the C family. Similar functionality is provided for cetain areas using a concept of Collections, but this does not extend to variables in the language itself.

    You can, of course, use arrays if you feel that is necessary.

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32653

      #3
      I also tried out the Eval() function for you, but was unable to get that to be any help :(

      Comment

      • alinagoo
        New Member
        • Apr 2010
        • 53

        #4
        Originally posted by NeoPa
        I also tried out the Eval() function for you, but was unable to get that to be any help :(
        Hello NeoPa
        You are right, But in my case i can't use array or any data structure like that(one name with some indexes).
        I need to work to specific variables with distinguishable Name and data type.
        You also suggested me to use EVAL() function.
        Thanks.
        I tried it earlier and it works fine with formulas and functions but unfortunately it does not work like you said and i wanted(giving the content of the variable that it's name maintain in another string variable)
        Now I'm confusing and i do not know to do what to pass this problem.~X(

        Comment

        • OldBirdman
          Contributor
          • Mar 2007
          • 675

          #5
          I don't have an answer here. This seems like a very old problem. I have been stumped by this same problem in attempting to set up an .INI file to set defaults for Access projects.
          Many commercial programs use an .INI file to allow customization. The program may even create such a file on installization, and modify it to preserve options between uses. Someone has a solution.
          I'd like to intepret each line of such a file, such as
          Code:
          FormColor=128005
          ShowHints=True
          CompanyName="Tradewinds"
          . . .
          EOF
          My only solution so far is a very ugly SELECT CASE. Like alinagoo, using arrays doesn't help.

          Comment

          • alinagoo
            New Member
            • Apr 2010
            • 53

            #6
            Yeah
            Select Case is possible but it's very bad solution specially for my project with tens distinguished variables of different data types.
            Some one offered to use "VB Collections" for this case but i can't understand how can i access to a collection's members implicitly!
            I think with Collections this problem is exist.
            What do you think?

            Comment

            • OldBirdman
              Contributor
              • Mar 2007
              • 675

              #7
              Is there a "Variables" collection?
              I would expect so, with properties of type, scale, name, and value. Otherwise how could Item2 = Item1 + 1 be executed. The built-in function VarType() will give the type of a variant variable, so some of this information is available at run-time.
              I don't have an answer. I was hoping to get someone else on this thread by suggesting a common problem similar to the original question.
              So...What is the name of the Variables Collection?

              Comment

              • NeoPa
                Recognized Expert Moderator MVP
                • Oct 2006
                • 32653

                #8
                There is no inbuilt collection as far as I know, for variables (as I mentioned in post #2).

                However, it is possible, though laborious to one dimension, to set up your own collection to store the variables you're interested in. This is a bit like array work except you can assign more meaningful names of course.

                I don't know if this suits your requirements properly, but see this example code which illustrates the concept :
                Code:
                Public Sub TestMe(strVar As String)
                    Dim str1 As String, str2 As String, str3 As String
                    Dim colMe As New Collection
                
                    str1 = "I am from str1"
                    str2 = "I am from str2"
                    str3 = "I am from str3"
                    Call colMe.Add(str1, "str1")
                    Call colMe.Add(str2, "str2")
                    Call colMe.Add(str3, "str3")
                    Debug.Print colMe(strVar)
                End Sub
                Calling the procedure with any valid variable name will cause the contents of the actual named variable to display in the Immediate Pane.
                Code:
                TestMe("str3")
                I am from str3
                Have fun playing with this.

                Comment

                • alinagoo
                  New Member
                  • Apr 2010
                  • 53

                  #9
                  Originally posted by NeoPa
                  There is no inbuilt collection as far as I know, for variables (as I mentioned in post #2).

                  However, it is possible, though laborious to one dimension, to set up your own collection to store the variables you're interested in. This is a bit like array work except you can assign more meaningful names of course.

                  I don't know if this suits your requirements properly, but see this example code which illustrates the concept :
                  Code:
                  Public Sub TestMe(strVar As String)
                      Dim str1 As String, str2 As String, str3 As String
                      Dim colMe As New Collection
                  
                      str1 = "I am from str1"
                      str2 = "I am from str2"
                      str3 = "I am from str3"
                      Call colMe.Add(str1, "str1")
                      Call colMe.Add(str2, "str2")
                      Call colMe.Add(str3, "str3")
                      Debug.Print colMe(strVar)
                  End Sub
                  Calling the procedure with any valid variable name will cause the contents of the actual named variable to display in the Immediate Pane.
                  Code:
                  TestMe("str3")
                  I am from str3
                  Have fun playing with this.
                  Good
                  Your answer is clear and complete.
                  I founded another more flexible Method , "TempVars":
                  Code:
                  Function foo()
                  TempVars("Temp1").Value = 3
                  For Each tv In TempVars
                      MsgBox tv.Name & " = " & CStr(tv.Value)
                  Next t
                  End Function
                  (This would show a message box with "Temp1 = 3").
                  But if you want to use this method in Access 2003 or earlier you must add a module with following code because it have been introduced from Access 2007.
                  Code:
                  Option Compare Database
                  Option Explicit
                  
                  'Note: Collection's index is one-based, not zero-based!
                  Private colTempVars As New Collection
                  
                  Public Function TempVars(Item As Variant) As Variant
                     TempVars = colTempVars(Item)
                  End Function
                  
                  Public Function AddTempVars(initialValue As Variant, TempVarName As String)
                  
                  colTempVars.Add initialValue, TempVarName
                  
                  End Function
                  
                  Public Sub ClearTempVars()
                  
                  Do Until colTempVars.Count = 0
                     colTempVars.Remove 1
                  Loop
                  
                  End Sub
                  Thanks more & Good Luck For Every Body

                  Comment

                  • NeoPa
                    Recognized Expert Moderator MVP
                    • Oct 2006
                    • 32653

                    #10
                    I see that module (for pre-2007) is essentially doing the same as the code I showed except it encapsulates the functionality into a module. You could go further if you liked, and implement this into a Class.

                    Comment

                    Working...