Andi Hawksley

Create Calculated Dataset and Add To Trend (VBA)

Discussion created by Andi Hawksley on Sep 5, 2014
Latest reply on Apr 6, 2016 by dhollebeek

 I am trying to create a new calculated dataset in VBA and then programmatically adding it to an existing trend.


Using the example in VBA Language Reference help, I came up with this code:

  Dim MyDatasets As Datasets
  Dim MyDataset As PIExpressionDataset
  Dim MyDataPoints As DataPoints
  Dim MyDataPoint As DataPoint
  Dim MyPlaceHolders As Placeholders
  Dim MyPlaceHolder As Placeholder
  Dim MyColumn As String
  Dim MyColumns As Columns
  Dim MyDisplay As Display
  Dim MyDisplays As Displays
  Dim MySymbols As Symbols
  Dim MyTrend As Trend
  Dim MyTrace As Trace
  Dim Application As Object
  Set MyDatasets = Application.Datasets
  Set MyDataset = MyDatasets.Add("NewCalc", Nothing, True, 1, True, pbDatasetPIExpression)
  Set MyDataset = MyDatasets.GetDataset("NewCalc")
  MyDataset.Expression = "If TagVal('TAG','*') = DESIREDSTATE Then 'TAGTODISPLAY' Else NoOutput()"
  MyDataset.RefreshInterval = 3600
  MyDataset.ColumnName = "Value"
  MyDataset.Interval = "10m"
  MyDataset.Description = "Calculated Dataset"
  MyDatasets.SetDataset MyDataset

  Set MyPlaceHolders = MyDataset.Placeholders
  Set MyPlaceHolder = MyPlaceHolders.Item(1)
  MyPlaceholder.SetValue("Value1", pbcPlaceholderText)
  Set MyColumns = MyDataset.Columns
  MyColumn = MyColumns(1)
  Set MyDisplays = Application.Displays
  Set MyDisplay = MyDisplays.Item(1)
  Set MySymbols = MyDisplay.Symbols
  Set MyTrend = MySymbols.Item("Trend1")
  MyTrend.AddTrace (MyColumn)
  MyTrend.CurrentTrace = "MyTrace"
  Set MyDataPoint = MyTrend.DataPoint
  Set MyPlaceHolders = MyDataPoint.Placeholders
  Set MyPlaceHolder = MyPlaceHolders.Item(1)
  MyPlaceHolder.Value = "NewValue"

However, I encounter multiple errors with this. I'm not all that sure my dataset I create is completely valid. 

It doesn't like the syntax MyPlaceholder.SetValue line, it is requiring an expression of the form MyPlaceholder.SetValue() = __.  Also, VBA is saying the I haven't defined what a trace was (Dim MyTrace As Trace).  The final major issue I am having with this is the last line (MyPlaceHolder.Value = "NewValue") is saying "Can't assign to read-only property". 



Could someone help me come up with a functioning VBA code to get the dataset into a graph?