7 Replies Latest reply on Jun 5, 2017 3:02 PM by NathanHuberBrion

    How to get latest result with dataset

    thomas.clchan

      Hi all,

       

      As I know, using GetValue function can get the latest value with multi-State symbol.

       

      But if I don't want to use Multi-State symbol, any vba coding that I can get dataset or tag latest value?

       

      Also, if I want to get historical value(example : I want to get 2014/12/2 2 PM's value)  any coding can get it?

       

       

      Thomas

        • Re: How to get latest result with dataset
          Roger Palmen

          Hi Thomas,

          Some quick questions. Do you want to do this in ProcessBook? And once you have the latest value, or a historical value, what do you want to do with that?

           

          Because both are really easy to do, so i'm wondering if i understand your question correctly.

            • Re: How to get latest result with dataset
              thomas.clchan

              Hi Roger,

               

              Yes, I want do this in ProcessBook. I get the latest value is use for change colour in rectangle(If show with alarm colour, user can click it to open a new form with trend). The historical value is use for if user type a date on it, it show that time status.

               

              Thomas

                • Re: How to get latest result with dataset
                  pthivierge

                  Hello Thomas,

                   

                  Yes this is possible to get either historical or latest value using PISDK in ProcessBook.

                  I am providing you an example here, first look at those 3 functions that are in a module: ConnectPIServer, GetSnapshotValue, GetArchiveValue.

                   

                  ConnectPIServer needs to be called first before you can get anything from the PI Server.

                  GetSnapshotValue and GetArchiveValue shows you how to get values from the Server.  I would also recommend that you have a look into PISDK help ( Press F1 when you are in PI ProcessBook Visual Basic Editor, and look for the PIData object.  PIData Methods will give you all the information you need to get data in different ways ( several archive values, summaries, interpolated, etc.. )

                   

                  ModPISDK

                  Option Explicit
                  Private mPIServer As PISDK.Server
                  
                  
                  '---------------------------------------------------------------------------------------
                  ' Procedure : ConnectPIServer
                  ' Purpose   : Connect to PI Server
                  '---------------------------------------------------------------------------------------
                  '
                  Public Function ConnectPIServer(Optional ServerName As String, Optional Username As String, Optional Password As String, Optional ReconnectIfConnected As Boolean = False) As Boolean
                  
                  
                      If ServerName = "" Then
                          Set mPIServer = PISDK.Servers.DefaultServer
                      Else
                          Set mPIServer = PISDK.Servers(ServerName)
                      End If
                  
                  
                      ' if server could not be found exit the function
                      If mPIServer Is Nothing Then ConnectPIServer = False: Exit Function
                  
                  
                      If ReconnectIfConnected Then
                          If mPIServer.Connected Then mPIServer.Close
                      End If
                  
                  
                      ' manages basic authentication
                      Dim connectionString As String
                      If Username <> "" Then
                          connectionString = "UID=" & Username & ";PWD=" & Password
                      End If
                  
                  
                      mPIServer.Open connectionString
                  
                  
                      Debug.Print "PI Server Connected"
                  
                  
                      ConnectPIServer = mPIServer.Connected
                  
                  
                  End Function
                  
                  
                  '---------------------------------------------------------------------------------------
                  ' Procedure : GetPIValue
                  ' Purpose   : Gets a Snapshot value
                  '---------------------------------------------------------------------------------------
                  '
                  Public Function GetSnapshotValue(ByVal tagName As String, Optional TimeStamp As Variant) As PIValue
                  
                  
                      Dim pValue As PIValue
                  
                  
                      Set pValue = mPIServer.PIPoints(tagName).Data.Snapshot
                  
                  
                      Set GetSnapshotValue = pValue
                    
                  
                  
                  End Function
                  
                  
                  '---------------------------------------------------------------------------------------
                  ' Procedure : GetArchiveValue
                  ' Purpose   : Gets a value with a timestamp, timestamp can be a Date or a string. i.e. *-1d, "2014-12-17"
                  '---------------------------------------------------------------------------------------
                  '
                  Public Function GetArchiveValue(ByVal tagName As String, TimeStamp As Variant) As PIValue
                  
                  
                      Dim pValue As PIValue
                      
                      ' here be aware to checl what RetrievalTypeConstant you need.  Please refer to SDK help.
                      Set pValue = mPIServer.PIPoints(tagName).Data.ArcValue(TimeStamp, rtAuto)
                      
                      Set GetArchiveValue = pValue
                  
                  
                  End Function
                  
                  
                  
                  

                   

                  So, to update a value in ProcessBook using PI SDK I would create a function that uses the previous code shown.  Since these functions are public, you may call these functions with a button:

                  ThisDisplay

                  Public Sub UpdateValueToLatest()
                      
                      ' connect to PI Server using PI SDK
                      ModPISDK.ConnectPIServer "optimus"
                      ThisDisplay.txtSinusoidVal.Contents = ModPISDK.GetSnapshotValue("sinusoid")
                  
                  
                  End Sub
                  
                  
                  Public Sub UpdateValueToLastHour()
                      
                      ' connect to PI Server using PI SDK
                      ModPISDK.ConnectPIServer "optimus"
                      ThisDisplay.txtSinusoidVal.Contents = ModPISDK.GetArchiveValue("sinusoid", "*-1h")
                  
                  
                  End Sub
                  

                   

                   

                  2014-12-17_12-25-02_PI ProcessBook - [Display1_].png

                   

                  I also attached this example to the post.

                   

                  Let me know if this helps of if you have more questions.

                  1 of 1 people found this helpful
              • Re: How to get latest result with dataset
                NathanHuberBrion

                Little bit late on this one but here's what I did -

                 

                I build the dataset then linked it to a display Value on my Processbook screen (Thisdisplay.H41.)

                 

                 

                In VB I then just referenced Thisdisplay.H41.Getvalue (vrdate,vrstatus) to retrieve the value.

                 

                Here is my example:

                 

                Sub Calculations ()

                 

                ThisDisplay.U41.Value = Round((ThisDisplay.H41.GetValue(vrDate, vrStatus) + ThisDisplay.O41.Value + ThisDisplay.S41.Value), 2)

                 

                End Sub