PIUsers Example

picture

This example, during the Form_Load, connects to the default server, displays the server name, and displays the server's user list. When a user in the list is clicked on, it displays the selected user's description, default context, current context and the user's groups.

Build the PIUsers 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.

Private Sub Form_Load()
On Error GoTo ErrorHandler
  'Connect to the default server and show its name
  Dim srv As Server
  Set srv = Servers.DefaultServer
  srv.Open
  Label1.Caption = srv.Name
  'Fill the first list box with the users
  Dim usr As PIUser
  List1.Clear
    For Each usr In srv.PIUsers
    List1.AddItem usr.Name
  Next
  ' select the first item to get click event and fill groups
  List1.ListIndex = 0
Exit Sub
ErrorHandler:
  MsgBox Err.Description & vbCrLf & Hex(Err.Number)

End Sub

Private Sub List1_Click()
On Error GoTo ErrorHandler
  ' Put the selected user's PIGroups in list2
  Dim srv As Server
  Set srv = Servers.DefaultServer
  Dim usr As PIUser
  ' the user
  Set usr = srv.PIUsers(List1.Text)
  Dim grp As PIGroup
  List2.Clear
  For Each grp In usr.PIGroups
    List2.AddItem grp.Name
  Next
  ' Show the description, and context strings
  Label2.Caption = usr.Description
  Label3.Caption = usr.DefaultContext
  Label4.Caption = usr.CurrentContext

Exit Sub
ErrorHandler:
  MsgBox Err.Description & vbCrLf & Hex(Err.Number)
End Sub
Enabling Operational Intelligence