3 Replies Latest reply on Apr 5, 2016 9:44 AM by pthivierge

    Values refresh after context change with VBA


      Hello Everyone!


      I'm creating a display with some VBA code, that:

      1. Select relevant elements from AF structure with OLEDB

      2. Go through these elements with changing the context of the element relative display

      3. Save all displays as JPEG

      4. Send it as email attachment


      On the display I have SQC charts and values that are attributes either with Data reference: none or PI tags.

      I have problem with the 2. or 3. point. After changing the context, the data on SQC charts and values with none data reference are updated on the exported JPEG, while Values pointing to attributes with PI tags are not.

      But this only happen if I run it. If I put a breakpoint after context change or go through the code with f8, it works fine.


      I use this code to set the context:


      Sub SetContextElements(sContext As String)
          Dim myContext As ContextHandler
          Set myContext = Application.ContextHandlers("E") ' select element relative context handler
          myContext.CurrentContext(ThisDisplay) = sContext ' changes the context
          myContext.ContextUpdated ThisDisplay  ' update? didn't see difference with or wothout it
          Application.Refresh ' also does not help
          ThisDisplay.Refresh ' also does not help
      End Sub

      Does anyone know this problem?


      Thank you for your help in advance.

        • Re: Values refresh after context change with VBA

          Okay it seems it could be solved by changing time on the Values:

          a = ThisDisplay.Value4.SetTimeRange("*-1d", "*")


          Also if you don't care if the timerange of your whole display is updated, this can be more easier:

          a = ThisDisplay.SetTimeRange("*-1d", "*")

          • Re: Values refresh after context change with VBA

            I've just got a better tip:

            a = DoEvents()


            This works fine without any additional effect

              • Re: Values refresh after context change with VBA

                Hello Robert,


                the use of DoEvents in VBA will allow the UI to be refresh, basically your code will stop to leave the time to the hosting application (e.g. Excel or PI ProcessBook) to refresh the screen and process other events that may have occurred in the code.

                In you case, since you are exporting the display as .jpg, this makes sense.  Because you need a refreshed UI before the picture gets generated.

                DoEvents is worth to use every X iterations in a long and expensive loops, so the user don't see the screen as frozen.  If you are using objects such as progress bars, DoEvents is also mandatory otherwise you wont see the progress.

                Basically, VBA is closely bounded to the UI thread so this is why it is necessary to think about refreshing the screen once in a while with the DoEvents.


                You made a good find


                Thanks for sharing!