Dynamic variable name

M

Magnolia

Hello,
I would like to dynamically initialize controls in my form. I have a
group of controls that have a name that starts by txtquantity. So I
have:

txtquantity1
txtquantity2
txtquantity3
etc

To set the value of those fields, the easy solution would be to do:
txtquantity1.value = toto1
txtquantity2.value = toto2
txtquantity3.value = toto3
etc

But I would prefer to use a while to dynamically create the command
line and to use something like this:

While intLevel <= intnbNiveau
txtQuantity & intLevel = "toto" & cstr(intLevel)
intLevel = intLevel + 1
Wend

But that code does not work. What would you recommend??? I have looked
on the newsgroup and I can't find something that works.
Thanks a lot in advance !!!
Nancy
---------------------------------------------
By the way, I have also tried the Eval function. In order to keep it
simple to start, I did the following test:
Step1 :
Forms!frmAddProductToPriceList!txtQuantity1 = 88

This works. It has set my field to the value 88.

Step 2: I have replaced my line by:
Eval ("Forms!frmAddProductToPriceList!txtQuantite1 = cstr(88)")

The field stayed empty !?!!??!?!?!
 
G

George Nicholson

One approach:

Dim varArray As Variant
Dim i As Integer
varArray = Array(toto1, toto2, toto3)

For i = lbound(varArray) to ubound(varArray)
Me.Controls(txtQuantity" & i ) = varArray(i)
Next i

The above assumes "Option Base 1" is at top of module (so i = 1 to 3). If
unstated or using the default "Option Base 0" (so i = 0 to 2), then change
to:
Me.Controls("txtQuantity" & i +1 ) = varArray(i)


HTH,
 
M

mscertified

You can use Eval function, look it up in HELP
For i = 1 to 10
Eval("txtquantity" & i) = "whatever"
Next

-Dorian
 
O

OldPro

Hello,
I would like to dynamically initialize controls in my form. I have a
group of controls that have a name that starts by txtquantity. So I
have:

txtquantity1
txtquantity2
txtquantity3
etc

To set the value of those fields, the easy solution would be to do:
txtquantity1.value = toto1
txtquantity2.value = toto2
txtquantity3.value = toto3
etc

But I would prefer to use a while to dynamically create the command
line and to use something like this:

While intLevel <= intnbNiveau
txtQuantity & intLevel = "toto" & cstr(intLevel)
intLevel = intLevel + 1
Wend

But that code does not work. What would you recommend??? I have looked
on the newsgroup and I can't find something that works.
Thanks a lot in advance !!!
Nancy
---------------------------------------------
By the way, I have also tried the Eval function. In order to keep it
simple to start, I did the following test:
Step1 :
Forms!frmAddProductToPriceList!txtQuantity1 = 88

This works. It has set my field to the value 88.

Step 2: I have replaced my line by:
Eval ("Forms!frmAddProductToPriceList!txtQuantite1 = cstr(88)")

The field stayed empty !?!!??!?!?!

The first part can be done like this:
me("txtQuantity" & num).text=
The second part can't be done with enum or any other indirect
assignment. How do you load the variables? Can you load
me("txtQuantity" & num).text the same way?
Another option is to create a function that returns the value and call
the function with enum.
 
M

Marshall Barton

Magnolia said:
I would like to dynamically initialize controls in my form. I have a
group of controls that have a name that starts by txtquantity. So I
have:

txtquantity1
txtquantity2
txtquantity3
etc

To set the value of those fields, the easy solution would be to do:
txtquantity1.value = toto1
txtquantity2.value = toto2
txtquantity3.value = toto3
etc

But I would prefer to use a while to dynamically create the command
line and to use something like this:

While intLevel <= intnbNiveau
txtQuantity & intLevel = "toto" & cstr(intLevel)
intLevel = intLevel + 1
Wend


Eval is a function that returns a value. Thus it can only
be used in an expression. It does not construct, parse,
compile and execute a statement.

Also, Eval is unaware of VBA variables, so if toto1 is a VBA
variable, Eval will not be able to evaluate something like
Eval("toto" & intLevel). The only VBA entity that Eval
recognizes are Public Functions in standard modules. So,
you could define them:
Public Function toto1()
toto1 = "ABC"
End Function
and then use Eval this way:
Eval("toto" & intLevel & "()")
but this seems like it's more trouble than it's worth.
Seems like an array would be far easier:
Dim toto As Variant
toto = Array("ABC", "DEF", "GHI")
so you can use toto(intLevel-1)
You didn't say what the "variables" are so this paragraph
may be irrelevant to your question.

You can use:
For intLevel = 1 To intnbNiveau
Me("txtquantity" & intLevel) = toto1
Next intLevel

but without a explanation of the toto thingies, that's only
half an answer.

Note that the Text property is not appropriate here.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top