ArcValue / RetrievalAttributes Example

 

 

 

This example, during the Form_Load, sets a variable to the default server, and displays the server name. It clears the labels and text boxes and initializes the combo box with the available retrieval types using a PIConstant collection. When Command1 is pressed it checks that something has been entered into the tag name field and the date/time field, converts the date text into a PITimeFormat and loads up a NamedValues collection with the RetrievalAttributes checked by the user.  It then calls for the archive value at the specified date/time using the retrieval type selected by the user.  The results are then displayed in the label fields.

 

Build the ArcValue example as follows:

1. Create a new project and add references to the PISDK libraries.

2. Add the visual elements as shown in the picture. Make sure the numbered element names (labels, text boxes, buttons) match those shown.  The check boxes are a control array as are the three Label12 labels.

3. Cut and paste the code below into the project.

When run the program will appear as follows:

 

Option Explicit
' An array of retrieval attribute names for convenience
Dim RetAttrNames(5) As String
Dim srv As Server
Private Sub Command1_Click()
Dim pt As PIPoint
On Error GoTo handler
   If Len(Text1.Text) = 0 Then
      MsgBox "Please enter a tag name."
      Exit Sub
   End If
   If Len(Text2.Text) = 0 Then
      MsgBox "Please enter a time."
      Exit Sub
   End If
   Dim dt As New PITimeFormat
   dt.InputString = CStr(Text2.Text)
   Set pt = srv.PIPoints(Text1.Text)
   ' Set the desired retrieval attributes
   Dim nvRetAttrs As New NamedValues
   Dim i As Integer
   For i = 0 To 4
      If Check1(i).Value = 1 Then
         nvRetAttrs.Add RetAttrNames(i), 1
      End If
   Next
   Set pt.Data.RetrievalAttributes = nvRetAttrs
   ' Get the archive value
   Dim pv As PIValue
   ' get the retrieval type
   Dim rttype As RetrievalTypeConstants
   rttype = PIConstants("RetrievalTypeConstants").Item(Combo1.Text).Value
   Set pv = pt.Data.ArcValue(dt, rttype)
   ' Show it
   Label10.Caption = CStr(pv.TimeStamp.LocalDate)
   Label11.Caption = CStr(pv.Value)
   ShowRetrievalAttributes pv
Exit Sub
handler:
MsgBox Err.Description
End Sub

Private Sub Form_Load()
On Error GoTo handler
   Set srv = Servers.DefaultServer
   Label2.Caption = srv.Name
   ClearFields
   RetAttrNames(0) = "Annotated"
   RetAttrNames(1) = "Substituted"
   RetAttrNames(2) = "Questionable"
   RetAttrNames(3) = "Annotations"
   RetAttrNames(4) = "All"
   ' fill the retrieval type combo box
   Dim pc As PIConstant
   Set pc = PIConstants("RetrievalTypeConstants")
   Dim nv As NamedValue
   For Each nv In pc
      Combo1.AddItem nv.Name
   Next
   Combo1.ListIndex = 0
Exit Sub
handler:
MsgBox Err.Description
End Sub

Private Sub ClearFields()
   Text1.Text = ""
   Text2.Text = ""
   Label10.Caption = ""
   Label11.Caption = ""
   Label12(0).Caption = ""
   Label12(1).Caption = ""
   Label12(2).Caption = ""
End Sub
Private Sub ShowRetrievalAttributes(pv As PIValue)
On Error GoTo handler
   Dim i As Integer
   Dim nvAttr As NamedValue
   Dim vtAttrVal As Variant
   For i = 0 To 3
      ' Did we ask for it?
      On Error Resume Next
      Set nvAttr = pv.ValueAttributes(RetAttrNames(i))
      If Err.Number <> 0 Then
         Err.Clear
         Label12(i).Caption = "N/A"
      Else
         On Error GoTo handler
         If VarType(nvAttr.Value) = vbBoolean Then
           If nvAttr.Value Then
             Label12(i).Caption = "True"
           Else
             Label12(i).Caption = "False"
           End If
         Else
           Label12(i).Caption = nvAttr.Value
         End If
      End If
   Next
   Exit Sub
handler:
   MsgBox Err.Description
End Sub

 

Enabling Operational Intelligence