Enhancing User Interaction Through Programming

Slides:



Advertisements
Similar presentations
Office 2003 Post-Advanced Concepts and Techniques M i c r o s o f t Excel Project 7 Using Macros and Visual Basic for Applications (VBA) with Excel.
Advertisements

Using Macros and Visual Basic for Applications (VBA) with Excel
Developing an Excel Application
Tutorial 8: Developing an Excel Application
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Tutorial 12: Enhancing Excel with Visual Basic for Applications
VBA Modules, Functions, Variables, and Constants
Exploring Office Grauer and Barber 1 Creating More Powerful Applications: Introduction to VBA(Wk9)
1 Chapter 4 The Fundamentals of VBA, Macros, and Command Bars.
SUNY Morrisville-Norwich Campus-Week 12 CITA 130 Advanced Computer Applications II Spring 2005 Prof. Tom Smith.
Using the Visual Basic Editor Visual Basic for Applications 1.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Chapter 2: The Visual Studio.NET Development Environment Visual Basic.NET Programming: From Problem Analysis to Program Design.
Office 2003 Post-Advanced Concepts and Techniques M i c r o s o f t Word Project 8 Working with Macros and Visual Basic for Applications (VBA)
COMPREHENSIVE Excel Tutorial 8 Developing an Excel Application.
Exploring Microsoft Excel 2002 Chapter 8 Chapter 8 Automating Repetitive Tasks: Macros and Visual Basic for Applications By Robert T. Grauer Maryann Barber.
Chapter 9 Introduction to ActionScript 3.0. Chapter 9 Lessons 1.Understand ActionScript Work with instances of movie clip symbols 3.Use code snippets.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
A First Program Using C#
Visual Basic Chapter 1 Mr. Wangler.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications.
XP Chapter 7 Succeeding in Business with Microsoft Office Access 2003: A Problem-Solving Approach 1 Enhancing User Interaction Through Programming Chapter.
Automating Tasks with Visual Basic. Introduction  When can’t find a readymade macro action that does the job you want, you can use Visual Basic code.
Tutorial 11 Using and Writing Visual Basic for Applications Code
INSERT BOOK COVER 1Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Microsoft Office Access 2010 by Robert Grauer, Keith.
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
1 Visual Basic for Applications (VBA) for Excel Prof. Yitzchak Rosenthal.
® Microsoft Access 2010 Tutorial 11 Using and Writing Visual Basic for Applications Code.
WEEK 3 AND 4 USING CLIENT-SIDE SCRIPTS TO ENHANCE WEB APPLICATIONS.
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 Microsoft Office Excel Copyright © 2008 Pearson Prentice Hall. All rights reserved
Automating Database Processing Chapter 6. Chapter Introduction Design and implement user-friendly menu – Called navigation form Macros – Automate repetitive.
Using Visual Basic for Applications (VBA) – Project 8.
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 11 Committed to Shaping the Next Generation of IT Experts. Chapter 10 Customizing a Database.
 Application – another name for a program.  Interface – is what appears on the screen when the application is running.  Program Code – is instructions.
Chapter One An Introduction to Visual Basic 2010 Programming with Microsoft Visual Basic th Edition.
Program Design and Coding
Microsoft Visual Basic 2012 CHAPTER THREE Program Design and Coding.
Microsoft Visual Basic 2010 CHAPTER THREE Program Design and Coding.
Microsoft Visual Basic 2005 CHAPTER 4 Variables and Arithmetic Operations.
OCC Network Drives  H:\  P:\ 
Introduction to Programming with RAPTOR
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 8 Debugging, Creating Executable Files, and Distributing a Windows Application.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
INSERT BOOK COVER 1Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Getting Started with VBA for Microsoft Office 2010 by.
CHAPTER 6 LESSON B Creating Custom Forms. Lesson B Objectives  Suppress default system messages  Create alerts and messages to provide system feedback.
Copyright (c) 2003 by Prentice Hall Provided By: Qasim Al-ajmi Chapter 2 Introduction to Visual Basic Programming Visual Basic.NET.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
COMPREHENSIVE Access Tutorial 11 Using and Writing Visual Basic for Applications Code.
Microsoft Visual Basic 2012: Reloaded Fifth Edition Chapter One An Introduction to Visual Basic 2012.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Chapter 10 Using Macros, Controls and Visual Basic for Applications (VBA) with Excel Microsoft Excel 2013.
Chapter 2: The Visual Studio.NET Development Environment Visual Basic.NET Programming: From Problem Analysis to Program Design.
COMPREHENSIVE Excel Tutorial 12 Expanding Excel with Visual Basic for Applications.
Excel Tutorial 8 Developing an Excel Application
A variable is a name for a value stored in memory.
Chapter 2: The Visual Studio .NET Development Environment
Microsoft Access 2003 Illustrated Complete
Microsoft Access Illustrated
Using Procedures and Exception Handling
Microsoft Office Illustrated
Objectives Learn about Function procedures (functions), Sub procedures (subroutines), and modules Review and modify an existing subroutine in an event.
Variables and Arithmetic Operations
Exploring Microsoft Office Access 2007
Exploring Microsoft Excel
WEB PROGRAMMING JavaScript.
Tutorial 11 Using and Writing Visual Basic for Applications Code
Presentation transcript:

