PIGroups Example

 

This example, during the Form_Load, connects to the default server, displays the server name, and fills a list box with the server's user groups (PIGroup objects). Clicking on a group in the list box displays its description (Label2) and the list of users in the group (List2). Groups can be removed from the server by selecting the desired PIGroup from the list box (List1) and hitting the button Command1. The list of user groups is refreshed if the removal is successful. To add a PIGroup, fill in the name and description text boxes as desired and hit the button Command2. The list of groups is refreshed if the add is successful.

 

Build the PIGroups example as follows:

1. Create a new project and add a reference to the PISDK library.

2. Add the visual elements as shown in the picture. Make sure the numbered element names (label1, list1, &ldots;) match those shown.

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

   Option Explicit
Dim srv As Server
Private Sub Command1_Click()
On Error GoTo ErrorHandler
  If List1.Text = "" Then
    MsgBox "Please select a PIGroup to remove."
    Exit Sub
  End If
srv.PIGroups.Remove List1.Text
RepaintGroups
Exit Sub
ErrorHandler:
  MsgBox Err.Description & vbCrLf & Hex(Err.Number)

End Sub

Private Sub Command2_Click()
On Error GoTo ErrorHandler
  If Text1.Text = "" Then
    MsgBox "Please enter a group name to add"
    Exit Sub

  End If
  srv.PIGroups.Add Text1.Text, Text2.Text
  RepaintGroups
Exit Sub
ErrorHandler:
  MsgBox Err.Description & vbCrLf & Hex(Err.Number)

End Sub

Private Sub Form_Load()
On Error GoTo ErrorHandler
  Set srv = Servers.DefaultServer
  ' Open with a user with enough permission to add groups
  srv.Open "UID=piadmin"
  ' Fill the list of groups
  Dim grp As PIGroup
  For Each grp In srv.PIGroups
    List1.AddItem grp.Name
  Next
  List1.ListIndex = 0

  Text1.Text = ""

  Text2.Text = ""
  Label1.Caption = srv.Name
  Label2.Caption = ""
Exit Sub
ErrorHandler:
  MsgBox Err.Description & vbCrLf & Hex(Err.Number)
End Sub

Private Sub List1_Click()
On Error GoTo ErrorHandler
  'Show the description for this group
  Dim grp As PIGroup
  Set grp = srv.PIGroups.Item(List1.Text)
  Label2.Caption = grp.Description
  'Show the PIUsers for this group
  Dim usr As PIUser
  List2.Clear
  For Each usr In grp.PIUsers

    List2.AddItem usr.Name
  Next
Exit Sub
ErrorHandler:
  MsgBox Err.Description & vbCrLf & Hex(Err.Number)

End Sub
Private Sub RepaintGroups()
  List1.Clear

  Dim grp As PIGroup
  For Each grp In srv.PIGroups
    List1.AddItem grp.Name
  Next
End Sub
Enabling Operational Intelligence