Consulting

Results 1 to 4 of 4

Thread: Solved: concatenating a objectname with a variable

  1. #1
    VBAX Regular
    Joined
    Jan 2006
    Posts
    11
    Location

    Solved: concatenating a objectname with a variable

    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
    [vba]frmDemo!newLabel_ & newLabelNumber.caption="something"[/vba]

    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!
    [vba]frmDemo!newLabel_1.caption="something"[/vba]

    Can anyone point me in the right direction?
    thanx!

    Regards,
    Casper
    The Netherlands
    Last edited by Koesper; 01-24-2006 at 01:57 AM. Reason: I already found a typo, i'm certain you'll find more

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Koesper
    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
    [vba]frmDemo!newLabel_ & newLabelNumber.caption="something"[/vba]

    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!
    [vba]frmDemo!newLabel_1.caption="something"[/vba]

    Can anyone point me in the right direction?
    thanx!

    Regards,
    Casper
    The Netherlands
    Try something like

    [vba]
    frmDemo.Controls("newLabel_" & newLabelNumber).Caption = "something"
    [/vba]

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

  3. #3
    VBAX Regular
    Joined
    Jan 2006
    Posts
    11
    Location
    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!

    [VBA]set myNewLabel = frmDemo.Controls("newLabel_" & newLabelNumber)
    myNewLabel.caption = "something"[/VBA]

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

  4. #4
    VBAX Regular
    Joined
    Jan 2006
    Posts
    11
    Location
    HaHa, while i was writing my own solution XLD also found the anwser for me.

    thanx anyway!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •