Need help with VBA coding for SE trends w/ comboboxes

Join Date
Feb 2014
Location
Saguenay
Posts
8
Hi guys,

My customer asked to be have only one trend display and then be able to select which pen should appear on it. I found a way using comboboxes and the code supplied by RA on the "KB 51769 Lab".

I created 5 comboboxes with each having every analog values available (26) so he can choose w/e he wants to appear (Datalog model is setup and working fine)

In the trend properties I have the 26 pens assignated to the Datalog model.

As soon as I open the trend display, the comboboxes are cleared and the 26 pens are displayed properly with historical data. The problem comes as soon as I choose pens with a combo box then press "refresh", the display shows the selected pens but restarts the datalogging at 0 so I lose all previously saved points. Then it works fine even if I change the display etc.. until I change pens and refresh again.

The "KB 51769 Lab" code is TechConnect level is I'm not gonna post it all but here's the generic line I use to display the selected pen using the first combobox as example:

If Pen1_ComboBox.Value > " " Then
Trend1.Pens.Add "{" & Pen1_ComboBox.Value & "}", Pen1_ComboBox.Value, "eu", 0, 100, "[Datalog model - LETHS]", False, True
End If

Does anyone know what should I do to prevent the datalogging to stop and restart?
 
Following Rockwell's advice on almost anything is just plain stupid, and 90% of the time, they make something up or point you to an invalid tech note just to get the call cleared.

---------------

Add all pens to the trend, and set the scaling, units, whatever as you want it.

Create a "Select Tag" pop-up On-Top window that can be opened from the trend display.

Put one button on that window, set it's size, right click it, select "Expose to VBA". Also open up its properties, and give it a real name. I use PB_ToggleP01 for example. Copy that on your pop up window for as many trend tags that you have and want to select / deselect at runtime.

Stuff this code (expanding, of course, for the number of pens you want to be selectable) into the VBA for the pen select window, changing the name "MyHistoryWindowName" to whatever the name of your trend display is.

Code:
Dim oDS As Displays
Dim oTrendDisp As Display


Private Sub Display_AnimationStart()
On Error GoTo ErrHandler
    
    Set oDS = LoadedDisplays
    Set oTrendDisp = oDS.Item("MyHistoryWindowName")
    
    SetPenStates oTrendDisp.Trend.Pens.Item(1), PB_ToggleP01
    SetPenStates oTrendDisp.Trend.Pens.Item(2), PB_ToggleP02
    SetPenStates oTrendDisp.Trend.Pens.Item(3), PB_ToggleP03
Exit Sub
    
ErrHandler:
    LogDiagnosticsMessage Err.Description & " Pensel.gfx Error", ftDiagSeverityError, ftDiagAudienceEngineer
    
End Sub


Private Sub PB_ClearAll_Released()

On Error GoTo ErrHandler

    ClearPenStates oTrendDisp.Trend.Pens.Item(1), PB_ToggleP01
    ClearPenStates oTrendDisp.Trend.Pens.Item(2), PB_ToggleP02
    ClearPenStates oTrendDisp.Trend.Pens.Item(3), PB_ToggleP03

Exit Sub
    
ErrHandler:
    LogDiagnosticsMessage Err.Description & " Pensel.gfx Error", ftDiagSeverityError, ftDiagAudienceEngineer

End Sub

Private Sub PB_ToggleP01_Released()
On Error GoTo ErrHandler

    TogglePen oTrendDisp.Trend.Pens.Item(1), PB_ToggleP01
Exit Sub
ErrHandler:
    LogDiagnosticsMessage Err.Description & " Pensel.gfx Pen Index Error", ftDiagSeverityError, ftDiagAudienceEngineer


End Sub

Private Sub PB_ToggleP02_Released()
On Error GoTo ErrHandler

    TogglePen oTrendDisp.Trend.Pens.Item(2), PB_ToggleP02
Exit Sub
ErrHandler:
    LogDiagnosticsMessage Err.Description & " Pensel.gfx Pen Index Error", ftDiagSeverityError, ftDiagAudienceEngineer

End Sub
Private Sub PB_ToggleP03_Released()
On Error GoTo ErrHandler

    TogglePen oTrendDisp.Trend.Pens.Item(3), PB_ToggleP03
Exit Sub
ErrHandler:
    LogDiagnosticsMessage Err.Description & " Pensel.gfx Pen Index Error", ftDiagSeverityError, ftDiagAudienceEngineer

End Sub
Private Sub ClearPenStates(thePen As Pen, thePB As Button)
On Error GoTo ErrHandler
    
    thePB.Caption = thePen.Description
    If thePen.Visible Then
        thePen.Visible = False
        thePB.BackColor = 8421504
        thePB.ForeColor = 0
    End If
    
Exit Sub
    
ErrHandler:
    LogDiagnosticsMessage Err.Description & " Pensel.gfx Error", ftDiagSeverityError, ftDiagAudienceEngineer
    
End Sub


