20 Replies Latest reply on Oct 9, 2009 5:39 PM by spilon

    The SelectedSymbols Collection

    Mohammad

      Hello

       

      I am following the online course for PI Advanced Scripting for ProcessBook and I have a minor issue with one the scripts in the course.

       

      The section that I am working on is The Selected Symbols Collection

       

      I wrote the following program which is pretty similar to the online course but it does not give me the right answer.

      Sub GetData()

          Dim i As Long
          Dim time As Variant
          Dim value As Variant
          Dim pointcount As Long
          Dim restart As Long
          Dim trendproxy As PBsymlib.Trend

          If ThisDisplay.SelectedSymbols.Count = 0 Then
              MsgBox "Please select a trend."
              End
          ElseIf ThisDisplay.SelectedSymbols.Item(1).Type <> PBObjLib.pbSymbolTrend Then
              MsgBox " Please select A trend."
              End
          End If

          Set trendproxy = ThisDisplay.SelectedSymbols.Item(1)

          trendproxy.CurrentTrace = trendproxy.TraceCount

          pointcount = trendproxy.TraceValuesCount

          ThisDisplay.ListBox1.Clear
          ThisDisplay.ListBox2.Clear

          For i = 1 To pointcount

              value = trendproxy.GetTraceValue(i, time, restart)

              ThisDisplay.ListBox1.AddItem (time)
              ThisDisplay.ListBox2.AddItem (Format(value, "0.00"))

          Next i

      End Sub

      The program just gives me the message to select the trend and the second message. I select the trends and press the button but still get the "Please select A trend"  message.

       

      The sequence I did was building three trends , then list boxes, then VBE , then create a button and connect it to the macro name which is the name of my VB sub.

       

      Thanks

        • Re: The SelectedSymbols Collection
          cescamilla

          Can you debug the second part of the "if else if" (the part that checks for the type) and tell us the content of "ThisDisplay.SelectedSymbols.Item(1).Type" after you select one object?

            • Re: The SelectedSymbols Collection
              Mohammad

              Thanks Cristobal

               

              Here the screenshots

               

              1.bmp

               

              2.bmp

                • Re: The SelectedSymbols Collection

                  Strange, it works perfectly for me.

                  • Re: The SelectedSymbols Collection
                    Daniel Takara

                    Mohammad,

                     

                    Your first screenshot seems to suggest that you get the tooltip "PBObjLib.pbSymbolTrend = 10" when your mouse cursor is over the ThisDisplay.SelectedSymbols.Item(1).Type property on the code body. I just observed the same here (although your code is working just fine here).

                     

                    Add a watch of the ThisDisplay.SelectedSymbols collection and check if the value of Item(1).Type is 10 or not.

                      • Re: The SelectedSymbols Collection
                        Mohammad

                        Daniel

                         

                        I am not sure if I understood your sentence. I did all over the sentence but all it gives me the same sentence PBObjLib.pbSymbolTrend = 10

                         

                        how can I watch the value of  ThisDisplay.SelectedSymbols.Item(1).?

                         

                        thanks

                          • Re: The SelectedSymbols Collection
                            Daniel Takara

                            This is how you can watch the values of all properties of ThisDisplay.SelectedSymbols:

                             

                            1. select ThisDisplay.SelectedSymbols on the code body:

                             

                            vba_5F00_watch01.JPG

                             

                             

                             

                            2. right-click on the selection and choose the option "Add Watch..."

                             

                            vba_5F00_watch02.JPG

                             

                             

                             

                             

                              • Re: The SelectedSymbols Collection
                                Mohammad

                                Daniel

                                 

                                Here the screenshot

                                 

                                no value...3.bmp

                                  • Re: The SelectedSymbols Collection
                                    Daniel Takara

                                    Well, it seems like you did actually get the watch of ThisDisplay.SelectedSymbols: just take a look at the right lower box named "Watches" in the VBA Editor.

                                     

                                    Expand the hierarchy under ThisDisplay.SelectedSymbols until you find the value of its Item(1).Type, so that we can compare it with PBObjLib.pbSymbolTrend (which is 10) and try to find out the reason behind the unexpected behavior of the code in your machine.

                                    • Re: The SelectedSymbols Collection
                                      cescamilla

                                      I would have typed something like this (so you specifically compare to what you want... and, AFAIK compilers do not follow any more comparisons if the first series of a boolean operation is already false.

                                      If ThisDisplay.SelectedSymbols.Count > 0 and ThisDisplay.SelectedSymbols.Item(1).Type = PBObjLib.pbSymbolTrend Then
                                          'Do something
                                      End If

                                      I don't really know if 'and' is the correct operator for that.

                                       

                                      Anyway, it seems that both elements have the same value... I'll try the code in PB.

                                        • Re: The SelectedSymbols Collection
                                          cescamilla

                                          Ok, I tried it out

                                           

                                          and, after creating two lists, this code worked flawlessly.


                                          Private Sub GetData()
                                            Dim i As Long
                                            Dim time As Variant
                                            Dim value As Variant
                                            Dim pointcount As Long
                                            Dim restart As Long
                                            Dim trendproxy As PBsymlib.Trend

                                            If ThisDisplay.SelectedSymbols.Count > 0 Then
                                              If ThisDisplay.SelectedSymbols.Item(1).Type = PBObjLib.pbSymbolTrend Then
                                                Set trendproxy = ThisDisplay.SelectedSymbols.Item(1)

                                                trendproxy.CurrentTrace = trendproxy.TraceCount

                                                pointcount = trendproxy.TraceValuesCount

                                                ThisDisplay.ListBox1.Clear
                                                ThisDisplay.ListBox2.Clear

                                                For i = 1 To pointcount
                                                  value = trendproxy.GetTraceValue(i, time, restart)

                                                  ThisDisplay.ListBox1.AddItem (time)
                                                  ThisDisplay.ListBox2.AddItem (Format(value, "0.00"))
                                                Next i
                                                Exit Sub
                                              End If
                                            End If
                                            MsgBox " Please select a trend."
                                          End Sub

                                          I moved the if's a bit, and I was proved wrong by VBA, it does compare after the first one was already tested as false.

                                           

                                          But it works great. as expected.

                                           

                                           

                                          • Re: The SelectedSymbols Collection
                                            Daniel Takara

                                            Cristobal Escamilla

                                            AFAIK compilers do not follow any more comparisons if the first series of a boolean operation is already false. 

                                            You are referring to short-circuiting logical operations, which are available in Java (with the && and || operators) and Visual Basic (with the AndAlso and OrElse operators), but apparently not in VBA.

                              • Re: The SelectedSymbols Collection

                                Change

                                If ThisDisplay.SelectedSymbols.Count = 0 Then
                                    MsgBox "Please select a trend."
                                    End
                                ElseIf ThisDisplay.SelectedSymbols.Item(1).Type <> PBObjLib.pbSymbolTrend Then
                                    MsgBox " Please select A trend."
                                    End
                                End If

                                To

                                If ThisDisplay.SelectedSymbols.Count = 0 Then
                                    MsgBox "Please select a trend."
                                    Exit Sub
                                ElseIf ThisDisplay.SelectedSymbols.Item(1).Type <> PBObjLib.pbSymbolTrend Then
                                    MsgBox " Please select A trend."
                                    Exit Sub
                                End If

                                To get it working.
                                Just as a note if you want the last selected symbol then you need to highest index of the SelectedSymbols collection i.e. SelectedSymbols(SelectedSymbols.Count)

                                 

                                Rhys.

                                • Re: The SelectedSymbols Collection

                                  The problem is that the button you click to call the macro becomes the "selected item" as you click on it. Therefore, ThisDisplay.SelectedSymbols.Item(1) is that button and its .Type = PBObjLib.pbSymbolButton.

                                   

                                  You'll simply need to save ThisDisplay.SelectedSymbols, ignore the "selection" of the button when you click on it, then do the operations on that saved version of the selected symbols. The Display_SelectionChange is where you'll probably want to do that.

                                    • Re: The SelectedSymbols Collection
                                      Daniel Takara

                                      Steve Pilon

                                      The problem is that the button you click to call the macro becomes the "selected item" as you click on it. Therefore, ThisDisplay.SelectedSymbols.Item(1) is that button and its .Type = PBObjLib.pbSymbolButton.

                                       

                                       

                                       

                                      Ahhh, so this was the issue: it happens only if he uses the ProcessBook button object. Well, I actually never thought of using it to run macros, it's an interesting application.

                                       

                                      Mohammad can use Microsoft Forms CommandButton instead, which is the one I used (and probably Cristobal and RJK as well). If we click on it, the ThisDisplay.SelectedSymbols collection won't change.

                                      • Re: The SelectedSymbols Collection
                                        Mohammad

                                        Steve, the thing is that I wrote PBObjLib.pbSymbolTrend not PBObjLib.pbSymbolButton

                                         

                                         

                                          • Re: The SelectedSymbols Collection

                                            Well I'm not sure I understand your last statement - I thought we found and solved the problem where your code never get outside the initial If's - could you please clarify? 

                                             

                                            Also, I think you might want to consider something that's already provided within PI ProcessBook: the Details pane. I'm not sure what exactly you are doing with those values in the lists, but if it's solely for displaying to the user, then the Details pane might do it

                                              • Re: The SelectedSymbols Collection
                                                Mohammad

                                                Steve

                                                 

                                                Thanks for the answer. Actually you are right. I should have used Microsoft Form Commond Buton not PI Process Book.

                                                 

                                                I did solve the issue finaly

                                                 

                                                However, to summarize the issue we can say that we can not use PI ProcessBook buttom in this code, right?

                                                  • Re: The SelectedSymbols Collection

                                                    Glad to read you got everything sorted out!

                                                     

                                                    Mohammad

                                                    we can say that we can not use PI ProcessBook buttom in this code, right?

                                                     

                                                    Not quite. It's easier to use the Microsoft Forms CommandButton in this case, but you could use the PI ProcessBook Button. You'd simply need to implement something where you save ThisDisplay.SelectedSymbols, ignore the "selection" of the button when you click on it, then do the operations on that saved version of the selected symbols. You would probably do that under the Display_SelectionChange event.

                                                     

                                                    It sounds more complicated than it actually looks in code   But at the end of the day, you're better off just using the Microsoft Form CommandButton