FactoryTalk View SE Macro to run VB Sub

justinvk6

Member
Join Date
Sep 2008
Location
Chiltern
Posts
5
Hi guys,

I was wondering if anyone could help me with a little problem regarding FactoryTalk View SE (FTV).

I am having problems with running a VB sub from a macro. I have completed a couple of RSView32 applications in the past and all I did then was create a macro as follows:-

VbaExec NameOfVBSub

This macro command, VbaExec, is not available for option in FTV as it was RSView32. My purpose of wanting to do this is to run certain code at startup and shutdown of the project (mainly to open/close DB connections, etc.).
My experience with this platform of both FTV/RSview32 and VB is limited so am I likely to be missing something, or is there another way of getting around this?

Thanks in advance,
Justin
 
Hi guys,

I was wondering if anyone could help me with a little problem regarding FactoryTalk View SE (FTV).

I am having problems with running a VB sub from a macro. I have completed a couple of RSView32 applications in the past and all I did then was create a macro as follows:-

VbaExec NameOfVBSub

This macro command, VbaExec, is not available for option in FTV as it was RSView32. My purpose of wanting to do this is to run certain code at startup and shutdown of the project (mainly to open/close DB connections, etc.).
My experience with this platform of both FTV/RSview32 and VB is limited so am I likely to be missing something, or is there another way of getting around this?

Thanks in advance,
Justin

RS/FTView SE Must have all VBA attached to a Display. You can't have stand-alone VBA modules that can be called from macro's.

The common way, is to make a 'Hidden' display, containing all of your VBA code, and then trigger the code based on changes of elements on the hidden display.
 
Sounds a little backwards but I understand what you are saying. Do you know why they changed the structure of RSView32 having a common VB script to the current FTV display orientated VB modules? To me it is a step back from RSView32 doing it this way. This package is just frustrating me more and more the further I progress with the installation.

Thanks a lot for your feedback though, it is much appreciated.

Regards,
Justin
 
Hi,
I understand how to create the hidden window, can you show an example of triggering the sub? Thanks

Put a numeric display on your hidden display (opened with /ZA switch). Tie it to your 'Trigger Tag', which can be a bool if you like. Right click on it, select "Expose to VBA". Then, go and edit the VBA for that display. You should have a pre-defined skeleton sub for your numeric display, which triggers on any change.

Pretty much the first check I do in that sub, is to make sure that it's a '1' or 'true' (since I use discretes for the triggers), if so, I run my code. If not, I just exit the sub (as the sub gets fired on any change, 0 to 1, or 1 to 0.
 
thanks rdrast!
I was hoping the functions would pass between screens but looks unlikely. thankyou very much.

You can call functions from other screens, that was actually fixed (silently) in one or another patch to FTV 5.x. To do so, you have to first build a list of loaded displays, then select one, then call the whatever... Example: to toggle a pen on a trend from another display...

(this is done on the fly, no error checking, YMMV)


' At the top, as global Declarations:
Dim AllDisplays as Displays
Dim MyTrendDisplay as Display

Private Sub Display_AnimationStart()
Set AllDisplays = LoadedDisplays
Set MyTrendDisplay = AllDisplays.Item("MyTrendWindow")

End Sub


Private Sub TogglePen (NewState as Bool)

MyTrendDisplay.MyTrendObjectName.Pens.Item(1) = NewState

End Sub




Just be sure to TEST TEST TEST. Sprinkle in a lot of calls to LogDiagnosticsMessage to make sure that program flow is as you like, and remember also that the VBA calls can take time if you try to do too much in VBA.
 
Last edited:
Thanks again rdrast,
I have tried to use this code to execute a function but to no end. Is it possible to extend this to a vba function on another page? Basically i am trying to store all code on one screen and all pages call to that one. To make it clear i will elaborate. One display, "Kernel" where i want to keep all code and another page called "Overview" where i have a button that i want to run a sub "funky1" which is held within the 'Kernel' display.

so extending the use of your suggested code, i would have:
**VBA on "Overview" display:**
Dim AllDisplays as Displays
Dim MyKernelDisplay as Display
Private Sub Display_AnimationStart()
Set AllDisplays = LoadedDisplays
Set MyKernelDisplay = AllDisplays.Item("Kernel")
End Sub

Private Sub Button1_Click()
call MyKernelDisplay.funky1() 'issue is going to be on this line, is there another way to do this? I know there is the way you suggest about triggering a tag then looking for a change on the other page holding the code but not only does that require you to create a HMI tag for each sub/function you want to trigger but also it does not allow for passing variables to functions on other pages (less you add another HMI tag for each variable for each function(FUN!)
End Sub


**VBA on "Kernel" display:**

Sub funky1()
msgbox("Hello World")
end sub

