PIModules Example

See the IPIVersion example for details on using the PIModule Version property.

 

Option Explicit
  
Private Sub Command1_Click()
   SimpleAddAndRemove
   cmdTest2_Click
   cmdClear1And2_Click
End Sub

Private Sub SimpleAddAndRemove()
  
   Dim Module As PIModule
   Dim SubModule As PIModule
   Dim Modules As PIModules
   Dim ModuleDb As PIModuleDB
  
   Set ModuleDb = PISDK.Servers.DefaultServer.PIModuleDB
  ' Add a module to the root PIModules Collection
   On Error Resume Next
   Set Module = ModuleDb.PIModules.Add("Example_One")
   Set Module = ModuleDb.PIModules.Item("Example_One")
  ' Add a module to this modules collection
   Set SubModule = Module.PIModules.Add("Example_One_Sub")
   Set SubModule = Module.PIModules.Item("Example_One_Sub")
   On Error GoTo 0
  ' Now remove it
   Module.PIModules.Remove SubModule
End Sub
  
Private Sub cmdTest2_Click()
  
  Dim TempModule As PIModule
  Dim TestRoot As PIModule
  Dim Parent1 As PIModule
  Dim Parent2 As PIModule
  Dim ChildA As PIModule
  Dim ChildB As PIModule
  Dim ChildAFrom2 As PIModule
  Dim ChildBFrom1 As PIModule
  Dim PListFromA As PIModuleList
  Dim PListFromB As PIModuleList
  
  On Error Resume Next
  Set TempModule = PISDK.Servers.DefaultServer.PIModuleDB.PIModules.Add("Examples")
  Set TempModule = PISDK.Servers.DefaultServer.PIModuleDB.PIModules.Item("Examples")
  Set TestRoot = TempModule.PIModules.Add("InsertTest")
  Set TestRoot = TempModule.PIModules.Item("InsertTest")
  Set Parent1 = TestRoot.PIModules.Add("Parent1")
  Set Parent1 = TestRoot.PIModules.Item("Parent1")
  Set Parent2 = TestRoot.PIModules.Add("Parent2")
  Set Parent2 = TestRoot.PIModules.Item("Parent2")
  Set ChildA = Parent1.PIModules.Add("ChildA")
  Set ChildA = Parent1.PIModules.Item("ChildA")
  Set ChildB = Parent2.PIModules.Add("ChildB")
  Set ChildB = Parent2.PIModules.Item("ChildB")
  Parent1.PIModules.Insert ChildB
  Parent2.PIModules.Insert ChildA
  
  ' Create circular reference. This better fail
  Err.Clear
  ChildA.PIModules.Insert Parent1
  If Err <> 0 Then
	MsgBox "Error Inserting Parent1: " & Err.Description
  End If
  Err.Clear
  Set ChildAFrom2 = Parent2.PIModules.Item("ChildA")
  If Err <> 0 Then
	MsgBox "Error: " & Err.Description
  End If
  Err.Clear
  Set ChildBFrom1 = Parent1.PIModules.Item("ChildB")
  If Err <> 0 Then
  	MsgBox "Error: " & Err.Description
  End If
End Sub
  
Private Sub cmdClear1And2_Click()
  
  Dim TempModule As PIModule
  Dim TestRoot As PIModule
  Dim Parent1 As PIModule
  Dim Parent2 As PIModule
  Dim ChildA As PIModule
  Dim ChildB As PIModule
  Dim ChildAFrom2 As PIModule
  Dim ChildBFrom1 As PIModule
  Dim PListFromA As PIModuleList
  Dim PListFromB As PIModuleList
  
  On Error Resume Next
  Set TempModule = PISDK.Servers.DefaultServer.PIModuleDB.PIModules.Add("Examples")
  Set TempModule = PISDK.Servers.DefaultServer.PIModuleDB.PIModules.Item("Examples")
  Set TestRoot = TempModule.PIModules.Add("InsertTest")
  Set TestRoot = TempModule.PIModules.Item("InsertTest")
  Set Parent1 = TestRoot.PIModules.Add("Parent1")
  Set Parent1 = TestRoot.PIModules.Item("Parent1")
  Set Parent2 = TestRoot.PIModules.Add("Parent2")
  Set Parent2 = TestRoot.PIModules.Item("Parent2")
  Set ChildA = Parent1.PIModules.Add("ChildA")
  Set ChildA = Parent1.PIModules.Item("ChildA")
  Set ChildB = Parent2.PIModules.Add("ChildB")
  Set ChildB = Parent2.PIModules.Item("ChildB")
  Parent1.PIModules.Insert ChildB
  Parent2.PIModules.Insert ChildA
  Err.Clear
  Set ChildAFrom2 = Parent2.PIModules.Item("ChildA")
  If Err <> 0 Then
	MsgBox "Error: " & Err.Description
  End If
  Err.Clear
  Set ChildBFrom1 = Parent1.PIModules.Item("ChildB")
  If Err <> 0 Then
  	MsgBox "Error: " & Err.Description
  End If
  Set PListFromA = ChildA.ParentList
  Set PListFromB = ChildB.ParentList
  Parent1.PIModules.Remove ChildA
  Parent2.PIModules.Remove ChildB
  Parent2.PIModules.Remove ChildA
  Parent1.PIModules.Remove ChildB
  TestRoot.PIModules.Remove Parent2
  TestRoot.PIModules.Remove Parent1
End Sub
Enabling Operational Intelligence