How to loop through variables

lws

Registered User.
Local time
Today, 13:18
Joined
Mar 14, 2013
Messages
22
I am looking to simply loop through serveral variables in vba. I know this should be simple but I am struggling.





str0 = "OOS"
str1 = "PM"
str2 = "CAL"

For i = 0 To 2

Generate_VField_Loop:

sst = "str" & i

frm![Variable_Field] = frm![Variable_Field] & sst & vbCrLf

Exit_Generate_VField_Loop:

Next i






The desired output in frm![Variable_Field] should be as follows:

OOS
PM
CAL

What I get is:

str0
str1
str2


Any help would be very much appreciated.

Thanks
 
This is a good chance for you to use the locals window and step trhough the code to see whats going...
 
You're trying to use an array without actually using an array. Google 'VBA Array'.
 
Code:
Dim sst(3) As String

sst(0) = "OOS"
sst(1) = "PM"
sst(2) = "CAL"

For i = 0 To 2

    Me.[Variable_Field] = Me.[Variable_Field] & sst(i) & vbCrLf

Next i
 
The code I posted should work - ?
 
Ok I made it up as an array. with code as follows:

Dim sst(3) As String
Dim i As Long
sst(0) = "OOS"
sst(1) = "PM"
sst(2) = "CAL"

For i = 0 To 2

sst(i) = i

Me.[Variable_Field] = Me.[Variable_Field] & sst(i) & vbCrLf


Next i





The output is:
0
1
2

What I would like it to be is:
OOS
PM
CAL


What am doing wrong?
 
Yes it does i forgot to delete the sst(i)=i statement.

Thanks a bunch
 
You're welcome. How many vars will you have and will they be dynamic? Their may be better ways to do this...
 
There will be about 4-6. Unsure if I want to incorperate the last 2 yet. And no they will not be dynamic. It will depend on what the operator checks on the form.

Thanks again I'll give this a whirl and see what will work the best.
 
Cool - Yeah, if it's just a handful your probably good hardcoding 'em...
 

Users who are viewing this thread

Back
Top Bottom