Beruflich Dokumente
Kultur Dokumente
PROCESS VIEW
VBA TUTORIAL
FIRST IN FIELDBUS
JUN / 02
PROCESS VIEW
VBA TUTORIAL
VERSION 6.0
TM
FOUNDATION P V I E WT U T M E
smar
web: www.smar.com e-mail: smarinfo@smar.com
USA
Smar International Corporation Smar Laboratories Corporation Smar Research Corporation
6001 Stonington Street, Suite 100 10960 Millridge North, Suite 107 4250 Veterans Memorial Hwy.
Houston, TX 77040 Houston, TX 77070 Suite 156
Tel.: +1 713 849-2021 Tel.: +1 281 807-1501 Holbrook , NY 11741
Fax: +1 713 849-2022 Fax: +1 281 807-1506 Tel: +1-631-737-3111
e-mail: sales@smar.com e-mail: smarlabs@swbell.net Fax: +1-631-737-3892
e-mail: sales@smarresearch.com
© ICONICS, Inc., 2000
GraphWorX32, TrendWorX32, AlarmWorX32, ControlWorX32,
WebHMI, AlarmWorX6.0, ToolWorX, ToolBox, and OPC-To-The-
Core are trademarks of ICONICS, Inc.
Contents
1. ProcessView Functionality........................................................................1-1
1.1 Introduction...................................................................................................................... 1-1
1.2 The Programming Environment ...................................................................................... 1-1
1.3 VBA Basics...................................................................................................................... 1-3
1.3.1 Procedures and Functions ...................................................................................... 1-3
1.3.2. Declaring variables ................................................................................................ 1-3
1.3.3. Assigning values .................................................................................................... 1-4
1.4 Displaying messages to the operator.............................................................................. 1-4
1.5 Reading the value of a variable....................................................................................... 1-5
1.6 Changing the value of a variable..................................................................................... 1-7
1.7 Using forms in VBA programs......................................................................................... 1-8
1.8 Defining user functions.................................................................................................. 1-11
1.9 Entering values with a touchscreen .............................................................................. 1-12
III
Process View VBA Tutorial
Notes:
IV
Introduction
V
Process View VBA Tutorial
VI
Section 1
ProcessView Functionality
1.1 Introduction
This section covers the ProcessView functions, including GraphWorX32, TrendWorX32 and
AlarmWorX32 functions.
The different functions of an application can easily be accessed from within the application itself. For
example in GraphWorX32 starting at the ThisDisplay object any GraphWorX32 method or property can
be accessed. From this object either direct access to a property is provided, or if the property belongs
to an underlying object, methods are provided access to the underlying objects.
We will first explain the programming environment and some VBA basics, before actually starting to
program.
Once familiar with the programming environment the programming can start. In this section the
following topics will be covered:
• Display messages to the operator
• Entering data
• Creating simple forms
• Defining and using user created functions
• Reading from and writing to OPC Variables and Local GraphWorX32 Variables
1.1
ProcessView - VBA Tutorial
The following parts are present clockwise starting at the upper left.
• The project window (View Project Explorer): In the project window the different parts of the
projects are shown. Usually the following three folders are present: GWX32 Objects, containing all
GraphWorX32 objects, in this case the ThisDisplay object; Forms, which contains all defined forms
and Modules that contains all defined code modules. There is also a fourth folder possible that will
contain the class modules, but it is not that common.
• The form window: In the form window it is possible to change the look of a form using the toolbox
shown next to it. This toolbox contains by default about 15 component that can be used to create
forms, but it is also possible to add more components. VB 5.0 components as well as any ActiveX
control can be used in addition to VBA components.
• The code window: In the code window, the actual coding takes places and the different functions
and procedures are displayed. Different functions and or procedures can be selected from the two
pull down boxes found at the top of the code window.
• The Watches window (View Watch Window): In the watches-window, a watch can be added.
The window is only used during debugging; it can contain variables, which show the current value
while running the code.
• The last window is the property window (View Properties Window): This window shows the
properties of a module or function when the code window is active. It can also show the properties
of an object on a form or of the form itself, if the form window is active.
Besides the previously mentioned window there are also some additional windows that are quite
useful:
• The Object Browser (View Object Browser): In the object browser all methods and properties
belonging to a particular library can be viewed.
• The References Windows (Tools References): In the references-window, the currently used
references can be viewed. Additionally, it is possible to add references to the VBA project. Adding
additional references increases the functionality of the VBA project.
1.2
ProcessView - Functionality
1.3
ProcessView - VBA Tutorial
The example:
A help button has to be implemented in the Heating section of the display to provide advice to the
operator if needed. When pressed this button will show the operator information in the Heating section.
With the sample display, base.gdf open, use the following steps to create the help button and the VBA
code behind it:
1. Add a button to the screen using the toolbar or by choosing Dynamics Intrinsics Push-button
from the menu.
2. Change the Action to Run VBA Script
3. Press the Create Button to Open the VBA Script Wizard dialog
4. Enter "HeatingHelp" as the Script Name and press the OK button
5. Press the Edit Button to open the VBA Editor
6. Type in the following lines of the code in the module GwxHeatingHelp_Main:
The MsgBox function uses, the string that will be displayed in the dialog box on execution of the
script, as a parameter.
7. In the VBA Editor use File Close and Return to Gwx32 from the menu to return to
GraphWorX32.
8. Use the property page to change the text on the button to "Help"
The result of adding this button is shown below. When pressing the button located on the left a
message box is displayed in the middle of the screen.
1.4
ProcessView - Functionality
The example:
To be able to change the values of OPC tags and ProcessView local variables, data will have to be
exchanged between the VBA program and the variables (OPC or ProcessView Internal). Due to the
object oriented structure of ProcessView is very easy to read data into the VBA program where it can
then be used as desired.
Use the following steps to create a button and the code behind it to read and display the value of a
ProcessView variable.
1. Add a button to the screen using the toolbar or by choosing Dynamics Intrinsics Push-
button from the menu.
2. Change the Action to Run VBA Script
3. Press the Create Button to Open the VBA Script Wizard dialog
4. Enter "ReadingValue" as the Script Name and press the OK button
5. Press the Edit Button to open the VBA Editor
6. Type in the following lines of the code in the module GwxReadingValue_Main:
1.5
ProcessView - VBA Tutorial
7. In the VBA Editor use File Close and Return to Gwx32 from the menu to return to
GraphWorX32.
8. Use the property page to change the text on the button to "Read Value"
The result of adding this button is shown below. When pressing the ‘Read Value’ button a message
box is displayed in the middle of the screen displaying the current value of the variable
~~setenablecooling~~, in this case 1 because cooling is enabled (Enable Cooling = Yes):
1.6
ProcessView - Functionality
The example:
Besides being able to read the value of a point it is also necessary to be able to change the value of
point. Writing to a variable is very much like reading from a variable, the only difference is that instead
of assigning the value property to a local variable, you will assign a local variable, result of a formula or
constant value to the value property.
Use the following steps to create a button and the code behind it, which will allow the operator to type
in a value and write the value to a ProcessView local variable.
1. Add a button to the screen using the toolbar or by choosing Dynamics Intrinsics Push-
button from the menu.
2. Change the Action to Run VBA Script
3. Press the Create Button to Open the VBA Script Wizard dialog
4. Enter "WriteValue" as the Script Name and press the OK button
5. Press the Edit Button to open the VBA Editor
6. Type in the following lines of the code in the module GwxWriteValue_Main:
7. In the VBA Editor use File Close and Return to Gwx32 from the menu to return to
GraphWorX32.
8. Use the property page to change the text on the button to "Write"
A link is created between the MyPoint variable and the local variable ~~setenablecooling~~, for a
description of setting up the link see reading values.
To let the operator type in a value the InputBox function is used this function displays a message to the
operator and allows the operator to type in some text. The function returns with the typed in text. The
typed in text is stored in the variable MyStr. To be able to write the typed in text to MyPoint, the text
1.7
ProcessView - VBA Tutorial
has to be converted to a value. This can be achieved with the Val function, the result of this function is
stored in the variable MyValue. The value of the MyValue variable is assigned to the Value property of
the MyPoint variable.
In the end the link between the local variable and the MyPoint variable is destroyed by assigning
Nothing to MyPoint.
The result of adding this button is shown below. When pressing the Write button an input box is
displayed in the middle of the screen in which the operator can enter a value in this case the value 0 is
entered. Pressing the OK button or the Enter will close the input box and continue with the script.
The example:
Forms can be created to make more complex user interaction possible or to limit the operator to what
values can be chosen. In this example a form will be created that allows the operator to turn the
cooling system on or off using option buttons. Option buttons must be created in at least a set of two.
And from that set of option buttons only one option button can and will be active at any point in time. In
the example a form with two option buttons will be created, one option button for turning the cooling
system off and one option button for turning the cooling system on. There is also an OK button on the
form to close the form. When the form is closed the value selected and the option button set is written
to the ProcessView variable.
Use the following steps to create a button and the form and code behind it to have the operator choose
an option button and write the value to a ProcessView local variable.
1. Add a button to the screen using the toolbar or by choosing Dynamics Intrinsics Push-
button from the menu.
2. Change the Action to Run VBA Script
1.8
ProcessView - Functionality
3. Press the Create Button to Open the VBA Script Wizard dialog
4. Enter "FormWriteValue" as the Script Name
5. Check the box "Insert a from" and press the OK button
6. Press the Edit Button to open the VBA Editor
7. In the project window double click on "GwxFormWriteValue_MainForm" this will open the user
form and the form toolbox. If the toolbox is not visible choose View Toolbox from the menu.
The Toolbox is shown below
Create a frame in the form, and change the Caption property to "Cooling Section" using the property
window.
12. Double click on the Exit button to attach code to the Exit button
1.9
ProcessView - VBA Tutorial
14. In the VBA Editor use File Close and Return to Gwx32 from the menu to return to
GraphWorX32.
15. Use the property page to change the text on the button to "Write Form"
A link is created between the MyPoint variable and the local variable ~~setenablecooling~~, for a
description of setting up the link see reading values.
Using the If statement checks which option button is active. If the On button is active the value 1 is
assigned to the Value property of the MyPoint variable otherwise the value 0 is assigned to the Value
property of the MyPoint variable.
Assigning Nothing to MyPoint destroys the link between the local variable and the MyPoint variable.
At the end the form is closed using the Unload Me command.
The result of adding this button is shown below. When pressing the Write Form button a form is
presented, to the operator in which the appropriate setting for the cooling section can be selected.
Pressing the Exit button will close the form and write the selected value to a variable in ProcessView.
1.10
ProcessView - Functionality
The example:
To be able to re-use already written code it will be useful to define functions and procedures that
perform a certain task with a number of parameters given to the function or procedure. The difference
between a function and a procedure is that a function returns a certain value and a procedure does
not.
In this example a function will be defined which takes 1 parameter, a string defining a ProcessView
point name, and return the value of that point. Use the following steps to create a push button and the
function example:
1. Add a button to the screen using the toolbar or by choosing Dynamics Intrinsics Push-
button from the menu.
2. Change the Action to Run VBA Script
3. Press the Create Button to Open the VBA Script Wizard dialog
4. Enter "FunctionExample" as the Script Name and press the OK button
5. Press the Edit Button to open the VBA Editor
6. Type the following lines of the code in the module GwxFunctionExample_Main to define a
function that returns the value of a given variable based on the name of that variable:
Function Example(Name As String) As Variant
Dim MyPoint As GwxPoint
Set MyPoint = ThisDisplay.GetPointObjectFromName(Name)
Example = MyPoint.Value
End Function
1.11
ProcessView - VBA Tutorial
7. To make use of the defined function enter the following code in the module
GwxFunctionExample_Main that displays the values of two variables using the MsgBox function:
Sub FunctionExample(o As GwxPick)
MsgBox Example("~~set_furnace1~~")
MsgBox Example("~~set_furnace2~~")
End Sub
8. In the VBA Editor use File Close and Return to Gwx32 from the menu to return to
GraphWorX32.
9. Use the property page to change the text on the button to "Function"
How the VBA program works: Pressing the button will call the FunctionExample code. This code
contains a reference to the Example Function that takes a string as a parameter to identify a variable
and returns the value of that variable. The value of the variable is shown using the MsgBox function.
The function Example will do the following when called:
First it defines a variable called MyPoint, after that it sets up a link to the variable specified in the Name
parameter of the function. Once the link is created the Value property can be accessed. This value
property is then assigned to the function name which means that the function has now as return value
the value of the Value property of the ProcessView variable pointed at by MyPoint.
The result of adding this button and VBA code is shown below. When pressing the Function button a
dialog box is presented in which the current temperature of furnace 1 is show, closing this box using
the OK button will show another box which displays the current temperature of furnace 2.
The example:
When a touchscreen is the only means of interaction between the operator and application it is not as
easy for the operator to enter setpoint as when the operator also has a keyboard.
This example will show how to define a keypad in a form and how to connect this keypad to process
points making it possible for the operator to enter values and download these values to the process
point.
Use the following steps to create a push button and the code behind it:
1. Add a process point to the screen using the toolbar or by choosing Dynamics Intrinsics
Process Point from the menu.
2. Enter a datasource to connect to, in this example the datasource will be ~~set_furnace1~~.
3. Press OK to leave the dialog box
4. Add a pick action to the Process Point using the toolbar or by choosing Dynamics Actions
Pick from the menu.
5. Change the description of the pick action to ~~ set_furnace1~~. The name of the variable that
needs to be changed is placed in the description, to make it possible to create a script that can be
used for many different variables. The script will just have to look in the description field to find
out which variable to modify.
6. Change the Action to “Run VBA Script”
7. Press the Create Button to Open the VBA Script Wizard dialog
8. Enter "KeyPad" as the Script Name
9. Check the box "Insert a form" and press the OK button
10. Press the Edit Button to open the VBA Editor
11. Enter the following code:
Public ValueStr As String
Sub KeyPad(o As GwxPick)
Dim MyPoint As GwxPoint
' Show custom form
GwxKeyPad_MainForm.Show
If Len(ValueStr) > 0 Then
Set MyPoint = ThisDisplay.GetPointObjectFromName(o.UserDescription)
MyPoint.Value = Val(ValueStr)
End If
End Sub
12. In the project window double click on "GwxKeyPad_MainForm" this will open the userform for
editing and the toolbox. If the toolbox is not visible choose View Toolbox from the menu.
13. The following form properties will have to be set:
Form: (Name): GwxKeyPad_MainForm
Caption: KeyPad
14. The following objects will have to be created on the form (the font for all objects is Tahoma 14
Bold):
TextBox: (Name): Value
Button: (Name): Key0 Caption: 0
Button: (Name): Key1 Caption: 1
Button: (Name): Key2 Caption: 2
Button: (Name): Key3 Caption: 3
Button: (Name): Key4 Caption: 4
Button: (Name): Key5 Caption: 5
Button: (Name): Key6 Caption: 6
Button: (Name): Key7 Caption: 7
Button: (Name): Key8 Caption: 8
Button: (Name): Key9 Caption: 9
Button: (Name): BS Caption: BS
Button: (Name): Cancel Caption: Close
Button: (Name): Enter Caption: Enter
15. Place all these buttons in the form the create the following layout
1.13
ProcessView - VBA Tutorial
16. When all objects are placed on the screen double click on the BS button and enter the following
code:
Dim UsedDot As Boolean
Private Sub BS_Click()
Dim l As Integer
l = Len(Value.Text)
If l > 1 Then
If Mid$(Value.Text, l, 1) = "." Then UsedDot = False
Value.Text = Mid$(Value.Text, 1, l - 1)
Else
Value.Text = ""
UsedDot = False
End If
End Sub
1.14
ProcessView - Functionality
1.15
ProcessView - VBA Tutorial
17. In the VBA editor use File Close and Return to Gwx32 from the menu to return to
GraphWorX32
18. To use the script on other process points repeat step 1 to 6 for the different variables. After that
enter the following script name "GwxKeyPad_Main.KeyPad" instead of pressing the Create
button. Press OK to leave the dialog box.
1.16
ProcessView - Functionality
1.17
Chapter 2
2.1 Introduction
To be able to integrate different applications together it must be possible to access other applications
while using an application. Specific tasks can be executed in the most efficient way because other
applications can be used to do it. Therefore, let database functions be executed by a database
application such as Access, let spreadsheet functions be executed by a spreadsheet application such
as Excel and so on.
The technology behind this functionality is called OLE Automation. Using OLE Automation and simple
VBA commands it becomes possible to access and use other applications as desired.
The easiest way of using other applications functionality is by defining an object in VBA and assigning
that object to the desired application, for example:
The above code will first define two variables one of the object type Excel.Application and a second of
the type Excel.Workbook, secondly it will try to connect Excel to the first variable and then it will open a
workbook and link that to the second variable.
If successful the situation can be represented as follows:
ProcessView Excel97
VBA Work
Work Book
Book
ExcelObj
Work
WBOb Book
This method can be used to connect to any application that supports OLE Automation. This section
shows methods of setting up connections to Excel, Access and Outlook.
2-1
Accessing other Applications
The example:
In this example an already running instance of Excel will be accessed, a specific workbook will be
loaded in it and once the workbook is loaded, Excel will be shown on top. To create this functionality
use the following steps:
1. Add a button to the screen using the toolbar or by choosing Dynamics Intrinsics Push-
button from the menu.
2. Change the Action to Run VBA Script
3. Press the Create Button to Open the VBA Script Wizard dialog
4. Enter "UseExcel" as the Script Name and press the OK button
5. Press the Edit Button to open the VBA Editor
6. Go to Tools References in the menu and mark the following library: "Microsoft Excel 8.0 Object
Library" to have easy access to Excel functionality.
7. Type in the following lines of the code in the module GwxUseExcel_Main:
Sub UseExcel(o As GwxPick)
Dim ExcelObj As Application
Set ExcelObj = GetObject(, "Excel.Application")
ExcelObj.Workbooks.Open ("d:\wwsc\wwsc.xls")
ExcelObj.WindowState = xlNormal
AppActivate ExcelObj
End Sub
8. In the VBA Editor use File Close and Return to Gwx32 from the menu to return to
GraphWorX32.
9. Use the property page to change the text on the button to "Use Excel"
2-2
ProcessView - VBA Tutorial
The example:
The previous example showed how to gain access to another application and make use of functions
embedded in that other application. However sometimes it is better not to confront the operator with a
different program such as Excel, because it has a different interface or because of the security holes
within it. Yet some of the functionality offered by the other program (Excel) needs to be used for the
appropriate operation of integrated application. In this case a link to the other application can be setup
but the application will never be shown, instead data from the other application can be displayed in
user defined forms. The following example shows how to retrieve data from Excel and show it in a user
defined form.
In this example different recipes are retrieved from Excel using OLE automation and presented in a
user-defined form. The operator can choose a recipe and download the values in the recipe to the
ProcessView variables. Use the following steps to create a push button and the code behind it:
1. Add a button to the screen using the toolbar or by choosing Dynamics Intrinsics Push-
button from the menu.
2. Change the Action to Run VBA Script
3. Press the Create Button to Open the VBA Script Wizard dialog
4. Enter "Recipe" as the Script Name
5. Check the box "Insert a from" and press the OK button
6. Press the Edit Button to open the VBA Editor
2-3
Accessing other Applications
7. Go to Tools References in the menu and mark the following library: "Microsoft Excel 8.0 Object
Library" to have easy access to Excel functionality.
8. In the project window double click on "GwxRecipe_MainForm" this will open the user form and the
form toolbox. If the toolbox is not visible choose View Toolbox from the menu.
9. The following form will have to be created:
2-4
ProcessView - VBA Tutorial
16. In the project window double click on "GwxRecipe_MainForm" this will open the user form again.
17. Double click on the List box this will change the view to the code window. Enter the following
code:
Private Sub Profiles_Click()
Dim index As Integer
index = Profiles.ListIndex + 5
Furn1.Text = WBObj.Sheets(1).Cells(index, 3)
Furn2.Text = WBObj.Sheets(1).Cells(index, 4)
Cool.Text = WBObj.Sheets(1).Cells(index, 5)
Furn3.Text = WBObj.Sheets(1).Cells(index, 6)
Furn4.Text = WBObj.Sheets(1).Cells(index, 7)
Furn5.Text = WBObj.Sheets(1).Cells(index, 8)
End Sub
18. In the project window double click on "GwxRecipe_MainForm" this will open the user form again.
19. Double click on the button labeled "Use & Exit" this will change the view to the code window.
Enter the following code:
Sub SetValue(Name As String, Value As Variant)
Dim MyPoint As GwxPoint
Set MyPoint = ThisDisplay.GetPointObjectFromName(Name)
MyPoint.Value = Value
Set MyPoint = Nothing
End Sub
20. In the project window double click on "GwxRecipe_MainForm" this will open the user form again.
21. Double click on the button labeled "Use Not & Exit" this will change the view to the code window.
Enter the following code:
Private Sub NoUseExit_Click()
Unload Me
End Sub
22. In the VBA Editor use File Close and Return to Gwx32 from the menu to return to
GraphWorX32.
23. Use the property page to change the text on the button to "Recipes"
2-5
Accessing other Applications
2-6
ProcessView - VBA Tutorial
The result of adding the Recipes button and VBA code is shown below. When pressing the Recipes
button the wwsc.xls workbook is loaded and the data from that workbook is shown in the created form.
The example:
This example is very much like the previous example, only in this case data is retrieved from an
Access Database instead of an Excel spreadsheet. Besides that it is also possible to add and change
profiles (=records) whereas in the previous example this was not possible.
In this example different recipes are retrieved from Access using OLE automation and presented in a
user-defined form. The operator can either choose a recipe and download the values in the recipe to
the ProcessView variables or change/add a profile in the database. Use the following steps to create a
push button and the code behind it:
1. Add a button to the screen using the toolbar or by choosing Dynamics Intrinsics Push-
button from the menu.
2. Change the Action to Run VBA Script
2-7
Accessing other Applications
3. Press the Create Button to Open the VBA Script Wizard dialog
4. Enter "AccessRecipe" as the Script Name
5. Check the box "Insert a from" and press the OK button
6. Press the Edit Button to open the VBA Editor
7. Go to Tools References in the menu and mark the following libraries: "Microsoft Access 8.0
Object Library" and “Microsoft DAO 3.5 Object Library” to have easy access to the Access
functionality.
8. In the project window double click on "GwxAccessRecipe_MainForm" this will open the user form
and the form toolbox. If the toolbox is not visible choose View Toolbox from the menu.
2-8
ProcessView - VBA Tutorial
The first three objects are defined to be able to connect to Access and the objects in Access.
The procedure UserForm_Initialize creates a connection to Access using the GetObject function.
When a link to Access is created a particular database can be opened. After that a link is setup to
the database object. When the link to the database exists a link to a particular table (Profiles) is
created with the OpenRecordSet method. Once the three links are created two items (TRUE and
FALSE) are added to the Cooling combobox.
When the procedure Update is started, this procedure will first clear the listbox on the screen,
after that it will read the records one by one in the table and add those to the profiles listbox. The
2-9
Accessing other Applications
procedure UserForm_Terminate runs when the form is closed, it closes the database and resets
the objects that are linked to Access.
17. In the project window double click on "GwxAccessRecipe_MainForm" this will open the user form
again.
18. Double click on the listbox in the form this will open the code window again, enter the following
code here:
Private Sub Profiles_Click()
Dim i As Integer
Dim found As Boolean
Profrec.MoveFirst
found = False
For i = 0 To Profrec.RecordCount – 1
If Profrec.Fields(1).Value = Profiles.Value Then
found = True
Exit For
End If
Profrec.MoveNext
Next I
If found Then
ProfileName = Profrec.Fields(1).Value
Furnace1 = Profrec.Fields(2).Value
Furnace2 = Profrec.Fields(3).Value
Furnace3 = Profrec.Fields(4).Value
Furnace4 = Profrec.Fields(5).Value
Furnace5 = Profrec.Fields(6).Value
Cooling = Profrec.Fields(7).Value
Else
ProfileName = ""
Furnace1 = ""
Furnace2 = ""
Furnace3 = ""
Furnace4 = ""
Furnace5 = ""
Cooling = ""
End If
End Sub
This procedure tries to find the profile in the table that was clicked on in the profiles listbox. If this
record is found in the table, it displays the settings of this particular record in the form, if the
record is not found it empties the fields in the form.
19. In the project window double click on "GwxAccessRecipe_MainForm" this will open the user form
again.
20. Double click on the “Add Profile”-button in the form this will open the code window again, enter
the following code here:
Private Sub NewProfile_Click()
With Profrec
.AddNew
!Profile = ProfileName
!Furnace1 = Furnace1
!Furnace2 = Furnace2
!Furnace3 = Furnace3
!Furnace4 = Furnace4
!Furnace5 = Furnace5
!Cooling = Cooling
.UpDate
2-10
ProcessView - VBA Tutorial
End With
Call UpDate
End Sub
This procedure uses the With statement to perform certain methods on the Profrec object. The
AddNew method adds a new record in the table. After that the different fields in the table are filled
in. To access a field in a table use the following form: tablename!fieldname. Once all the fields are
filled in the Update method is called to write the changes to the database. At the end the Update
function is called to update the listbox in the form.
21. In the project window double click on "GwxAccessRecipe_MainForm" this will open the user form
again.
22. Double click on the “Change Profile”-button in the form this will open the code window again,
enter the following code here:
Private Sub ChangeProfile_Click()
With Profrec
.Edit
!Profile = ProfileName
!Furnace1 = Furnace1
!Furnace2 = Furnace2
!Furnace3 = Furnace3
!Furnace4 = Furnace4
!Furnace5 = Furnace5
!Cooling = Cooling
.UpDate
End With
Call UpDate
End Sub
This procedure uses the With statement to perform certain methods on the Profrec object. The
Edit method makes it possible to modify a record in the table. When the record is set into edit
mode the different fields in the table are updated. Once all the fields are filled in the Update
method is called to write the changes to the database. At the end the Update function is called to
update the listbox in the form.
23. In the project window double click on "GwxAccessRecipe_MainForm" this will open the user form
again.
24. Double click on the “Delete Profile”-button in the form this will open the code window again, enter
the following code here:
Private Sub DelProfile_Click()
Profrec.Delete
Call UpDate
End Sub
This procedure deletes the current record and calls the Update function to update the listbox in
the form.
25. In the project window double click on "GwxAccessRecipe_MainForm" this will open the user form
again.
26. Double click on the “Use Profile”-button in the form this will open the code window again, enter
the following code here:
Sub SetValue(Name As String, Value As Variant)
Dim MyPoint As GwxPoint
Set MyPoint = ThisDisplay.GetPointObjectFromName(Name)
MyPoint.Value = Value
Set MyPoint = Nothing
End Sub
Private Sub UseProfile_Click()
2-11
Accessing other Applications
The procedure SetValue takes two parameters, the first one is the name of a point name in
GraphWorX32 and the second one is the value that should be written to the point. The procedure
tries to find the specified name and writes the specified value to it. The procedure UseProfile calls
the SetValue procedure for each of the different points that should be written. At the end it
unloads the form (=Me).
27. In the project window double click on "GwxAccessRecipe_MainForm" this will open the user form
again.
28. Double click on the “Cancel”-button in the form this will open the code window again, enter the
following code here:
Private Sub Cancel_Click()
Unload Me
End Sub
2-12
ProcessView - VBA Tutorial
The example:
The functionality offered by Excel makes it possible to create advanced reports using some simple
VBA commands. This example creates an advanced Excel report with graphs based on an Excel
template workbook and data present in GraphWorX32.
2-13
Accessing other Applications
14. Double click on the form and enter the following code:
2-14
ProcessView - VBA Tutorial
15. In the VBA editor use File Close and Return to Gwx32 to close the menu and return to
GraphWorX32
How it works:
Once the form is opened a script is started this script connects to Excel and opens an Excel workbook.
This workbook functions as a template and the script will add the current values of 6 process points to
it. Based on these values the graph in the report will be updated. During the running of the script the
process bar is constantly updated, so the operator can see the progress of the script. Towards the end
of the script a new name for the workbook is determined based on the date and time, and using this
name the workbook is saved.
2-15
Accessing other Applications
2-16
ProcessView - VBA Tutorial
The example:
The functionality offered by Outlook 97 makes it possible to create and send emails from within
GraphWorX32. In this example we will show how to enter some text in a form, create a new email and
add the entered text in the email.
9. Select the form and change the caption property to “Send an email”
10. On the form create the following objects:
Label: (Name): Label1 Caption: Send To:
Label: (Name): Label2 Caption: Message:
TextBox: (Name): SendTo
TextBox: (Name): Message Multiline: True
Button: (Name): Cancel Caption: Cancel
Button: (Name): Send Caption: Send
2-17
Accessing other Applications
11. Once all objects are created on the form the should look like the figure shown below
12. Double click on the “Cancel” button in the form and enter the following code:
Private Sub Cancel_Click()
Unload Me
End Sub
13. In the project window double click on "GwxSendMail_MainForm" this will open the user form and
the form toolbox.
14. Double click on the “Send” button in the form and enter the following code:
The procedure Send first defines two objects one of the type Outlook.Application and one of the
type MailItem. After that it connects to Outlook. Using the OutlookObj a new Mail message is
created. The content of the SendTo textbox in the form is used as the recipient of the email. The
subject is set to GraphWorX32 mail message. The body text of the mail message is copied from
the Message textbox in the form. Once all properties are set, the mail is sent using the Send
method of the MailItem object. In the end both objects are set to Nothing and the form is closed
using the Unload statement.
16. In the VBA editor use File Close and Return to Gwx32 from the menu to return to
GraphWorX32
17. Change the text of the button to “Send Mail”
How it works:
If the button is pressed the form is opened and the operator can enter an email address where the
email will be sent. The operator can also enter a text message. The operator can press either Cancel
to discard the email or Send to actually send the email.
2-18
ProcessView - VBA Tutorial
2-19
Accessing other Applications
2-20
Chapter 3
Accessing OPC Data via OLE Automation
3.1 Introduction
This last chapter will give an example on how an application that does not have direct access to OPC
Servers, such as ProcessView, is still able to read data from OPC Servers. The example will setup the
connection from Excel but basically any program that has VBA in it for example the Office 97 products
or programming languages such as Visual Basic 5.0 can use this method of connecting to OPC
Servers. The code given in this example can be used in any program except for the last 10 lines,
because these 10 lines enter the read data into Excel.
This program example does not support automatic updates initiated from the server. The user will have
to explicitly ask for the data by running the Excel 97 macro. Of course it is possible to make it change
driver, which means that the OPC Server will inform the OPC Client (Excel in this case) when new
data has arrived, but that is outside the scope of this tutorial.
Sub GenerateReport()
' Define variables
Dim OPCServer As IOPCServerDisp
Dim OPCItemMgt As IOPCItemMgtDisp
Dim OPCItem As IOPCItemDisp
Dim Updaterate As Long
Dim ServerHdl As Long
Dim ItemIDs(50) As String
Dim AccessPaths(50) As String
Dim ServerHandles As Variant
Dim Active(50) As Boolean
Dim Sections As Integer
Dim Units As Integer
Dim ClientHandles(50) As Long
Dim I As Integer
Dim j As Integer
Dim ItemObjects As Variant
Dim Errors As Variant
3.1
ProcessView VBA Tutorial
j = Sections * 7 + Units + 3
Sheets(1).Cells(j, 5).Value = Values(i)
Sheets(1).Cells(j, 6).Value = Values(i + 1)
Sheets(1).Cells(j, 7).Value = Values(i + 2)
Next Units
Next Sections
End Sub
How the VBA program works:
First the program creates an OPC Server object using the CreateObject function. The CreateObject
function takes the name of an OPC Server Automation Interface as the only parameter.
When the OPC Server object is created the program adds a group to it with an update rate of 500 ms.
Once the group is created the different point names are prepared and the different parameters
belonging to the points are set as well. All this information is stored in the following arrays
• ItemIDs contains the names of the points
• Active contains whether or not the points are active, in this example they are always True
• AccessPaths contains the accesspath to the point in this example always ""
• ClientHandles contains the handles associated with the points.
When the points are prepared they are added to the group. After that the values are read and then
stored in the Values array. When the values are read all points are removed, the group is deleted, and
the connection with the OPC Server is terminated. At last the values are written into the Excel
spreadsheet.
Notes:
3.4