I'm pretty new to this and VBA coding, but I am trying to figure if I can shorten my code by using a loop...
I have several ComboBoxes, TextBoxes, Text on my display (ComboBox1, ComboBox2, ComboBox3 ... ComboBox10 and the same for TextBox1, Text1 etc..) which I want to get the value of or write to.
Currently my VBA Macro is similar to the following below:
Set svr = Servers("Myserver") tagname = "something1" Set tag = svr.PIPoints(tagname) tagvalue = tag.Data.Snapshot ThisDisplay.Text1.Contents = tagvalue ThisDisplay.ComboBox1.Value = ThisDisplay.Text1.Contents somevalue123 = ThisDisplay.TextBox1.Value tagname = "something2" Set tag = svr.PIPoints(tagname) tagvalue = tag.Data.Snapshot ThisDisplay.Text2.Contents = tagvalue ThisDisplay.ComboBox2.Value = ThisDisplay.Text2.Contents somevalue123 = ThisDisplay.TextBox2.Value tagname = "something3" Set tag = svr.PIPoints(tagname) tagvalue = tag.Data.Snapshot ThisDisplay.Text3.Contents = tagvalue ThisDisplay.ComboBox3.Value = ThisDisplay.Text3.Contents somevalue123 = ThisDisplay.TextBox3.Value
I am wondering if it is possible to use VBA in PI to do something like a loop to loop through the ComboBoxes, TextBoxes and more. I am pretty new to this and could not really figure out if it was possible or not.
For i = 1 to 3 tagname = "something" & i Set tag = svr.PIPoints(tagname) tagvalue = tag.Data.Snapshot ThisDisplay.Text(i).Contents = tagvalue ThisDisplay.Combobox(i).Value = ThisDisplay.Text(i).Contents somevalue123 = ThisDisplay.TextBox(i).Value Next i
I am not sure the context of this VBA project, but it looks like you're using the PI SDK. The PI SDK has an object called a "PointList". I would recommend adding all your points to a PointList, then using a "For Each" statement to walk the Points in your PointList instead of using a For loop.
I hope this helps!