Servers Example

 

This example displays the servers from the Known Servers Table in a list box. When a server is selected from the list box, its connection attributes are displayed. The example allows adding new servers to the Known Servers using text boxes and combo boxes for the entry of connection attributes. The example also provides a button for removing the currently selected server from the Known Servers Table.

Build this Servers 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 handler
  ' Validate entered data
  If Text1.Text = "" Then
    MsgBox "Please enter a local name for the new server."
    Exit Sub
  End If
  If Text2.Text = "" Then
    MsgBox "Please enter a path for the new server."
    Exit Sub
  End If
  If Text3.Text = "" Then
    MsgBox "Please enter a default user name for the new server."
    Exit Sub
  End If
  Dim dflt As String
  If Check1.Value = 1 Then
    dflt = "1"
  Else
    dflt = "0"
  End If
  Dim strConnect As String
  strConnect = "UID=" & Text3.Text & ";path=" & Text2.Text _
     & ";port=" & Combo1.Text & ";default=" & dflt
  Servers.Add Text1.Text, strConnect, Combo2.Text
  MsgBox "Server " & Text1.Text & " successfully added"
  DisplayKnownServers
Exit Sub
handler:
  MsgBox Err.Description
End Sub

Private Sub Command2_Click()
On Error GoTo handler
  If List1.Text = "" Then
    MsgBox "Please select a server to remove"
    Exit Sub
  End If
  Servers.Remove List1.Text
  MsgBox "Server " & List1.Text & " successfully removed"
  DisplayKnownServers
Exit Sub
handler:
  MsgBox Err.Description
End Sub

Private Sub Form_Load()
On Error GoTo handler
  InitDropdowns
  ClearServerAttributes
  ClearNewServerAttributes
  DisplayKnownServers
Exit Sub
handler:
  MsgBox Err.Description
End Sub
Private Sub ClearServerAttributes()
  Label15.Caption = ""
  Label16.Caption = ""
  Label17.Caption = ""
  Label18.Caption = ""
  Label19.Caption = ""
  Label20.Caption = ""
End Sub
Private Sub ClearNewServerAttributes()
  Text1.Text = ""
  Text2.Text = ""
  Text3.Text = ""
  Combo1.ListIndex = 0
  Combo2.ListIndex = 0
  Check1.Value = False
End Sub
Private Sub InitDropdowns()
  Combo1.AddItem "5450", 0
  Combo1.AddItem "545", 1
  Combo2.AddItem "PI3", 0
  Combo2.AddItem "PI2", 1
End Sub

Private Sub List1_Click()
  Set srv = Servers(List1.Text)
  ShowServerProps srv
End Sub
Private Sub ShowServerProps(srv As Server)
  Label15.Caption = srv.Name
  Label16.Caption = srv.Path
  Label17.Caption = srv.DefaultUser
  Label18.Caption = srv.Port
  Label19.Caption = srv.ConnectionType
  If srv.Name = Servers.DefaultServer.Name Then
    Label20.Caption = "Yes"
  Else
    Label20.Caption = "No"
  End If
End Sub

Private Sub DisplayKnownServers()
  List1.Clear
  For Each srv In Servers
    List1.AddItem srv.Name
  Next
End Sub
Enabling Operational Intelligence