Private Sub SetPenStates(thePen As Pen, thePB As Button)
On Error GoTo ErrHandler
    
    thePB.Caption = thePen.Description
    If thePen.Visible Then
        thePB.BackColor = 32768
        thePB.ForeColor = 16777215
    Else
        thePB.BackColor = 8421504
        thePB.ForeColor = 0
    End If

Exit Sub
    
ErrHandler:
    LogDiagnosticsMessage Err.Description & " Pensel.gfx Error", ftDiagSeverityError, ftDiagAudienceEngineer


End Sub
Private Sub TogglePen(thePen As Pen, thePB As Button)
On Error GoTo ErrHandler
    
    If thePen.Visible Then
        thePen.Visible = False
        thePB.BackColor = 8421504
        thePB.ForeColor = 0
    Else: thePen.Visible = True
        thePB.BackColor = 32768
        thePB.ForeColor = 16777215
    End If
Exit Sub
    
ErrHandler:
    LogDiagnosticsMessage Err.Description & " Pensel.gfx Error", ftDiagSeverityError, ftDiagAudienceEngineer


End Sub

All this does is pick up a caption for each button based on the description configured on the trend configuration, and toggle the visible state of the pens, so no chart redrawing required.

For extra bonus points, put this code in the vba for the actual trend window:

Code:
Private Sub Display_AnimationStart()
    Trend.LoadTemplate "LastView"
End Sub

Private Sub Display_BeforeAnimationStop()
    Trend.SaveTemplate "LastView"
End Sub

That will make the selections (all selections on the trend) persistent through closing/reopening the trend window, or restarting the client.
 
Thanks a lot for the help, however there is something I'm missing here:


the editor doesnt recognize the user-defined type when I compile the code up this line:


Code:
Private Sub ClearPenStates(thePen As Pen, thePB As Button)


EDIT: The type "pen" is not recognized. Trying to find the right type to use
 
Last edited:
Sorry..
In the VBA editor, go to the menu bar "Tools | References".
Put a check box in the following:

Visual Basic for Applications
RSView Display Client Object Model
OLE Automation
Rockwell Software TrendX
RSView Display Client Object Model

I'm thinking you don't have the Rockwell Software TrendX selected as a reference, which is where the "Pen" type is defined.
 
I can't get the code to work. This is how I adapted it ( I kept only 3 pens below to make it shorter)

Code:
Option Explicit

Dim oDS As Displays
Dim oTrendDisp As Display


'On animation start of the popup window:
'Note that popup needs to find the window with the trend on it

Private Sub Display_AnimationStart()
On Error GoTo ErrHandler
    
    Set oDS = LoadedDisplays
    Set oTrendDisp = oDS.Item("Tendance")
    
    SetPenStates oTrendDisp.Trend.Pens.Item(1), PB_Pen1
    SetPenStates oTrendDisp.Trend.Pens.Item(2), PB_Pen2
    SetPenStates oTrendDisp.Trend.Pens.Item(3), PB_Pen3
    
Exit Sub
    
ErrHandler:
    LogDiagnosticsMessage Err.Description & " Pensel.gfx Error", ftDiagSeverityError, ftDiagAudienceEngineer
    
End Sub

'Set pen states pulls info from the actual trend pen settings:
Private Sub SetPenStates(thePen As Pen, thePB As Button)
On Error GoTo ErrHandler
    
    thePB.Caption = thePen.Description
    If thePen.Visible Then
        thePB.BackColor = 32768
        thePB.ForeColor = 16777215
    Else
        thePB.BackColor = 8421504
        thePB.ForeColor = 0
    End If

Exit Sub
    
ErrHandler:
    LogDiagnosticsMessage Err.Description & " Pensel.gfx Error", ftDiagSeverityError, ftDiagAudienceEngineer

End Sub

' Pressing a Pen Toggle Button on the popup calls the toggle states function for the associated pen:
Private Sub PB_Pen1_Released()
On Error GoTo ErrHandler

    TogglePen oTrendDisp.Trend.Pens.Item(1), PB_Pen1
Exit Sub
ErrHandler:
    LogDiagnosticsMessage Err.Description & " Pensel.gfx Pen Index Error", ftDiagSeverityError, ftDiagAudienceEngineer
End Sub

Private Sub PB_Pen2_Released()
On Error GoTo ErrHandler

    TogglePen oTrendDisp.Trend.Pens.Item(2), PB_Pen2
Exit Sub
ErrHandler:
    LogDiagnosticsMessage Err.Description & " Pensel.gfx Pen Index Error", ftDiagSeverityError, ftDiagAudienceEngineer

End Sub

Private Sub PB_Pen3_Released()
On Error GoTo ErrHandler

    TogglePen oTrendDisp.Trend.Pens.Item(3), PB_Pen3
Exit Sub
ErrHandler:
    LogDiagnosticsMessage Err.Description & " Pensel.gfx Pen Index Error", ftDiagSeverityError, ftDiagAudienceEngineer

End Sub

