Home > OS >  How to use String value as a dot operation in VBA? it is possible?
How to use String value as a dot operation in VBA? it is possible?

Time:01-25

Sub getTrailInfo()
    Dim attr As Variant
    Dim attrTB As String
    attrNames = Split("trailName,trailType,aSideSite,zSideSite,status", ",")
    Dim trailForm As New formTrailInfo
        For Each attr In attrNames
            attrTB = attr   "TB"
             trailForm.attrTB = attr
        Next attr

        trailForm.Show
End Sub

When I run the above code it gives a compilor error: Method or Data not found at line trailForm.attrTB = attr

I have required variables in attrNames String array. I need to put values of these variables in corosponding textboxes in a userForm. The name of Text Box in this userForm is attrNameTB. For example Text box for trailName is trailNameTB.

CodePudding user response:

You cannot use VBA like that.

When you start your code, the compiler will first compile the code and check that you want to access a property named attrTB of your form. This doesn't exist and you will get the error you mentioned.

The compiler cannot wait until your variable attrTB has an actual value and then guess that you don't want a property with that name, but use the content of that variable as property name.

However, every form has a collection of all it's controls (button, edit boxes, combo boxes, labels...), and you can access the members of a collection either by index or by name. As you have the name of the control in attrTB, you could simply write

trailForm.Controls(attrTB).Text = attr
  • Related