Results 1 to 12 of 12

Thread: [RESOLVED] get value for a variable where the name of the variable is composed dynamically

  1. #1

    Thread Starter
    Frenzied Member wengang's Avatar
    Join Date
    Mar 2000
    Location
    Beijing, China
    Posts
    1,550

    Resolved [RESOLVED] get value for a variable where the name of the variable is composed dynamically

    Sorry for that clunky title.

    I need to do this:
    Dim strName as String
    strName = "Bill"

    a = "str"
    b = "Name"

    x = Eval(a&b) 'this is where I need help

    and now x should hold the value "Bill"

    I know this can be done because I've done it before.
    How do I compose the name of a variable programmatically and retrieve its value?
    I know i can pass strName to x directly, but I need to be able to do it dynamically.

    I don't think Eval() is what I'm looking for, but I know there is a way to do it. Just can't remember.

    Thanks.
    Wen Gang, Programmer
    VB6, QB, HTML, ASP, VBScript, Visual C++, Java

  2. #2
    Hyperactive Member
    Join Date
    Mar 2018
    Posts
    445

    Re: get value for a variable where the name of the variable is composed dynamically

    you can do it but the variable needs to be public in the module. Also works if the variable is part of a class (replace me with the class in the callbyname function)


    Code:
    Option Explicit
    
    Public strName As String  'must be public
    
    Private Sub Form_Load()
        Dim a As String
        Dim b As String
        Dim x As String
        
        strName = "Bill"
    
        a = "str"
        b = "Name"
    
        x = CallByName(Me, a & b, VbGet)
    
        Debug.Print x
    
    End Sub

  3. #3

    Thread Starter
    Frenzied Member wengang's Avatar
    Join Date
    Mar 2000
    Location
    Beijing, China
    Posts
    1,550

    Re: get value for a variable where the name of the variable is composed dynamically

    Quote Originally Posted by DllHell View Post
    you can do it but the variable needs to be public in the module. Also works if the variable is part of a class (replace me with the class in the callbyname function)

    End Sub
    [/CODE]

    This worked in a major way. I started with trying to get an array control created at runtime but just could not get it going.

    Your string solution worked, so I got greedy and modified it for the control, and it worked:

    CallByName(lblLeft(t), "BackColor", VbGet)

    lblLeft(t) is actually Label206 on the form (in this case), but Access just would not allow me to dynamically construct that name and feed it into Me.Controls.

    Thanks for a fancy solution I never even heard of. It reminds me of GetElementById in Java.
    Wen Gang, Programmer
    VB6, QB, HTML, ASP, VBScript, Visual C++, Java

  4. #4
    Hyperactive Member
    Join Date
    Mar 2018
    Posts
    445

    Re: get value for a variable where the name of the variable is composed dynamically

    Quote Originally Posted by wengang View Post
    Thanks for a fancy solution I never even heard of. It reminds me of GetElementById in Java.
    callbyname has some overhead so you might have a performance penalty if you are using it a lot

  5. #5
    Hyperactive Member
    Join Date
    Mar 2018
    Posts
    445

    Re: get value for a variable where the name of the variable is composed dynamically

    dupe

  6. #6

    Thread Starter
    Frenzied Member wengang's Avatar
    Join Date
    Mar 2000
    Location
    Beijing, China
    Posts
    1,550

    can't get control properties when referencing the control by string

    Hi all.
    Backdrop: I have an Access form with numerous labels (Label, Label102, Label103, etc.)
    In code, to make them easier to work with, I set up a number of label arrays and grouped the form's labels into them. Names of arrays are lblLeft, lblRight, lblTop, lblBottom, etc.

    I need a sub that gets the .BackColor from a specific element of a specific array. The array name is passed as a parameter to the sub. The index is provided in the code as I work through the array. I could do this fairly easily if I wrote one sub for each group, but I want to write a single sub.
    ............
    The problem. I've tried multiple methods to dynamically build the name of the control and get its .BackColor property, but it fails every time.

    What I've tried:
    Me.Controls("Label206").BackColor 'This works, but it references the control on the form, not the array control
    Me.Controls("lblLeft(1)").BackColor 'Does not work, "Access cannot find the Field 'lblLeft(1)'...

    CallByName(Label206, "BackColor", VbGet) 'This works but for the form control, not the array control
    CallByName(lblLeft(1), "BackColor", VbGet) 'This works, but I referenced the array control itself

    I need the following to work:
    CallByName("lbl" & ArrayName & "(" & Index & ")", "BackColor", VbGet) 'This does not work, says 'Object required' because it expected the string I passed to be a control.

    This worked too:
    Dim MyLabel As Label
    Set MyLabel = Controls("Label206") form label in quotes
    MsgBox (MyLabel.BackColor)

    This did not work either:
    Dim MyLabel As Label
    Set MyLabel = Controls("lblLeft(1)")
    MsgBox (MyLabel.BackColor) ' cannot find the Field lblLeft(1)

    Dim MyLabel As Label ' THis does not work. Same reason.
    Set MyLabel = Controls("lblLeft")
    MsgBox (MyLabel(1).BackColor)

    I need to be able to dynamically insert the Group name (Left, Right, etc) and the index number, and then access properties of that specific control.

    Help?!

    Thanks.
    Wen Gang, Programmer
    VB6, QB, HTML, ASP, VBScript, Visual C++, Java

  7. #7

    Thread Starter
    Frenzied Member wengang's Avatar
    Join Date
    Mar 2000
    Location
    Beijing, China
    Posts
    1,550

    Re: [RESOLVED] get value for a variable where the name of the variable is composed dy

    In the end, the posted codes worked but when I tried to switch back to building the control name in a string, it simply will not work no matter what I do.

    Since I was actually in Access and not VB6, I moved the question to Office Development.
    Wen Gang, Programmer
    VB6, QB, HTML, ASP, VBScript, Visual C++, Java

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    36,280

    Re: [RESOLVED] get value for a variable where the name of the variable is composed dy

    Well, actually, I moved it. In the future, you can just report the post and we'll move it for you. That way you retain anything of value from the old location, along with an (expiring) link from the old site.
    My usual boring signature: Nothing

  9. #9

    Thread Starter
    Frenzied Member wengang's Avatar
    Join Date
    Mar 2000
    Location
    Beijing, China
    Posts
    1,550

    Re: [RESOLVED] get value for a variable where the name of the variable is composed dy

    Quote Originally Posted by Shaggy Hiker View Post
    Well, actually, I moved it. In the future, you can just report the post and we'll move it for you. That way you retain anything of value from the old location, along with an (expiring) link from the old site.
    Well, the problem is I had marked the old thread resolved. Now this one is marked resolved.

  10. #10
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    2,560

    Re: [RESOLVED] get value for a variable where the name of the variable is composed dy

    IIRC, Elroy did something like an "Array for Controls in VBA" here in the Office-Forum (or was it in the vb6-Forum?)

    EDIT: Found it
    https://www.vbforums.com/showthread....r-VBA-Controls
    One System to rule them all, One IDE to find them,
    One Code to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    For health reasons i try to avoid reading unformatted Code

  11. #11
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,375

    Re: [RESOLVED] get value for a variable where the name of the variable is composed dy

    you can add all /some controls to a collection of classes of control objects for a type of control, this will allow the receipt of events as well as access to properties (similar to a control array in vb6)

    create a class of a control object with events
    iterate through all controls, create a new class for each matching control type
    pass the event to a procedure with the object that sends it as a parameter

    like this a single procedure can process the events for multiple controls of the same type
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  12. #12

    Thread Starter
    Frenzied Member wengang's Avatar
    Join Date
    Mar 2000
    Location
    Beijing, China
    Posts
    1,550

    Re: [RESOLVED] get value for a variable where the name of the variable is composed dy

    Hi all. Thanks for the input. It proved too challenging for me to overcome the inability to call the programatically created array elements by string composed names, so I gave up. I came up with a workaround. When I feed all the elements into the respective arrays, I record all of the actual Label names in a single string array. Since I know how many elements there are in each of the arrays, I know where each array's element names start in the string array. So I was able to make a quick conversion of Element Name + Index into the actual Label name. This worked, so I'm happy. All the software/Access form "ware" I write these days is just for myself or coworkers/friends, so I've adopted a "good enough" attitude to coding. I know some of you would cringe if you saw my inefficient codes, but it's working now. Thanks again all.
    Wen Gang, Programmer
    VB6, QB, HTML, ASP, VBScript, Visual C++, Java

Posting Permissions

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



Click Here to Expand Forum to Full Width