154 Student Guide
15. Introduction to Visual Basic for Applications _________________
Chapter 15
Introduction to Visual Basic for Applications
Copyright © 2000-2003, GE Fanuc International, Inc. 15-1 154 v3.0 -–01.03
iFIX Fundamentals
15. Introduction to Visual Basic for Applications _________________
Section Objectives
This section begins a topic that will be used throughout the remainder of the course. That topic
is scripting. iFIX uses Visual Basic for Applications (VBA) as its scripting language. Scripting
can be used in many places throughout iFIX. This section includes:
• Introduction to Visual Basic for Applications
• Differences between Visual Basic and VBA
• Using VBA within iFIX
Many of the terms introduced in this section will be referenced later in the course.
Copyright © 2000-2003, GE Fanuc International, Inc. 15-2 154 v3.0 -–01.03
154 Student Guide
15.1. Application Development Flowchart ________________________
SCADA Server iClient (HMI)
System System
Configuration Configuration
I/O Driver
Configuration
Graphics
Development
Process Database
Development
Alarm Alarm
Configuration Monitoring
Security Security
Configuration Configuration
Configure Archived Data
Data Archiving Monitoring
Figure 15-1: Application Development Flowchart (showing Graphics Development)
Copyright © 2000-2003, GE Fanuc International, Inc. 15-3 154 v3.0 -–01.03
iFIX Fundamentals
15.2. Visual Basic for Applications Overview _____________________
A. Terminology:
• VB - Visual Basic
- usually refers to Microsoft® Visual Basic® 5.0 or 6.0
- this is external to iFIX and VBA
• VBA - Microsoft® Visual Basic® for Applications
- an object-oriented and event-driven development environment that
supports Microsoft forms and ActiveX
- usually referred to as it is integrated into iFIX
- used throughout Microsoft products for customization
◊ adding a toolbar in Excel
◊ creating a form in Access
• VBE - Visual Basic Editor
- part of VBA
- accessible from the Intellution WorkSpace
B. Differences between Visual Basic for Applications and Visual Basic
• Visual Basic uses Visual Basic for Applications as its central code engine
• VB allows you to create executables and ActiveX Controls
• VBA allows you to customize and to extend the capabilities of a product
• Slight variations in the specific names of some events and properties
Copyright © 2000-2003, GE Fanuc International, Inc. 15-4 154 v3.0 -–01.03
154 Student Guide
15.2. Visual Basic for Applications Overview ____________________
Visual Basic Visual Basic
for
Applications
Figure 15-2: VBA versus VB
Copyright © 2000-2003, GE Fanuc International, Inc. 15-5 154 v3.0 -–01.03
iFIX Fundamentals
15.3. Using VBA in iFIX________________________________________
A. Within iFIX, use VBA to:
• Animate objects in pictures
• Automatically generate pictures or objects
• Read from and write to database blocks
• Automatically run other applications
• Incorporate custom security features
• Create custom prompts and messages for operators
• Access ODBC data sources
• Incorporate and communicate with third-party and custom ActiveX controls
• Write custom wizards for frequently-performed tasks
B. Overview
• VBA code is saved in either:
- a picture (GRF file)
- a schedule (EVS file)
- a global object (User.fxg)
• Scripting in iFIX allows access to:
- standard VBA components
- all exposed iFIX objects and their properties, methods, and events
◊ see Figure 15-3 for definitions
• The properties, methods, and events available for an individual object are based
on the object class
- Example:
◊ Rectangle objects do not have the same font properties as text
objects
• Objects respond to events based on scripts that are written for each event
- scripts can be written for one event, some events, or all events, depending
on what needs to be accomplished
Copyright © 2000-2003, GE Fanuc International, Inc. 15-6 154 v3.0 -–01.03
154 Student Guide
15.3. Using VBA in iFIX ______________________________________
VBA Terminology
Objects Comprised of data and procedures that are treated as a unit
Each object has its own properties, methods, and events that can
be used to write scripts
Examples: Rectangle, Timer, Schedule Event
Properties Characteristics of an object
Examples: color, size, location in the picture
Methods Subroutines that effect the behavior of object
Examples: such as scale or refresh
Events Actions that the object recognizes, such as when a user clicks the
mouse on the object or changes its size
If a script is associated with an event, the script executes when
the event triggers or occurs
Events can occur as a result of user action, program code, or other
occurrences in iFIX
Examples: mouse click, value reaches a threshold
Figure 15-3: VBA Terminology
Copyright © 2000-2003, GE Fanuc International, Inc. 15-7 154 v3.0 -–01.03
iFIX Fundamentals
15.3. Using VBA in iFIX ______________________________________
C. Assigning scripts to objects
• There are two ways to assign VBA scripting to objects:
- using Command Experts
- using the Visual Basic Editor (VBE)
D. Task Wizard
• Tool used to access all experts
• Experts are separated into categories:
- Animation
- Command
- Database
- Data Entry
- Picture
- Report
E. Using Command Experts
• These experts are object based
• Code is inserted for common actions
• Examples:
- Open a picture
- Write a value to database block
• Using the Experts:
- select the object the script is to be assigned to
- select the Command Expert button for the desired command
Copyright © 2000-2003, GE Fanuc International, Inc. 15-8 154 v3.0 -–01.03
154 Student Guide
15.3. Using VBA in iFIX ______________________________________
Data Entry
Open Picture
Close Picture
Replace Picture
Open Digital Tag
Close Digital Tag
Toggle Digital Tag
Acknowledge Alarm
Figure 15-4: Command Experts
Copyright © 2000-2003, GE Fanuc International, Inc. 15-9 154 v3.0 -–01.03
iFIX Fundamentals
15.4. VBA Examples __________________________________________
A. Example
• VBE – Visual Basic Editor
• Figure 15-5 shows an example of a simple script using the VBE
- Line 1 - creates the script (or subroutine) called Rect5_Click()
◊ this is a script connected to an object called Rect5
◊ the script is run when the Rect5 object is clicked
- Line 2 - runs the command CloseDigitalPoint
◊ this sends a value of 1 to the data source listed
- Line 3 - tells the script to stop executing
• See pages 15-12 and 15-13 for additional sample scripts
Copyright © 2000-2003, GE Fanuc International, Inc. 15-10 154 v3.0 -–01.03
154 Student Guide
15.4. VBA Examples _________________________________________
Figure 15-5: Sample Script
Copyright © 2000-2003, GE Fanuc International, Inc. 15-11 154 v3.0 -–01.03
iFIX Fundamentals
15.4. VBA Examples _________________________________________
Private Sub RoundRect1_Click()
OpenPicture "Picture2"
End Sub
Figure 15-6: Scripting Example 1
Private Sub Rect1_Click()
WriteValue 30, "fix32.node8.ao1.f_cv"
End Sub
Figure 15-7: Scripting Example 2
Private Sub Oval1_DblClick()
AcknowledgeAllAlarms "Picture2"
End Sub
Figure 15-8: Scripting Example 3
Copyright © 2000-2003, GE Fanuc International, Inc. 15-12 154 v3.0 -–01.03
154 Student Guide
15.4. VBA Examples _________________________________________
Private Sub Rect2_Click()
Rect2.RotationAngle = Rect2.RotationAngle + 20
End Sub
Figure 15-9: Scripting Example 4
Private Sub Rect3_Click()
Dim iValue As Integer
iValue = ReadValue("fix32.node8.AI1.f_cv")
If iValue < 50 Then
RampValue "50", False, "AO1"
Else
MsgBox "Value over 50"
End If
End Sub
Figure 15-10: Scripting Example 5
Copyright © 2000-2003, GE Fanuc International, Inc. 15-13 154 v3.0 -–01.03
iFIX Fundamentals
15.5. Summary Questions _____________________________________
1. What are the differences between VB and VBA?
2. Describe some of the places in iFIX where scripting can be used.
3. Give examples of properties used in iFIX.
4. Give examples of events used in iFIX.
5. Give examples of methods used in iFIX.
Copyright © 2000-2003, GE Fanuc International, Inc. 15-14 154 v3.0 -–01.03