Results 1 to 6 of 6
  1. #1
    kdbailey is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Aug 2012
    Posts
    228

    Use a string to refer to a variable

    I'm more or less just curious if this is even possible. There have been many instances where I wished I could just do a for-loop that rotates through controls as well as the relating variables.

    For instance...

    p1 is the variable that contains the number that will go into control "c1"
    p2 to "c2"
    so on...



    I realize I can use an array, but at times I'd rather not deal with an array. Is it possible to do something like the following (I'll use "Variables" as a fake property)...

    Code:
    Function Test()
    For d = 1 to 5
       Forms("example").Controls("c" & d) = Variables("p" & d)
    Next d
    End Function

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    not quite, but you can make a collection ,(better than array)

    Code:
      dim col as new collection
      dim sVar as string
    
      for d = 1 to 5
         sVar = "p" & d
          col.add "c" & d, sVar
      next
    
      ' msgbox  col("p1")  will show "c1"
    
         'set control
       controls("c1") =  col("p1")

  3. #3
    CJ_London is online now VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,933
    yes except you would normally run from within the form so it would be

    me("c" & d)=....

  4. #4
    kdbailey is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Aug 2012
    Posts
    228
    Interesting, I've never used a collection. I'll have to take a look into them.

  5. #5
    kdbailey is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Aug 2012
    Posts
    228
    Ok, so a local variable can be accessed within a form. How about a variable in a module or a global variable?

  6. #6
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I'm more or less just curious if this is even possible. There have been many instances where I wished I could just do a for-loop that rotates through controls as well as the relating variables.
    Not sure where you are headed with this, but this is as close as I could come up with.

    Create a form named "example". Add 5 text boxes named "c1", "c2", "c3", "c4", & "c5".
    Paste in the following code in any module:
    Code:
    Sub Test()
        Dim d As Integer
        Dim p1 As Integer, p2 As Integer, p3 As Integer, p4 As Integer, p5 As Integer
        Dim tmp As Integer
    
        p1 = 100
        p2 = 200
        p3 = 300
        p4 = 400
        p5 = 500
    
        For d = 1 To 5
            tmp = 0   'clear last value
    
            tmp = Choose(d, p1, p2, p3, p4, p5)
            
            Forms!example.Controls("c" & d) = tmp
        Next d
    
    End Sub
    Single step through or execute the code. Is this what you are looking for??
    As an alternative to the Choose() function, you could instead use "SELECT CASE... END SELECT".

Please reply to this thread with any new information or opinions.

Similar Threads

  1. How to use DLookup with string variable Criteria
    By JrMontgom in forum Programming
    Replies: 1
    Last Post: 03-09-2014, 07:55 PM
  2. Using Variable String to Set a Form object
    By neo651 in forum Programming
    Replies: 17
    Last Post: 07-08-2013, 12:59 PM
  3. Replies: 3
    Last Post: 05-28-2013, 12:53 PM
  4. Extract Value from Variable in String
    By nguyenak in forum Programming
    Replies: 3
    Last Post: 05-24-2012, 03:50 PM
  5. Using a string variable to specify a control
    By Gerry in forum Programming
    Replies: 3
    Last Post: 04-14-2010, 02:28 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