PDA

View Full Version : Solved: concatenating a objectname with a variable



Koesper
01-24-2006, 01:56 AM
Hi!

Until 2 weeks ago i was a complete newbie to VBA, but i've been trying to build a little something in excel to do several magical things to a worksheet filled with adresses. I've learned a lot so far, but i've still got a lot of questions... (who hasn't?)

But here is the one i'm currently fighting with:

My VBA dynamically adds new objects (labels) on the userform frmDemo, but in a different sub i want to do something with those new objects.
Their names are built up as a prefix "newLabel_" and a number.

in the other sub all the numbers are stored in newComboNumber, and from there i want to reference to those new comboboxes, but how?

could i do something like
frmDemo!newLabel_ & newLabelNumber.caption="something"

If i do this, i get the error that the object cannot be found,
while if i write the objectname without concatenating it with the number stored in the variable, it works like a charm!
frmDemo!newLabel_1.caption="something"

Can anyone point me in the right direction?
thanx!

Regards,
Casper
The Netherlands

Bob Phillips
01-24-2006, 02:36 AM
Hi!

Until 2 weeks ago i was a complete newbie to VBA, but i've been trying to build a little something in excel to do several magical things to a worksheet filled with adresses. I've learned a lot so far, but i've still got a lot of questions... (who hasn't?)

But here is the one i'm currently fighting with:

My VBA dynamically adds new objects (labels) on the userform frmDemo, but in a different sub i want to do something with those new objects.
Their names are built up as a prefix "newLabel_" and a number.

in the other sub all the numbers are stored in newComboNumber, and from there i want to reference to those new comboboxes, but how?

could i do something like
frmDemo!newLabel_ & newLabelNumber.caption="something"

If i do this, i get the error that the object cannot be found,
while if i write the objectname without concatenating it with the number stored in the variable, it works like a charm!
frmDemo!newLabel_1.caption="something"

Can anyone point me in the right direction?
thanx!

Regards,
Casper
The Netherlands

Try something like


frmDemo.Controls("newLabel_" & newLabelNumber).Caption = "something"


but adding controls to a userform dynamically is not a good idea, it is inefficient, it is difficult to maintain, etc.etc.

Koesper
01-24-2006, 02:42 AM
found it!

in the microsoft helpfile i found that i had to use the 'frmDemo!newLabel_1' notation, but somewhere else i found that this was only a shorter notation for
'frmDemo.Controls("newLabel_1")'

this inspired me to try this
'frmDemo.Controls("newLabel_" & newLabelNumber)'

and now it works!

set myNewLabel = frmDemo.Controls("newLabel_" & newLabelNumber)
myNewLabel.caption = "something"

Problem solved!
(unless someone has an even better solution)

Koesper
01-24-2006, 02:43 AM
HaHa, while i was writing my own solution XLD also found the anwser for me.

thanx anyway!