1 Reply Latest reply on May 2, 2018 2:29 PM by lmalone

    Loop through ComboBox/TextBox and Update or Get Value using VBA

    curiouspi

      Hi All,

       

      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
      

       

       

      Thanks!