Add / Remove PIUser Example

picture

 

This example, during the Form_Load, connects to the default server, displays the server name, and fills list boxes with the server's users, the server's contexts, and the server's groups. Users are removed by selecting the desired user from the list box (List1) and hitting the button Command1. The list of users is refreshed if the removal is successful. To add a user, fill in the User name, Description, and Password text boxes as desired, select a context from the context list box (List2) and the desired Groups from the Group list box (List3) and hit the button Command2. The list of users is refreshed if the add is successful.

 

Build the Add/Remove PIUser 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, ...;) match those shown.

3. Make List3 a multiselect list box by setting its MultiSelect property to 1

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

Option Explicit
Dim srv As Server

Private Sub Command1_Click()
'Removing a user
On Error GoTo ErrorHandler
  If List1.Text = "" Then
    MsgBox "Please select a user to remove"
  End If
  srv.PIUsers.Remove List1.Text
  RefreshUserList
Exit Sub
ErrorHandler:
  MsgBox Err.Description & vbCrLf & Hex(Err.Number)
End Sub

Private Sub Command2_Click()
'Adding a user
On Error GoTo ErrorHandler
' Make sure a user name was entered
If Text1.Text = "" Then
  MsgBox "Please enter a user name to be added"
  Exit Sub
End If
' Build a named values collection of the selected groups
Dim nvGroups As New NamedValues
Dim i As Integer
For i = 0 To List3.ListCount - 1
  If List3.Selected(i) Then
    nvGroups.Add List3.List(i), 0
  End If
Next
' Add the user
srv.PIUsers.Add Text1.Text, Text2.Text, Text3.Text, nvGroups, List2.Text
RefreshUserList
Exit Sub
ErrorHandler:
  MsgBox Err.Description & vbCrLf & Hex(Err.Number)
End Sub

Private Sub Form_Load()
On Error GoTo ErrorHandler
  Set srv = Servers.DefaultServer
  ' You may need to add a password specificaton here - PWD=xxx
  srv.Open "UID=piadmin"
  ' Show which server we are connected to
  Label1.Caption = srv.Name
  ' Fill List1 with all the users on the server
  Dim usr As PIUser
  For Each usr In srv.PIUsers
    List1.AddItem usr.Name
  Next
  List1.ListIndex = 0
  ' Clear the text boxes for adding a new user
  Text1.Text = ""
  Text2.Text = ""
  Text3.Text = ""
  ' Fill List2 with all the contexts on the server
  Dim ctx As PIContext
  For Each ctx In srv.PIContexts
    List2.AddItem ctx.Name
  Next
  List2.ListIndex = 0
  ' Fill List3 with all the groups on the server
  Dim grp As PIGroup
  For Each grp In srv.PIGroups
    List3.AddItem grp.Name
  Next
  List3.ListIndex = 0
Exit Sub
ErrorHandler:
  MsgBox Err.Description & vbCrLf & Hex(Err.Number)
End Sub
Private Sub RefreshUserList()
On Error GoTo ErrorHandler
  List1.Clear
  Dim usr As PIUser
  For Each usr In srv.PIUsers
    List1.AddItem usr.Name
  Next
  If List1.ListCount > 0 Then
    List1.ListIndex = 0
  End If
Exit Sub
ErrorHandler:
  MsgBox Err.Description & vbCrLf & Hex(Err.Number)
End Sub
Enabling Operational Intelligence