VBA code Guru's

dbh6

Lifetime Supporting Member
Join Date
Jan 2013
Location
Central, NJ
Posts
552
I'am using VBA code in FT View SE software. Ok just to put it out there I have no experience with VBA, but i was able to compile this code from using AB's Knowledge. Here is the brief summary of the code, Their is an HMI tag of boolean type that I created called "ControlNode". Now the the fname is point to a text file in the C folder and then "exits" wich is of type boolean uses the function FileThere() to see of the specified text file called "test" is in that folder. If the Text file called "test" which is a .txt file is in that folder then, the VBA code writes a 1 to the HMI tag that i created "ControlNode". If the text file is not their then the VBA code will write a 0 to the "ControlNode".

I tested this by putting a numeric display and linking it to the "ControlNode" and it works, however if you noticed at the beginning of the VBA code i have this VBA code called when "Sub Display_AnimationStart()" which is basically when the display is called in my case the main display, and thats when it runs the VBA code only at the start of the animation, but i'm tryin to make it so not on animation start, but during the whole time the runtime is running, i tried numerous things, but hit a road block, because i don't have experience with VBA. So i posted the code below, and if you can help me with that i would appreciate it.Also of you want to modify and post the code feel free to do so because again i may already have some unnecessary code in their.



Code is below

Option Explicit
'Global declarations
Public MyTagGroup As TagGroup

Sub Display_AnimationStart()
Dim fname As String
Dim tagValue As Tag
Dim exists As Boolean

fname = "C:\Installs\test.txt"
exists = FileThere(fname)

If MyTagGroup Is Nothing Then
'Create a tag group based on the Area that the Display is located in
Set MyTagGroup = Application.CreateTagGroup(Me.AreaName)

'Add tags to tag group
'HMI Tag Example 1 – system tag
MyTagGroup.Add "ControlNode"

End If


Dim HMITestTag As Tag
Dim DirReFactoryTalkActivationsg As Tag

Dim SystemSecondTagValue As Integer
Dim SystemSecondTagHighEU As Integer
Dim SystemSecondTagLowEU As Integer
Dim ErrorCatch As Integer ' Used to determine where error occured

On Error GoTo ErrorTrap
ErrorCatch = 1


Set HMITestTag = MyTagGroup.Item("ControlNode")

'Add one to current tag value and write back to tag
HMITestTag.Value = exists

ErrorTrap:
'Log a diagnostic message using the SystemLogModule.bas wrapper module
Set MyTagGroup = Nothing



End Sub


Function FileThere(FileName As String) As Boolean
If (Dir(FileName) = "") Then
FileThere = False
Else:
FileThere = True
End If
End Function


Private Sub Display_BeforeAnimationStop()

'Destroy tag group
Set MyTagGroup = Nothing
End Sub

Private Sub Group1_Click()

End Sub

Private Sub NumericDisplay4_Change()

End Sub
Private Sub NumericDisplay5_Change()
Dim fname As String
Dim tagValue As Tag
Dim exists As Boolean

fname = "C:\Installs\test.txt"
exists = FileThere(fname)


If MyTagGroup Is Nothing Then
'Create a tag group based on the Area that the Display is located in
Set MyTagGroup = Application.CreateTagGroup(Me.AreaName)

'Add tags to tag group
'HMI Tag Example 1 – system tag
MyTagGroup.Add "ControlNode"

End If


Dim HMITestTag As Tag
Dim DirReFactoryTalkActivationsg As Tag

Dim SystemSecondTagValue As Integer
Dim SystemSecondTagHighEU As Integer
Dim SystemSecondTagLowEU As Integer
Dim ErrorCatch As Integer ' Used to determine where error occured

On Error GoTo ErrorTrap
ErrorCatch = 1


Set HMITestTag = MyTagGroup.Item("ControlNode")

'Add one to current tag value and write back to tag
HMITestTag.Value = exists

ErrorTrap:
'Log a diagnostic message using the SystemLogModule.bas wrapper module
Set MyTagGroup = Nothing

End Sub
 
i'm tryin to make it so not on animation start, but during the whole time the runtime is running

First, are you trying to make this run while the application is running or just the display being active? If you want this function running while the application is active, you will need to create a display that is Cached, Always Updating and not Replaceable. Then you would need to add code to that display.

Second, unless you want to make this code re-usable for other items in your code, the use of the very short function FileThere does not give you any advantage to putting the code inline in your subroutine.