Enhancing User Interaction Through Programming Chapter 7 Enhancing User Interaction Through Programming Use the quote as a jumping off point for discussion, if desired.

Chapter Introduction Visual basic for applications (VBA) Fully customize and automate a database Perform more complex validity Use functions and actions not available with macros Succeeding in Business with Microsoft Access 2010

Tools Covered in This Chapter Assignment statement Breakpoints Code window DateDiff function DateSerial function Debug DoCmd statement Succeeding in Business with Microsoft Access 2010

Tools Covered in This Chapter (continued) If statement Immediate window Variables Visual Basic editor Visual Basic for Applications Watches window Succeeding in Business with Microsoft Access 2010

Level 1 Objectives: Writing Visual Basic for Applications Code Understand Visual Basic for Applications programming Design, create, and test a subroutine in a standard module Design, create, and test an event procedure Design, create, and test a function in a standard module Succeeding in Business with Microsoft Access 2010

Understanding Visual Basic for Applications Programming language for Microsoft Office programs Including Access Common syntax and set of common features for all Microsoft Office programs Features unique for each Microsoft Office program Use programming language Write set of instructions to direct computer to perform specific operations in specific order Succeeding in Business with Microsoft Access 2010

Understanding Visual Basic for Applications (continued) Coding Statement Event-driven language Object-oriented language Succeeding in Business with Microsoft Access 2010

Event-Driven Programming State condition or occurrence that Access recognizes Has associated event property Appear in property sheet for forms reports, and controls Create group of statements using VBA code Set event property value to name of that group of statements Event procedure Succeeding in Business with Microsoft Access 2010

Frequently Used Access Events Succeeding in Business with Microsoft Access 2010

Common Events for Selected Controls Succeeding in Business with Microsoft Access 2010

Coding VBA Types of procedures Function procedures Sub procedures Performs operations Returns value Accepts input values Can be used in expressions Sub procedures Performs operations and accepts input values Does not return value Cannot be used in expressions Succeeding in Business with Microsoft Access 2010

Coding VBA (continued) Module Group of related procedures Starts with declarations section One or more procedures Basic types of modules Standard modules Database object stored in memory Use from anywhere in database Class modules Usually associated with particular form or report Succeeding in Business with Microsoft Access 2010

Coding VBA (continued) Public procedure More than one object can use Local procedure or a private procedure Only the form or report for which class module created can use event procedure Succeeding in Business with Microsoft Access 2010

Creating a Subroutine in a Standard Module Replace macros with VBA procedures Better error handling and greater capabilities Some tasks cannot be performed with VBA AutoKeys AutoExec Succeeding in Business with Microsoft Access 2010

Creating a New Standard Module Click Create tab Click Module button in the Macros & Code group Begin new procedure in module Click Insert menu Click procedure Type name Select options Succeeding in Business with Microsoft Access 2010

Creating a New Standard Module (continued) Visual Basic Editor Create and modify VBA code Visual Basic window Program window that opens within VBE Code window Window to create modify and display specific VBA procedures Can have as many code windows open as modules in database Succeeding in Business with Microsoft Access 2010

Creating a New Standard Module (continued) Sub statement Ends with End Sub statement Includes Scope of procedure Name of procedure Opening and closing parenthesis Option compare statement Designates technique access uses to compare and sort text data Succeeding in Business with Microsoft Access 2010

Creating a Subroutine Type statements in procedure between Sub and End Sub statements DoCmd statement Executes action in procedure Access object Method Function that operates on specific objects or controls Succeeding in Business with Microsoft Access 2010

