3 Replies Latest reply on Jul 10, 2015 3:07 PM by orftoden

    VBA to Change pi Display Time Range


      Hello.  I am new to pi processbook and have some (although limited) experience with VBA.  I am trying to develop a series of screens that can "look back" x number of hours.  The screens will contain various values tied to pi tags that will be set to display the maximum value during the desired look back range.  The goal of the sheets would be to flag points that go above defined threshold values-- my plan is to display these exceedances using conditional formatting of objects.  The look back would be triggered though a button.




      Although I am in the beginning stages of setting up my project, I have began toying around with VBA code to define the desired "look back" range.  Here is what I have come up with, but it does not seem to be functional:


      Private Sub lookback_twelvehr()

      Dim StartTime As String
      Dim EndTime As String

      StartTime = Now
      EndTime = Now - 0.5
      ThisDisplay.SetTimeRange Format(StartTime.Value, "dd-MMM-yyyy 00:00"), Format(EndTime.Value, "dd-MMM-yyyy 00:00")
      End Sub


      My questions are as follows:


      1) Has anyone here attempted to create pages with similar functionality-- if so, have any advice or perhaps an example?

      2) Am I on the right track as far as my code is concerned?  Suggestions are most appreciated!


      I would like to thank any and all replies in advance.

        • Re: VBA to Change pi Display Time Range

          Hi Brad,


          You may have better luck with trend.SetTimeRange. For example,


          ThisDisplay.Symbols("Trend1").SetTimeRange "*-2h", "*"


          Instead, ThisDisplay.SetTimeRange seems to only change the time range if you first click on a symbol, and then shift-click the macro button.

          Calling SetTimeRange against the actual symbol gives finer control. Note this will only change the (run-time) time range. If the display is reopened, it will revert to the configured time range. To change the configured time range, use SetStartAndEndTime.

          Although I'm not sure how the details of the values/stats you want to show or format, I'm wondering if some of them could be performed using multistate symbols instead of custom coding.

          • Re: VBA to Change pi Display Time Range

            Hi Brad,


            What do you mean by "it does not seem to be functional"?


            Your current code seems to have the timestamps both at midnight of the same day. For example, you should be able to get it to work with either:


            ThisDisplay.SetTimeRange Format(StartTime.Value, "dd-MMM-yyyy hh:mm"), Format(EndTime.Value, "dd-MMM-yyyy hh:mm")

            or even simply,

            ThisDisplay.SetTimeRange Now - 0.5, Now

              • Re: VBA to Change pi Display Time Range

                Thank you both for your advice.  I was able to get the following code to work-- although it seems to only work for plots and not pi calculations.  Here's the code in case it helps out anyone else:


                Private Sub Update_Time_Range()


                Dim Start_Time As Date
                Dim End_Time As Date


                Start_Time = Format(DTPicker1, "MM/dd/yyyy HH:mm")
                End_Time = Format(DTPicker2, "MM/dd/yyyy HH:mm")


                If Start_Time = End_Time Then

                Exit Sub

                End If


                Dim Trend As Symbol


                For Each Trend In ThisDisplay.Symbols
                    If Trend Type = pbSYMBOLTYPE.pbSymbolTrend Then
                        Call Trend.SetStartAndEndTime(Start_Time, End_Time)
                    End If
                Next Trend


                End Sub