While loop Help

mapat

Registered User.
Local time
Today, 15:42
Joined
Feb 2, 2007
Messages
176
hello,

Let's say I have 10 variables (ID1, ID2, .........,ID10)
In java, I can use a while loop as follows to go through each variable

For example:
i=1
j=1
while (j<=10)
{
ID+i = 0 //this is some type of initialization of each var to 0
i = i + 1
j = J + 1
}
What would be the equivalent code in VBA, especially for "ID+i" ?? I use the '&' symbol but it concatenates everything meaning in the first loop goes to ID1 (which is correct), the second loop goes to ID12 (which is NOT correct), the third loop goes to ID13 (which is NOT correct either), and so on.

Thank you very much
 
Play with this:
Code:
Public Sub Test()
Dim j As Integer
Dim strID As String

j = 1
Do While j <= 10
    strID = "ID" & j
    MsgBox strID
    j = j + 1
Loop
      
End Sub
 
A word of warning is appropriate.

Concatenating identifiers such as ID1, ID2, ... ID10 is only possible and meaningful in a context where that name is dynamically re-evaluated.

For instance, if you had a table IDTABLE with 10 fields named ID1..ID10, you could use this identifier with

TableDefs("IDTABLE").Fields("ID"&CStr(j) ) (with appropriate loop controls for J from 1 to 10)

Of course, if you did this, odds are that you would have been working with badly denormalized data not even in 1st Normal Form (1NF).

In other contexts where identifiers are NOT dynamically evaluated, such as VBA code sequences working with variable names not part of a collection, this operation has no syntactic meaning.

In fact, if in your example ID isn't a pointer or the base element of an array, then your Java code is not syntactically correct either.
 

Users who are viewing this thread

Back
Top Bottom