While you can do this
Dim i As Long, a As Long
Dim myVar As String
a = 0
For i = 1 To 10
myVar = "A" & i & "B"
ActiveDocument.Variables(myVar).Value = i
a = a + ActiveDocument.Variables(myVar).Value
Next i
MsgBox a
You cannot do
Dim i As Long, a As Long
Dim myVar As String
a = 0
For i = 1 To 10
myVar = "A" & i & "B"
a = a + myVar
Next i
MsgBox a
as myVar is a String (even if you declare it as a variant, because of the
"A" and the "B". Neither can you assign a .Value to it directly
The OP talks about having a DB with elements like C1AM, C2AM..C10AM by which
I assume that he means fields like C1AM, C2AM..C10AM. To do what he wants,
the OP has to use the variable name generated by the "A" & i & "B" to get
the value from the database for each field that is represented by a variable
name using something like
a = a + DB.Fields(myVar)
where DB.Fields is a suitable command that will return the value from the
fields of the database
--
Hope this helps.
Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.
Doug Robbins - Word MVP
macropod said:
Hi Canblue,
What is happening that isn't as expected? How are you accessing the
variables' values?
--
Cheers
macropod
[MVP - Microsoft Word]
Canblue said:
I tried that and it does not work as expected. The variables, C1AM, C2AM
etc. has integer. I want to calculate the average of the values. The
number of elements ranges from 12 to 18 then 24. Looking for a blackbox.
How can I step through the DB items.
:
Hi Canblue,
Yes, you certainly can incorporate a counter into a variable name. For
example:
Sub Demo()
Dim i As Integer
Dim myVar As String
For i = 1 To 10
myVar = "A" & i & "B"
MsgBox myVar
Next i
End Sub
--
Cheers
macropod
[MVP - Microsoft Word]
I have a DB with elements like C1AM, C2AM..C10AM, all integers. In
other languages I can concatenate a counter to step through the data
items i.e.
C || counter || AM to cycle through the variables. Is there a way in
VB6 to do something similar? I was not looking at an array but if I
have to go there then so be it. do I = 1 to 10
a = a + C || I || AM
end