' Toggle the actual pen passed in and set the button state:
Private Sub TogglePen(thePen As Pen, thePB As Button)
On Error GoTo ErrHandler
    
    If thePen.Visible Then
        thePen.Visible = False
        thePB.BackColor = 8421504
        thePB.ForeColor = 0
    Else: thePen.Visible = True
        thePB.BackColor = 32768
        thePB.ForeColor = 16777215
    End If
Exit Sub
    
ErrHandler:
    LogDiagnosticsMessage Err.Description & " Pensel.gfx Error", ftDiagSeverityError, ftDiagAudienceEngineer


End Sub

Private Sub ClearPenStates(thePen As Pen, thePB As Button)
On Error GoTo ErrHandler
    
    thePB.Caption = thePen.Description
    If thePen.Visible Then
        thePen.Visible = False
        thePB.BackColor = 8421504
        thePB.ForeColor = 0
    End If
    
Exit Sub
    
ErrHandler:
    LogDiagnosticsMessage Err.Description & " Pensel.gfx Error", ftDiagSeverityError, ftDiagAudienceEngineer

End Sub

Private Sub PB_ClearAll_Released()

On Error GoTo ErrHandler

    ClearPenStates oTrendDisp.Trend.Pens.Item(1), PB_Pen1
    ClearPenStates oTrendDisp.Trend.Pens.Item(2), PB_Pen2
    ClearPenStates oTrendDisp.Trend.Pens.Item(3), PB_Pen3
    
Exit Sub
    
ErrHandler:
    LogDiagnosticsMessage Err.Description & " Pensel.gfx Error", ftDiagSeverityError, ftDiagAudienceEngineer

End Sub

During runtime as soon as I open the "pen selection" pop-up and also if I press any of the pen buttons I get a diagnostic error:
"Object does not support this property or method Pensel.gxf Pen Index Error"

it seems to be generated by the ErrHandler sub?


Also the trend display code is faulted at the lines:

Code:
Trend.LoadTemplate "LastView"
and
Code:
Trend.SaveTemplate "LastView"

The "trend" object is missing and I cant find it even though I added every possible References that contained "trend" in the name I still cant find it. Do you know how I can create/add it ?

I also noted in this post in which you gave a similiar code that "lastview" was written : ("Last_view") so I'm guessing the parenthesis and the underscore are not required for this line to work correctly?

I'm strill trying to figure out the syntax in VB as I've never worked with that before t.t
 
"Trend" is the exposed name of the trend object on the historical trend display. Go you your actual trend object, right click it, properties, common, Name. Set that to Trend.

While you are there, right click on the trend object, select "Property Panel" and make sure that "Expose to VBA is set to "VBA Control"
 
How should I specify the location to save the template? I get this error
when I open the display:

Runtime error '1006'
A file error occured while attempting to load, save or remove the specified template
 
Last edited:
You can give it a hard-coded path, I generally only enable that when installing the software on the final destination box, or just ignore the error. There will always be an initial error until a valid template file has been saved once.
 
Allright let's ignore it for the moment

I'm getting a "type mismatch" error when I load the pen selection popup and eac htime I press a pen selection button. However the VBA compiler shows no error so I'm not sure what element has the wrong type.
 
Yea and all my pens appear on the trend display during runtime


Based on the code, shouldnt the caption on each pen seletion button = the description of the pen property? I left the buttons blank but wrote a description for each pen in the trend properties but the butons remain blank at runtime
 
Last edited:
Yes, the pen description should appear as the caption of the buttons. If it isn't, it is implying that you aren't properly referencing the actual trend object. You must give it a name, I just use "Trend" to make it simple (and so my pen selector can work for multiple displays with historical trends).

Also, this must be run in a client, it cannot be run or tested in Studio, as within Studio, the intra-display communications cannot take place. Stupid, but that is FTView for you.
 
I use the SE display client to test and I named my trend "Trend" as you suggested.

I've been tweaking things/renaming tags trying to make it work but it eludes me!

I'm gonna keep trying/searching till I get something as I'm leaving for the site on monday.
 
Is your trend called "Trend" on the final tab of its properties page? There is a place on the first tab where you can name the trend, but the name that matters to VBA is on the final tab (or you can get to it I think through the "Property Panel" from right clicking).
 

Similar Topics

I'm begginer at VBA. What should be the correct way to check if a display is currently showing and then show another display? I tried to put this...
Replies
17
Views
8,353
I have a alarm banner A display that shows on the botton of the screen and that must shown along with any of two specific main centered displays...
Replies
2
Views
2,040
Public WithEvents oGroup1 As TagGroup Public vidrios1, vidrios2, vidrios3, vidrios4, vidrios5, vidrios6, vidrios7, vidrios8, vidrios9 As String...
Replies
5
Views
2,699
I am kind of over my head with this and need help, its a VBA application in RSView32, the program opens a historical trend screen, first it opens...
Replies
8
Views
4,976
I'm fairly new to Rockwell software, I've had some basic training in the past but nothing too advanced. My company and I use Reliable products for...
Replies
11
Views
335
Back
Top Bottom