Creating a Subroutine (continued) Comment Include anywhere in VBA procedure Describe what procedure or statement does Make it easier for programmers to identify purpose of statements Begin comment with Word Rem (for “remark”) Or single quotation mark (') Appear green in code window Succeeding in Business with Microsoft Access 2010

Creating a Subroutine (continued) Line label Statement that serves as starting point for block of statements in procedure Begins at start of line and ends with colon Underscore character at end of line Statement continues to next line Run procedure Click Run Sub/UserForm button Saves module Click Save button Succeeding in Business with Microsoft Access 2010

Designing an Event Procedure Associate procedure with form’s On Current event property Triggered when form’s Current event occurs Occurs when focus moves to record Add event procedure Open property sheet for form Click On Current list arrow Click [event procedure] Click Build button to right of On Current text box Succeeding in Business with Microsoft Access 2010

Using the If Statement in Decision Making Decision-making statement Executes group of statements based on outcome of condition In simplest statement, Access executes a group of statements if the condition is true Executes nothing if False Succeeding in Business with Microsoft Access 2010

Using the If Statement in Decision Making (Cont.) Tests condition follows one of two paths depending on outcome of condition General form If condition Then True-statement group [Else False-statement group] End If Succeeding in Business with Microsoft Access 2010

Using the If Statement in Decision Making (Cont.) Assignment statement Assigns value of expression to control or property Example EstSalary.ForeColor = vbGreen ForeColor property Color constants Predefined VBA names Have values that represent system color value IsNull function returns True value when field or control null False when not Succeeding in Business with Microsoft Access 2010

Testing an Event Procedure Switch to form view Navigate through records Make sure results correct in different situations Succeeding in Business with Microsoft Access 2010

Using a Function in a Standard Module Similar calculation requests Create function to perform calculation Eliminates possibility of error when entering code in more than one place Function Begins with Function statement Ends with End Function statement Succeeding in Business with Microsoft Access 2010

Using Functions Similar to way built-in functions used Call a function Function name followed by argument(s) Commas separate multiple arguments DateDiff function Calculates number of time intervals between two dates Succeeding in Business with Microsoft Access 2010

Using Variables Named location in computer memory Can contain value Use to store and retrieve data Reference using variable name assigned to location Holds only one value at a time Succeeding in Business with Microsoft Access 2010

Using Variables (continued) Naming rules (applies to subroutine and function names also) Must begin with letter Cannot exceed 255 characters Include letters numbers and underscore character Cannot use space, punctuation, or special characters Cannot be same as keywords or reserved words Each variable has data type Succeeding in Business with Microsoft Access 2010

Common VBA Data Types Succeeding in Business with Microsoft Access 2010

Testing a Function in the Immediate Window When statement entered Editor checks statement to make sure syntax correct Logic error Occurs when procedure produces incorrect results Immediate window Test VBA procedures without changing data in database Enter different values to test procedure Succeeding in Business with Microsoft Access 2010

Testing a Function in the Immediate Window (continued) Type keyword “print” or question mark (?) Followed by procedure name and argument values to test in parentheses and separated by commas Access executes the function and prints the value returned by the function Succeeding in Business with Microsoft Access 2010

Level 1 Summary VBA Create procedures in module Programming language for Microsoft Office programs Create procedures in module Event-driven programming Program statement execution triggered by events If statement Used to make decisions Function Returns a value Succeeding in Business with Microsoft Access 2010

Level 2 Objectives: Using Custom Functions and Verifying Data Design create and test a custom function in an event procedure Verify data using event procedures Use the Case control structure Succeeding in Business with Microsoft Access 2010

Enhancing an Event Procedure to Use a Custom Function Add and change statements in a procedure Open object that contains event procedure in Design view Add new procedure module or class module Click Insert on menu bar Click Procedure, Module, or Class Module Edit existing statements Scroll code window to desired statement Make necessary changes Press Save button on Visual Basic toolbar Succeeding in Business with Microsoft Access 2010

Using an ElseIf Statement in an If Statement Date function Returns current computer system date ElseIf statement Equivalent to Else clause followed by If statement Choice of which If statement version used Matter of personal preference Arranging order of condition testing Order of condition testing critical Place conditions in order from least inclusive to most inclusive Succeeding in Business with Microsoft Access 2010

Comparing an ElseIf Clause to an Else…if Clause Succeeding in Business with Microsoft Access 2010

Using an ElseIf Statement in an If Statement (continued) Dim statement Declare variables and associated data types in procedure Succeeding in Business with Microsoft Access 2010

Verifying Data with VBA Before Update event Occurs before changed data in control or record updated in database Use to verify entered data Succeeding in Business with Microsoft Access 2010

Designing the Field Validation Procedure to Verify Zip Codes Cancel As Integer Defines Cancel as parameter with Integer data type For some events access executes attached event procedure before performing default behavior Can cancel default behavior in event procedure or macro Using Cancel parameter Left function Returns string containing specified number of characters from left side of specified string Succeeding in Business with Microsoft Access 2010

Designing the Field Validation Procedure to Verify Zip Codes (continued) Val function Returns numbers contained in specified string as numeric value Succeeding in Business with Microsoft Access 2010

Using the Case Control Structure Set of VBA statements work together as a unit Case control structure Evaluates expression Performs one of several alternative sets of statements Based on resulting value (or condition) of evaluated expression Use Case Else statement as last Case statement Include false-statement group Succeeding in Business with Microsoft Access 2010

Using the Case Control Structure (continued) CancelEvent action Cancels event that caused procedure or macro containing action to execute SetFocus Moves focus to specified object or control Succeeding in Business with Microsoft Access 2010

Testing the Field Validation Procedure Switch to form view to test validation procedure Using event procedure for control or form Before Update event for each control on form Disadvantage All changes made to record canceled Succeeding in Business with Microsoft Access 2010

Designing the Field Validation Procedure to Verify Phone Area Codes Must test first three digits of phone number Message can suggest potential error Must accept any phone area code entry Succeeding in Business with Microsoft Access 2010

Level 2 Summary ElseIf statement Case Select statement Verify data using VBA Succeeding in Business with Microsoft Access 2010

Level 3 Objectives: Testing and Exploring VBA Troubleshoot VBA procedure errors Compile modules Develop sources for learning VBA Succeeding in Business with Microsoft Access 2010

Troubleshooting VBA Procedures Syntax error Occurs when VBA statement violates language rules for statement such as Misspelling Incorrect sequence of keywords Missing parenthesis Detected immediately when statement completed Error message opens and explains error Succeeding in Business with Microsoft Access 2010

Syntax Error Message Succeeding in Business with Microsoft Access 2010

Troubleshooting VBA Procedures (continued) Compilation error Occurs when procedures translated into form computer can understand Compilation Process of translating modules from VBA to form computer understands Succeeding in Business with Microsoft Access 2010

Troubleshooting VBA Procedures (continued) Prevent compilation errors at run time Use Compile command on Debug menu Compile as last step before saving module Execution error or run-time error Occurs when procedure executes and stops because it tries to perform operation that is impossible to perform Succeeding in Business with Microsoft Access 2010

Using the Debugger to Correct Errors Breakpoint Run subroutine or function up until line on which breakpoint set Procedure halts execution at point and displays module screen Good way to isolate place at which procedure stops producing anticipated result Succeeding in Business with Microsoft Access 2010

Using the Debugger to Correct Errors (continued) Use mouse pointer to get more information about procedure Pointing to variable name displays ScreenTip with value contained in variable at point that procedure halted Execute procedure one statement at a time Try and identify cause of error Click Debug on menu bar Click Step Into Succeeding in Business with Microsoft Access 2010

Using the Debugger to Correct Errors (continued) Step Into Procedure executes next statement and stops Step Out Executes current subroutine or function and then halts Reset button Stops debugger so you can fix problem Recompile and save When done, click Debug and Clear all Breakpoints Succeeding in Business with Microsoft Access 2010

Identifying Errors Using the Watches Window Shows current value of specified variable Add Watch dialog box Enter variable name to watch “<Out of context>” in value column Appears for watch expression When access not executing procedure that contains watch expression Succeeding in Business with Microsoft Access 2010

Add Watch Dialog Box Succeeding in Business with Microsoft Access 2010

Building Procedures Slowly to Isolate Problems Prevent problems in procedures Build them slowly using small groups of statements Prove them to be correct as you go Easier to correct small groups than long procedure Succeeding in Business with Microsoft Access 2010

Using Help to Learn More About Programming and VBA Access Help contains valuable information about VBA Can access Office.com for help if online If not online, uses Help downloaded at installation Search for a topic Returns results list Can view sample code for situations Succeeding in Business with Microsoft Access 2010

Visual Basic Help Table Of Contents Succeeding in Business with Microsoft Access 2010

Level 3 Summary Errors Debugger Help Syntax Compilation Execution or run-time Debugger Use to find and correct errors Help Learn more about VBA Succeeding in Business with Microsoft Access 2010

Chapter Summary VBA Decision structures Develop subroutines Programming language for Microsoft Office applications Decision structures If ElseIf Case Develop subroutines Use debugger to find errors Succeeding in Business with Microsoft Access 2010