在代码中连接对象名称内的变量

时间:2014-04-10 16:49:44

标签: vba ms-access concatenation

我试图将变量连接到代码中的对象名称。我有一个名为cmdopt29collapse的对象,我试图用变量替换29,但只是做cmdopt& ' X' &安培;崩溃,不工作,我缺少什么?

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,长话短说,我想将变量连接到对象引用

1 个答案:

答案 0 :(得分:0)

这就是我想出的。这个用户表单很简单。

它包含: TextBox1,内容为:“cmdopt29collapse” TextBox2,最初读作:“cmdopt29collapse” CommandButton1的

以下是代码:

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

如果您的变量是整数,请尝试:

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

现在这实际上并没有连接新名称,它正在取代它。但是,您可以使用TextBox2.Text并将其用于您想要的任何内容。希望这有帮助!

如果您不希望此用户表单实际显示,您可以使用:

UserForm1.Hide
Userform2.Show vbModal 

所以你的表格看起来像这样:

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