0

I have to iterate through a table and test 14 different fields for a certain value. All of these fields have a similar name, e.g. foo 1, foo 2, foo 3 etc.

I want to use a For loop to iterate through the different fields so I can condense 15 If-Else If statements to something like the following

For i = 2 to 15
    Dim fieldname as String
    fieldname = "foo " + i
    If conditionMet(MyTableRecordSet![fieldname]) Then
         'Update another column
    End If
Next i

But I know if I do it this way VBA will look for a field literally named "fieldname", it won't interpolate the variable's value as I wish. Is there a way to get the variable's value in there? If not, is there another way to systematically go through a record's values without too many If-Else If statements?

1 Answer 1

1

Try below code

For i = 2 To 15
    Dim fieldname As String
    fieldname = "foo " & i
    If conditionMet(MyTableRecordSet(fieldname)) Then
         'Update another column
    End If
Next i
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! I rarely program in VBA so I'm unfamiliar with its idiosyncrasies.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.