尝试使用变量和字符串来设置Label Caption值

时间:2018-04-06 13:48:11

标签: vba variables access

我正在尝试使用Interger变量来设置标签字幕。 这有效......

Public Int_Company_Chosen As Interger

Forms!StartUp.Label_Company_12.Caption = "TrueeBB" 

这些不起作用......

 ("Forms!StartUp.Label_Company_" & Int_Company_Chosen - 1 & ".Caption")
Int_Company_Chosen = 12

strTest1 = ("StartUp.Label_Company_" & Int_Company_Chosen - 1)
Forms!StartUp(strTest1).Caption = "TrueeAZ"

Me(strTest1).Caption = "TrueeZZ"

Forms!StartUp.[(strTest1)].Caption = "TrueeZZ"

感谢y'所有提前!!

2 个答案:

答案 0 :(得分:2)

您需要在窗体的控件集合中引用控件名称:

Sub Test()

    Dim Int_Company_Chosen As Integer

    Int_Company_Chosen = 2

    Forms!Startup.Controls("Label_Company_" & Int_Company_Chosen).Caption = "TrueBB"

End Sub

MSDN Controls Collection

修改:

另一种处理表单控件的方法:

Sub Test2()

    Dim frm As Form
    Dim ctl As Control
    Dim x As Long

    Set frm = Forms!Startup

    For Each ctl In frm.Controls
        If TypeName(ctl) = "Label" Then
            x = x + 1
            ctl.Caption = "Label #" & x
        End If
    Next ctl

End Sub

答案 1 :(得分:0)

我发现了问题。空格导致错误的下划线! 应该......

Me.Controls("Label Company " & Int_Company_Chosen).Caption = "TrueBB"

...不

Me.Controls("Label_Company_" & Int_Company_Chosen).Caption = "TrueBB"

感谢您的帮助!!乔