Third, to make the code run not just on Animation_Start, I would suggest creating a numeric display object on the display and assign a tag to it that changes regularly (system\BlinkSlow would be an example). Then, add your file checking code to the Display_Change event which should fire everytime your underlying tag value changes.

Note: I would keep the TagGroup creation in the Animation_Start event. No need to recreate / check the tag group everytime you check for the file existence.
 
@ mflaugh thanks for your reply. First of all i created the VBA code based on a display because i didn't know how to make it while the application was running. So to answer your question, i want to make it so when the application is running. So as you suggested i made the display Cached by right clicking on the display and selecting Yes check box to cached and checking the always updating box. However how do you make the display not replaceable??? Is it by changing the display type ti either overlay or on top????

Secondly i'am only using this VBA code to check if the file is there or not, if it is their then the "ControlNode" HMI tag should read a 1 if not 0. Then i was basically using the "ControlNode" for visibility purposes. You stated that since i'm not reusing this code for other items that, the use of the very short function FileThere doesn't give me an advantage, so i decided to delete it. Question is, which part of the code are you refering to is this part below.

Function FileThere(FileName As String) As Boolean
If (Dir(FileName) = "") Then
FileThere = False
Else:
FileThere = True
End If
End Function

Lastly I Just compiled the project and added the numeric change event to the code and this is what i have.





Option Explicit
'Global declarations
Public MyTagGroup As TagGroup

Sub Display_AnimationStart()
Dim fname As String
Dim tagValue As Tag
Dim exists As Boolean

fname = "C:\Installs\test.txt"
exists = FileThere(fname)


If MyTagGroup Is Nothing Then
'Create a tag group based on the Area that the Display is located in
Set MyTagGroup = Application.CreateTagGroup(Me.AreaName)

'Add tags to tag group
'HMI Tag Example 1 – system tag
MyTagGroup.Add "ControlNode"
End If


Dim HMITestTag As Tag
Dim DirReFactoryTalkActivationsg As Tag

Dim SystemSecondTagValue As Integer
Dim SystemSecondTagHighEU As Integer
Dim SystemSecondTagLowEU As Integer
Dim ErrorCatch As Integer ' Used to determine where error occured

On Error GoTo ErrorTrap
ErrorCatch = 1


Set HMITestTag = MyTagGroup.Item("ControlNode")

'Add one to current tag value and write back to tag
HMITestTag.Value = exists

ErrorTrap:
'Log a diagnostic message using the SystemLogModule.bas wrapper module
Set MyTagGroup = Nothing



End Sub


Function FileThere(FileName As String) As Boolean
If (Dir(FileName) = "") Then
FileThere = False
Else:
FileThere = True
End If
End Function


Private Sub Display_BeforeAnimationStop()

'Destroy tag group
Set MyTagGroup = Nothing
End Sub

Private Sub Group1_Click()

End Sub


Private Sub NumericDisplay5_Change()
Dim fname As String
Dim tagValue As Tag
Dim exists As Boolean

fname = "C:\Installs\test.txt"
exists = FileThere(fname)


If MyTagGroup Is Nothing Then
'Create a tag group based on the Area that the Display is located in
Set MyTagGroup = Application.CreateTagGroup(Me.AreaName)

'Add tags to tag group
'HMI Tag Example 1 – system tag
MyTagGroup.Add "ControlNode"

End If


Dim HMITestTag As Tag
Dim DirReFactoryTalkActivationsg As Tag

Dim SystemSecondTagValue As Integer
Dim SystemSecondTagHighEU As Integer
Dim SystemSecondTagLowEU As Integer
Dim ErrorCatch As Integer ' Used to determine where error occured

On Error GoTo ErrorTrap
ErrorCatch = 1


Set HMITestTag = MyTagGroup.Item("ControlNode")

'Add one to current tag value and write back to tag
HMITestTag.Value = exists

ErrorTrap:
'Log a diagnostic message using the SystemLogModule.bas wrapper module
Set MyTagGroup = Nothing

End Sub



I tested this and it works!!! :). One last thing though you said since we are already creating the tag group when the animation starts, their is no need to to check the tag group everytime their is a numberic change, which i agree on, so which part of the code do i get rid of to do that?, And if you noticed their is code for,

Private sub Group1_Click()
End Sub

Is this part of the code doing anything???

Thanks for your help appreciate it.
 
However how do you make the display not replaceable??? Is it by changing the display type ti either overlay or on top????

