1

i am trying to concatenate a variable into an object name in the code. I have an object named cmdopt29collapse, and i am trying to replace the 29 with a variable, but simply doing cmdopt & 'x' & collapse, isnt working, what am i missing?

Dim spaceBetween As Integer, origTop As Integer

If cmdOpt29Collapse.Caption = "-" Then
    cmdOpt29Collapse.SetFocus
    Opt29File_List_subform.Visible = False
    spaceBetween = LblOpt28.Top - Opt29File_List_subform.Top - Opt29File_List_subform.Height
    origTop = LblOpt28.Top
    LblOpt28.Top = lblOpt29.Top + lblOpt29.Height + spaceBetween
    CmdOpt28Collapse.Top = CmdOpt28Collapse.Top - (origTop - LblOpt28.Top)
    Box28.Top = Box28.Top - (origTop - LblOpt28.Top)
    Opt28File_List_subform.Top = LblOpt28.Top + LblOpt28.Height
    'move 27
    spaceBetween = lblOpt27.Top - Opt28File_List_subform.Top - Opt28File_List_subform.Height
    origTop = lblOpt27.Top
    lblOpt27.Top = LblOpt28.Top + LblOpt28.Height + spaceBetween
    CmdOpt27Collapse.Top = CmdOpt27Collapse.Top - (origTop - lblOpt27.Top)
    Box27.Top = Box27.Top - (origTop - lblOpt27.Top)
    Opt27File_List_subform.Top = lblOpt27.Top + lblOpt27.Height
    cmdOpt29Collapse.Caption = "+"
Else
    spaceBetween = LblOpt28.Top - lblOpt29.Top - lblOpt29.Height
    origTop = LblOpt28.Top
    LblOpt28.Top = Opt29File_List_subform.Top + Opt29File_List_subform.Height + spaceBetween
    CmdOpt28Collapse.Top = CmdOpt28Collapse.Top + (LblOpt28.Top - origTop)
    Box28.Top = Box28.Top + (LblOpt28.Top - origTop)
    Opt28File_List_subform.Top = LblOpt28.Top + LblOpt28.Height
    Opt29File_List_subform.Visible = True
    'move 27
    spaceBetween = lblOpt27.Top - LblOpt28.Top - LblOpt28.Height
    origTop = lblOpt27.Top
    lblOpt27.Top = Opt28File_List_subform.Top + Opt28File_List_subform.Height + spaceBetween
    CmdOpt27Collapse.Top = CmdOpt27Collapse.Top + (lblOpt27.Top - origTop)
    Box27.Top = Box27.Top + (lblOpt27.Top - origTop)
    Opt27File_List_subform.Top = lblOpt27.Top + lblOpt27.Height
    cmdOpt29Collapse.SetFocus
    cmdOpt29Collapse.Caption = "-"
End If

TL/DR, Long story short, i want to concatenate a variable into an object reference

9
  • 1
    This doesn't sound possible but you should post the code you have so far. Commented Apr 10, 2014 at 17:03
  • 1
    See this similar question... one suggestion is to use the CallByName support.microsoft.com/kb/186143 Commented Apr 10, 2014 at 17:08
  • If your object is actually a control on a form then you can do Me.Controls("cmdopt" & x & "collapse"), too. But if it is a variable seems like you are SOL unless you make it a property and use @DavidZemens link Commented Apr 10, 2014 at 17:11
  • Kind of a circumventing way to do it, but still it comes up with the string you want it to be. You'd have to do all the programming required with your variable, but it's a start. Posted below :) Commented Apr 10, 2014 at 17:25
  • i added the code, basically i am trying to be able to pass it a variable for whichever option i am on, so in this chunk of code id pass it 29 and and then have another variable set as y=x-1, and have 'y' take the place of every '28' in that code. Eventually it will be have to move about 10 things at a time instead of just 2, so i want to be able to just pass one parameter instead of 50(id have to pass all the full object names), and have the other variables set up as equations to get the correct number (always decreasing by one) Commented Apr 10, 2014 at 18:48

1 Answer 1

0

Here's what I came up with. This user form is simple.

It contains: TextBox1, which reads: "cmdopt29collapse" TextBox2, which initially reads: "cmdopt29collapse" CommandButton1

Here is the code:

Private Sub CommandButton1_Click()

Dim x As String
x = "ponies"
TextBox2.Text = Replace$("cmdopt29collapse", "29", x)

End Sub


Private Sub UserForm_Activate()

TextBox2.Text = TextBox1.Text

End Sub

If your variable is an integer, try this:

Private Sub CommandButton1_Click()

Dim x As Integer
x = 12
TextBox2.Text = Replace$("cmdopt29collapse", "29", x)

End Sub


Private Sub UserForm_Activate()

TextBox2.Text = TextBox1.Text

End Sub

Now this isn't actually concatenating the new name, it's replacing it. But then you can take TextBox2.Text and use it for whatever you'd like. Hope this helps!

If you don't want this userform to actually show you can use:

UserForm1.Hide
Userform2.Show vbModal 

So your form would look something like this:

Private Sub CommandButton1_Click()

Dim x As Integer
x = 12
TextBox2.Text = Replace$("cmdopt29collapse", "29", x)

End Sub


Private Sub UserForm_Activate()

TextBox2.Text = TextBox1.Text
UserForm1.Hide
UserForm2.Show vbModal

End Sub
Sign up to request clarification or add additional context in comments.

1 Comment

Ah, one last thing. I set the initial TextBox1 = cmdopt29collapse, already posted in to the UserForm's textbox. Under UserForm_Activate, you could have "TextBox1.Text = "cmdopt29collapse"" :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.