*End dodgy program*
Any suggestions would be most appreciated,
thank you.
-John



 
Firstly, what version, and what service pack level?
There was a bug that prevented calling subs on other displays in Release 5, without I think SR1 and a couple other patches.

If you are trying to call code on other VBA Forms, they must be active, I don't do that due to the hassles involved. Aside from that, it should work.

Don't be afraid to toss in lots of "LogDiagnosticsMessage" calls to trace your execution out and see where things may be failing.

EVERY VBA Sub under FTView MUST have an ON ERROR statement, I just send that to the end of the sub, with a LogDiagnosticsMessage with the Sub/function name, and module, and further error information if needed (like the text in the Err object). Without an ON ERROR, any error in the VBA at all will halt the VBA engine completely until a restart of the client.
 
Thank you very much rdrast. It works now. (ps i am running the latest patches etc. Thank you very much for your help, it is most appreciated.:p
 
Hi John i'm a complete beginner at VBA but am trying to do the same as you, calling code from an always active display when a button is pressed in another display but can't get it to work. could you send me a sample of your working code and what patches i need to install. Any help would be greatly appreciated.
Thanks
 
Hello martinS,
I'll list the required steps, I am not able to post the code as my work owns it. Its not the nicest programs to do this but here is how i got it going:

-Create a Kernel page, this is where we will hold all the functions.
-In the Kernel page, make it 'kept at back' and 'always updating' from the display settings

Create a macro, call it anything but i call mine startup
place the following in it:
Display Kernel /B /ZA
This sort of makes the Kernel properties redundant but hay...

Now you have to have an Overview page that all is called from, make this your default startup page. and have it run the 'startup' macro.

Ok, you now have the kernel run hidden in the background upon startup. Now lets see about adding functions:

In your kernel put the functions you want everyone to see. use the prefix 'Public Sub'.

now, on any page you want to call a function that is in the kernal place the following code:

Dim AllDisplays As Displays 'req to be able to call functions from other screens
Dim MyKernelDisplay As Display 'req to be able to call functions from other screens



Private Sub Display_AnimationStart() 'to be done on each page that req vba access to kernel
Set AllDisplays = LoadedDisplays
Set MyKernelDisplay = AllDisplays.Item("Kernel") 'out of all the pages shown, MyKernelDisplay will reference the Kernel Page
End Sub

to beable to use the function you must call it like follows:

Call MyKernelDisplay.IS_Extend(MySelfDisplay, MaxWidth, MinWidth, MaxHeight, MinHeight, iILPresent, ILWidth, ILHeight)

Notice what is happening. When i do a call, i use MyKernelDisplay which is a reference to the kernel page, then dot then function name. Since, for that function I needed to reference to the current page i used MySelfDisplay as one of the pass parameters. I hope this helped. This was done on FactoryTalk View Studio Local version 5.00.00 i had to get the latest version for this to work.
 
For completeness, i didn't tell you how to reference yourself (the page that you are currently calling your function from but may need to pass it)
Set MySelfDisplay = AllDisplays.Item(Me.Name) 'refer to myself as i need to pass this to kernel so it can pass back to me
I tend to personify things.
 
Hi John thanks for the quick reply, thats exactly what i needed i've tried the coed and its working great. Again Thanks for the help!
 
Hi every on,

I found the problem so i want to post my solution. I hope it should be helpfull !!

01 - Create a Event to detect a PLC flag to 1.
02 - Create a numeric display, set the property to "Exposed to VBA" to "VBA control" (It should be done over the Property Panel on FTView)
03 - Right click the numeric display, select the VBA code. In the Change Event add the following code:

Private Sub NumericDisplay1_Change()
On Error GoTo ErrorHandler
If Not IsError(NumericDisplay1.Value) Then
If NumericDisplay1.Value = 1 Then
ExecuteCommand "command string" (Here you can call a VBA function or whatever you want)
End If
End If
Exit Sub
ErrorHandler:
LogDiagnosticsMessage Err.Description, FTDiagSeverityError
End Sub

Note: If you create a background screan, you are going to have a PLC tag executing a VBA code.
 

Similar Topics

Hello, I am fairly new to AB PLC and HMI. I am using Factorytalk View to create an HMI application. While using Macros, I wanted to create a...
Replies
3
Views
2,379
Hello all I have a question, concerning datalogging in FTViewSE 8.0 I have 2 datalog models with 2 differents recording rates (30s for live...
Replies
0
Views
2,747
All, I am in the process of testing a FactoryTalk View SE application that has a display that calls a Macro from VBA. 9 times out of 10 this...
Replies
0
Views
6,024
Hi guys, I'm quite new to these forums so I wish I post my question in the good section. What I'm trying to accomplish is to display a trend...
Replies
0
Views
4,238
Hello everyone! I am having problems with running a Macro from VB Sub. How can I do it? Thanks in advance, Ilia
Replies
3
Views
4,760
Back
Top Bottom