0
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.

Harun24hr
  • 30,391
  • 4
  • 21
  • 36
  • With your intelliSense turned on, you should see the name of the textboxes after you type the dot. attrNameTB should show when this is a textbox on your form. Another hint is to start debugging and use watches... – Aldert Jan 24 '23 at 06:56
  • 1
    Not sure if you are trying `trailForm.Controls(attrTB).Text = attr`? – Siddharth Rout Jan 24 '23 at 08:06

1 Answers1

1

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
FunThomas
  • 23,043
  • 3
  • 18
  • 34
  • `You cannot use VBA like that` - until you [can](https://stackoverflow.com/q/5706791/11683) ;) – GSerg Jan 24 '23 at 10:53
  • 1
    I like to play with such attempts - for fun. But I cannot think about a real use of that... – FunThomas Jan 24 '23 at 11:44
  • Thank you Thomas, I am not sure if it is possible but can I somehow use the string value stored in attr as a variable? So attr is actually a varibale Name as a String. Example attr = "trailName" and trailName = "actualTrailName". Can I somehow get "actualTrailName" from attr? I don't know much about VBA just trying to automate some of my work. Thanks for the help :) – Lakshay Sharma Jan 24 '23 at 13:13
  • @LakshaySharma It is here a variable as string. – GSerg Jan 24 '23 at 13:17
  • @GSerg Yes attr is a String with a varibale name – Lakshay Sharma Jan 24 '23 at 13:23
  • I figured it out, I just saved all the variables in a Dictionary and used the following line of code. trailForm.Controls(attrTB).Value = Ats(attr) where Ats is a dictionary with all the variables and their values. Thanks alot guys – Lakshay Sharma Jan 24 '23 at 13:53