Iterate through numbered variables

terryvanduzee

Registered User.
Local time
Today, 06:58
Joined
Sep 23, 2005
Messages
22
Hello

I have a series of variables ( var1 - var20 )
I also have txtboxes numbered txt1 to txt20

I am trying to set all the variables to 0 by using a for loop

Code:
dim x as integer
dim c as string

for x = 1 to 20
c="var"
  c & x = 0
next x

This does not work

for the txtboxes:
Code:
for y = 1 to 20
d="txt" & y
   forms!myform. & d & .value = somenumericvalue ' "Expected: expression"

Next y
These are not working at all.

On both I get the error.

How can this be done

Thank you so much

Terry
 
Look up the str() function.

Also, you have:
c & x = 0. You cannot set two concatenated variables to...well, anything. It is not a left hand operator.

In you second sample, you can use:
Code:
d = "txt" & Str(y)
to get the results I think you're looking for.

Also, you didn't mention what error you were getting and where you were getting it. These were a couple of things that were easy to see.
 
Alternatively, use Controls collection instead.

Code:
Me.Controls("txt" & y).Value
 

Users who are viewing this thread

Back
Top Bottom