Results 1 to 4 of 4
  1. #1
    2 Star Lounger
    Join Date
    Nov 2001
    Posts
    119
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Can I loop through variable names? bln1,bln2,bln3 (2002)

    Hi,

    Simple question. If I have a number of variables named in sequence.

    Dim blnTest1 as boolean
    Dim blnTest2 as boolean
    Dim blnTest3 as boolean
    ...

    Is it possible to loop through these in a piece of code? ie:

    For i = 1 to 3
    blnTest(i) = True
    Next i

    I know that syntax is incorrect, but I is there a correct or alternative way of doing this?

    Thanks, Chris

  2. #2
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Can I loop through variable names? bln1,bln2,bln3 (2002)

    Hi Chris,
    You can do this by declaring a boolean array rather than individual variables:
    Dim arrTest(1 to 3) as Boolean
    For i = 1 to 3
    arrTest(i) = True
    Next i

    Hope that helps.
    Regards,
    Rory

    Microsoft MVP - Excel

  3. #3
    2 Star Lounger
    Join Date
    Nov 2001
    Posts
    119
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Can I loop through variable names? bln1,bln2,bln3 (2002)

    Perfect. Thanks Rory!

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Can I loop through variable names? bln1,bln2,bln3 (2002)

    If you have a case where an array isn't as convenient, for some reason, you can use CallByName to construct the variable name from bits and pieces. See <!post=Post #204170,204170>Post #204170<!/post> over on the Access board. Neat trick, eh!

Posting Permissions

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