I usually make the display an Overlay type but "hide" it behind another display. You could do that or put the code on a Navigation bar or something that is always visible on the screen. The idea is not to have it replaced when another display is called.

Question is, which part of the code are you refering to is this part below.

I am not saying anything you have is wrong. It could be more concise and efficient. Obviously since it is working, it could be left the same. Here is what I would modify your code to. The text within the asterisks are my explanations and are not needed in the code.

Option Explicit
'Global declarations
Public MyTagGroup As TagGroup
Dim tagValue As Tag

**** You can set the Tag & TagGroup as global variables and don't need to redeclare them with each event ***

Sub Display_AnimationStart()

If MyTagGroup Is Nothing Then
'Create a tag group based on the Area that the Display is located in
Set MyTagGroup = Application.CreateTagGroup(Me.AreaName)

'Add tags to tag group
'HMI Tag Example 1 – system tag
MyTagGroup.Add "ControlNode"
End If

End Sub
**** This is what I meant by leaving the TagGroup creation in the Animation Start event. This will keep the TagGroup in existence as long as the display is not replaced. You don't need to check your file existence here since this just sets up the TagGroup for further usage ****

Private Sub Display_BeforeAnimationStop()

'Destroy tag group
Set MyTagGroup = Nothing
End Sub

Private Sub NumericDisplay5_Change()
Dim fname As String
Dim exists As Boolean
Dim ErrorCatch As Integer ' Used to determine where error occured

fname = "C:\Installs\test.txt"
If (Dir(fname) = "") Then
exists = False
Else
exists = True
End If
**** This is what I meant by putting the File Check code inline rather than a function. It could be done either way but if you aren't checking for other file existence, the code is quicker if it doesn't have to break to another subroutine ****

On Error GoTo ErrorTrap
ErrorCatch = 1

Set tagValue= MyTagGroup.Item("ControlNode")

'Add one to current tag value and write back to tag
tagValue.Value = exists

Exit Sub

**** Make sure to exit the subroutine prior to reaching the Error Trap so that your Tag Group isn't destroyed everytime the event is processed. ****

ErrorTrap:
'Log a diagnostic message using the SystemLogModule.bas wrapper module
Set MyTagGroup = Nothing

End Sub

**** Note that I have not tested this code so the syntax may not be 100% correct. ****

Private sub Group1_Click()
End Sub

Is this part of the code doing anything???

No. It appears to be left over code in their example. Or they may have used it to provide the event for the code to run in.
 
Thanks for helping me clean up the code, appreciate it. One last thing since the VBA code is on one display do i make just the screen with the VBA code Overlay type then checkbox Keep at back to make it hide behind another display or i have to make each display that i have overlay? I think you meant only for the screen that has the VBA code not necessarily for the other screens, but i just want to be sure, because all my other screens are replace type. So let me know
 
I just tested it, code is working fine, however since i made the screen with the VBA code Overlay type with Keep at back check, if i switch to other screens and try to go back to that screen, it doesn't go back to it, do you have a solution to this? I unchecked the Keep at back check box on the screen that has the VBA code and made every other screen Overlay type & enabled cache on all of them as well as always updating, that seems to have resolved the issue,
 
Last edited:
When doing Supervisory Edition displays, I tend to not use full screen Replace type displays. If certain parts of the displays are the same (i.e. Navigation bar) I make them an individual display. That way I can add code that can run all the time. You could move the code to that type of display to keep it visible. Or you could add the code to a Replace display if you don't need to see the status all the time.

I would not suggest making all of the display Overlay with Caching unless you have very few displays. If you have any number of displays, performance will suffer since they are all trying to update all of the time.
 

Similar Topics

Hello Every one, I need a help on vba code for getting data from MSSQL server 2014 to display in FTview SE client version 12 . Thanks. Please...
Replies
4
Views
1,806
Hoping for some help on this one, we are running FTVIEW SE distributed on a server. The client file runs fine on this server. We have...
Replies
4
Views
3,733
Hello, it's been a little over a year now that I've been learning citect scada alone in version 7.2, I know how to make pages, synoptics, create...
Replies
4
Views
7,056
Hello guys, can anyone help me with FactoryTalk View Studio. I need to use a Global Object that contains VBA Macros in different displays, but it...
Replies
1
Views
1,673
I have VBA code on a FactorTalk View SE v11 project that reads various PLC tags in a FOR loop. All is good if the PLC tag exist. But for a...
Replies
4
Views
3,551
Back
Top Bottom