Date post: | 01-Jan-2016 |
Category: |
Documents |
Upload: | patience-smith |
View: | 227 times |
Download: | 3 times |
Outline Software and Programming Program Structure Tools for Designing Software Programming Languages Introduction to Visual Basic (VBA)
Types of Programming Procedural
Strict sequence of processing steps Distinct start/stop points in program
#include <stdio.h> main() { printf("Content-type: text/html\n\n"); printf("<html>"); printf("Hello World!"); printf("</html>"); }
Types of Programming Event-Driven
Waits for an “event” to trigger program execution
Modular programming No start/stop point in program
Types of Programming Event-Driven
Average program
Get data, accumulatesum
Compute & displayaverage
Get dataInitialize count, sumIncrement count,accumulate sum
Compute & displayaverage (normal)
Compute & displayaverage (no data)
Event-DrivenPrivate Sub cmbDates_Click()
Me.txtSSNScanned.SetFocusCall scanSSN
End Sub
Private Sub cmdEndProgram_Click()If endProgram Then EndElse endProgram = FalseEnd If
End Sub
Private Sub txtSSNScanned_GotFocus()If Me.cmbDates.Text = "" Then MsgBox ("You need to enter a date!") Me.cmbDates.SetFocus Exit SubEnd If
End Sub
Procedure #2
Procedure #3
Procedure #1
Programming Languages Assembly Language
Fast and most efficient Very difficult to program in
Basic Beginner’s All-purpose Symbolic Instruction Code
Easy to use Lacks total control
Programming Languages Fortran
FORmula TRANslator Great for mathematical programming Not good for other uses, e.g. graphics
C and C++ Powerful tools Harder to use/learn than BASIC Can do anything
Programming Languages Visual Basic
Develop windows programs easily Powerful tool Not as much control as Visual C VBA (Visual Basic for Applications)
Visual C and C++ Develop windows applications Not easy to learn
VBA Macro Programming VBA is a general purpose programming language
that comes standard with Excel or Office. Using VBA with Excel, powerful engineering
analysis tools can be developed quickly and with minimum cost.
VBA can be used for many engineering tasks communicating with engineering databases analyzing engineering data automating worksheet construction engineering modeling and simulation creating charts and engineering wizards (i.e. dialog boxes) creating GUI's
Advantages of VBA Programmer does not need to be an
expert Windows programmer Programmer creates GUI and
defines what happens when user interacts with it
Events are generated Push a button, move mouse, etc.
This is called event-driven programming
Elements of a Visual Basic
Objects & Events
Label
Text box
Picture box (text)
Picture box (image)
Command button
Object Properties The most important properties (at
least for the purpose of this course) are: Name (object’s name – important!!) Caption (text to be displayed by the
object) Font (what the text looks like) Visible (how it links to other
applications)
Visual Basic Events Change initiated by user
Load a Form Click on a button Mouse down/up, Mouse drag Key down, key press, etc.
Causes an event procedure (subprogram) to execute
Elements of Visual Basic Data
Constants (data that does not change) Numbers (e.g., 1234.56) Strings (e.g., “this is a string”)
Variables (names of data holders) Numbers Strings
Operators Arithmetic (+, -, *, /, etc.) String (&)
Visual Basic Integers Sequence of digits with no
Commas Decimal points
Negative numbers preceded by ‘-’ Positive numbers optionally
preceded by ‘+’
Single Precision Real Numbers Standard Representation
Integer (whole number) portion Decimal point Unsigned integer (fractional) portion
Visual Basic Strings String:
Sequence of characters (a, b, c, ..., 0, 1, 2, ..., ~, !, ...) treated as a unit
Enclosed in double quotes in VB statement
“this is a string” “John Smith” “737-2357” “a + b = “
Visual Basic Variables Variable
Symbolic name for data value Name of data “holder” Name of a location in random access memory
Variable names rules Must begin with a letter May contain only letters, digits, and
underscores (_) Up to 255 characters long
Variable Names a, b, c, ..., x, y, z distance, speed, time, average rateOfIncrease, startingTime,
hoursPerWeek rate_of_increase, starting_time,
hours_per_week
Declaring Variable Types Two options
Implicit declaration Explicit declaration
Explicit declaration Dim name As String
Dim count As IntegerDim average As SingleDim nextItem As Variant
Explicit declaration is always preferable!!
VB Arithmetic Operators Addition (+) a + b Subtraction (-) a - b Multiplication (*) a * b Division (/) a / b Exponentiation (^) a ^ b
Arithmetic Order of Evaluation
1. Exponentiation (R - L)2. Multiplication & division (L-R)3. Addition & subtraction (L-R)
Converting Formulas to VB 1a + b
1c
1d
ef
gh
+ -
2
(1.0 / (a + b)) ^ 2 / ((1.0 / c + 1.0 / d) * (e / f - g / h))