Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    smaier is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Jul 2020
    Posts
    43

    Variable within a variable syntax/punctuation?

    Hello, all!

    What I'm trying to do is use a For/Next loop to reduce the number of lines of code I have by referencing objects with a dynamic name, such as:

    Code:
    For x = 1 To 9
    Me.Controls("DimTypeLabel" & x).Caption = "dlkDTL" & x & "var"
    Next x
    ...where DimTypeLabel1 through DimTypeLabel9 are object names and dlkDTL1var through dlkDTL9var are declared variables. The variable x is just being used to rename the object reference and stored variable value each loop.

    When I run the above code, the object/label's captions read "dlkDTL1var" through "dlkDTL9var" in the correct objects/labels, but it's not recognizing that what's on the right side of the equal sign is a variable within a variable. It sees it as a variable (x) within a text string.

    Is there a proper way to phrase the variable-within-a-variable in this context?



    *Edit: minor change for clarity.

  2. #2
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    Sorry. Reread your requirement and my reply didn't apply so I deleted it.

  3. #3
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    if you're speaking of var as being the Variable...you need to drop the quotation marks around it:

    = "dlkDTL" & x & var

    not

    = "dlkDTL" & x & "var"

    the quotes tells the Access Gnomes that var is a String...not a Variable.

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  4. #4
    smaier is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Jul 2020
    Posts
    43
    No worries, davegri. I edited my post to hopefully clear up my bad phrasing.

  5. #5
    smaier is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Jul 2020
    Posts
    43
    Quote Originally Posted by Missinglinq View Post
    if you're speaking of var as being the Variable...you need to drop the quotation marks around it:

    = "dlkDTL" & x & var

    not

    = "dlkDTL" & x & "var"

    the quotes tells the Access Gnomes that var is a String...not a Variable.

    Linq ;0)>

    Thank you for the reply, Missinglinq!

    Actually, the declared variables are dlkDTL1var, dlkDTL2var, dlkDTL3var, on up to dlkDTL9var. The loop variable x is in place of the number in the middle of the variable.

    I'll try with no quotes at all but I thought it would see dlkDTL, x, and var as 3 separate variables.

  6. #6
    smaier is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Jul 2020
    Posts
    43
    Update;

    With no quotes it did indeed read the not-in-quotes strings as individual variables (variable not defined error).

    I could just change the variable names so the number is at the beginning or end instead of the middle which would work. Or I could just type out all the lines of code individually, dropping the For/Next routine. Just seemed like a slimmer and easier to troubleshoot solution. It would just bug me not knowing if I was asking the impossible or just unable to figure it out.

    I do appreciate the help!

  7. #7
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    This
    what's on the right side of the equal sign is a variable within a variable
    I just don't see. I'd expect the caption would be "dlkDTL1var" for example. As coded, var is a string, not a variable as was pointed out. Perhaps if you show more of the code, because var is somewhat ambiguous. An expected result would also help.
    If the labels are attached, I'm wondering if it would be far simpler to refer to the control's only default member, which is the label, and is referred to as .Controls(0) instead of worrying about referring to the control and it's label as separate entities. Don't know because the whole intent/purpose isn't clear, and what you're calling an object should probably be referred to as a control for clarity.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  8. #8
    smaier is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Jul 2020
    Posts
    43
    Hello, Micron. Thank you for the reply.

    I took your suggestion and have attached a database example. Hopefully this will make up for my inability to properly describe what I'm trying to accomplish code-wise. There's no table. Just a form with some controls and a module declaring variables.

    The short description is I'm trying to reduce 9 lines of code down to one (if you don't count the For/Next lines). If it's possible, this would allow me to apply the method on other forms, reducing the number of lines from ~200 down to maybe a dozen or so.

    Thanks again!


    Example1.accdb

  9. #9
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    If I understand what you're trying to code, then you cannot. Notwithstanding that your reference
    Me.Controls("DimTypeLabel" & x).Caption = dlkDTL & x & "var"

    is sort of backwards because it probably should be

    Me.Controls("DimTypeLabel" & x).Caption = dlkDTL & "var" & x or just

    Me.Controls("DimTypeLabel" & x).Caption = dlkDTLvar & x

    because your construct is dlkDTLvarx where x may be dlkDTLvar5. You're trying to put the var at the end when it's supposed to be in the middle.

    Regardless, you are trying to define constants such as dlkDTLvar9 and those constant names are as you have named them. I doubt that you can refer to them directly by a partial name and make it work just because you're tacking a number onto the end. Trying to refer to the constant as dlkDTLvar & x (where x is e.g.5) will create a string as "dlkDTLvar5". IMO, only if you could pass that string to the name property of something might this work, but I don't know what would hold that name property when you're dealing with constants. I'm not aware of whether or not a constant has a Name property and would have to look that up.

    I'll look at your db to see if I have another suggestion.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Cannot dynamically build VBA variable names. Perhaps you should use an array to save these 9 values then dynamically reference array element.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  11. #11
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    If we knew why this was needed we might be able to provide a simple solution. On my phone, so maybe I missed that?
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  12. #12
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Or consider using a table to hold the values. Open a record set and do the looping. Plus it is easier to add/edit values than editing arrays (don't have to edit code).

  13. #13
    smaier is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Jul 2020
    Posts
    43
    Thank you so much for the replies, guys!

    From what it sounds like, and which would actualy make sense (as June7 stated), you cannot dynamically build variable names in VBA. Not that big a deal as the only benefit I was chasing was limiting the number of lines of code used. Previously I have used this method when referencing controls dynamically with the same For/Next scheme when the control names allowed for it, usually when they were the same name with numerals at the end or wherever. At one point recently I thought to myself "Yaknow, that would be pretty cool if I could do the same with variable names!" and started the process of banging my head against the wall in hopes that could somehow make it work.

    As far as the function/work the code is doing, the database I posted earlier is but a small part of a larger group of forms that get filled out for dimensional inspections, most of which are, at the output side, highly repetitive. One sequence of input text boxes and 45 labels and textboxes that show the same things across multiple forms. This would lend itself to using an array as was also mentioned. This is probably the best solution for me as it stands as I could really use some practice on working with the values stored. I've only used an array in one other working project and it was very simple by comparison.

    Thank you for the replies and input, all! I feel a bit better knowing it wasn't working because it's not possible, rather than because I'm not capable of figuring it out.

  14. #14
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    If you made them textboxes you could probably could call a function and pass the textbox name or Tag property for example, thereby setting its value in the function. One little snippet of code, I'd say. The function could accept a string (which you then process accordingly) or the literal value, in which case you could do away with the constants altogether. In place of the constants or an array, you'd need to identify the function parameter in the textbox source property somehow. Same amount of coding I'd say, but at least it should work.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  15. #15
    smaier is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Jul 2020
    Posts
    43
    Hi Micron.

    That's an interesting suggestion. A bit more complex than I am accustomed to but that's a good thing. Shows a method that could offer more possibilities than my original approach.

    Again, thank you all for the answers and suggestions!

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 07-23-2019, 01:37 PM
  2. logical operator as a variable (syntax?)
    By markjkubicki in forum Programming
    Replies: 5
    Last Post: 03-11-2015, 10:59 AM
  3. simple syntax for putting a variable into a function
    By markjkubicki in forum Programming
    Replies: 2
    Last Post: 11-02-2012, 09:46 AM
  4. Replies: 0
    Last Post: 08-10-2011, 11:59 AM
  5. Long Variable syntax
    By eww in forum Programming
    Replies: 5
    Last Post: 02-10-2011, 03:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums