IPITransaction Example

 

Option Explicit

Private Sub Command1_Click()
Dim myServer As Server
Dim myModule As PIModule
Dim IPIT As IPITransaction
Dim transUID As Variant
Dim isDirty As Boolean
Dim myProp As PIProperty

Set myServer = PISDK.Servers.DefaultServer
myServer.Open

' Create a module to use for testing transaction mode (don't cause an error if it already exists)
On Error Resume Next
Set myModule = myServer.PIModuleDB.PIModules.Add("IPITransactionExample")
Set myModule = myServer.PIModuleDB.PIModules.Item("IPITransactionExample")
On Error GoTo 0
'
' Need to delete any properties it has so the program can be run more than once
'
For Each myProp In myModule.PIProperties
   myModule.PIProperties.Remove myProp
Next
'
' Add some PIProperty's
'
Set IPIT = myModule
transUID = IPIT.Begin
Set myProp = myModule.PIProperties.Add("Prop1", 1)
myProp.PIProperties.Add "SubProp1", 11
If (IPIT.isDirty = False) Then
   MsgBox "Changed module not marked as dirty"
End If
'
' Cancel transaction and check to see that module didn't change
'
IPIT.Cancel
If (myModule.PIProperties.Count > 0) Then
   MsgBox "Module was changed even after cancellation of the transaction"
End If
'
' Do the same transaction and commit
'
transUID = IPIT.Begin
Set myProp = myModule.PIProperties.Add("Prop1", 1)
myProp.PIProperties.Add "SubProp1", 11
'
' Commit transaction and check to see that module changed
'
IPIT.Commit
If (myModule.PIProperties.Count = 0) Then
   MsgBox "Module not changed after committing the transaction"
End If

MsgBox "All Tests Passed."
End Sub
 

Enabling Operational Intelligence