VBA Modules, Functions, Variables, and Constants

Slides:



Advertisements
Similar presentations
Sub and Function Procedures
Advertisements

Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Eight Sub and Function Procedures.
JavaScript Part for Repetition Statement for statement Cpecifies each of the items needed for counter-controlled repetition with a control variable.
1.
Chapter 10.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
Using Objects and Properties
Arrays Array of Controls: several controls, of the same type (Class: a prototype for an object indicating the properties and methods), that have the same.
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.
A Guide to Oracle9i1 Advanced SQL And PL/SQL Topics Chapter 9.
1 Chapter 8 Object-Based Programming in VBA. 8 Chapter Objectives Declare and use object variables Create procedures that use built-in form methods Find.
Arrays Array of Controls: several controls, of the same type (Class: a prototype for an object indicating the properties and methods), that have the same.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
VB .NET Programming Fundamentals
Programming with Microsoft Visual Basic 2012 Chapter 7: Sub and Function Procedures.
Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
IE 212: Computational Methods for Industrial Engineering
XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications.
1 Chapter 9 Writing, Testing, and Debugging Access Applications.
Tutorial 11 Using and Writing Visual Basic for Applications Code
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.
1 VBA – podstawowe reguły języka Opracowanie Janusz Górczyński wg Microsoft Help.
1 Web-Enabled Decision Support Systems Objects and Procedures Don McLaughlin IE 423 Design of Decision Support Systems (304)
Using Arrays and File Handling
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
C++ for Engineers and Scientists Second Edition Chapter 6 Modularity Using Functions.
Programming with Microsoft Visual Basic 2008 Fourth Edition
Chapter 6: User-Defined Functions
 2008 Pearson Education, Inc. All rights reserved JavaScript: Functions.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 7 Sub and Function Procedures.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
IMS 3253: Subroutines 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Procedures Subroutines Parameters –By Value.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.
1 Working with Data Structures Kashef Mughal. 2 Chapter 5  Please review on your own  A few terms .NET Framework - programming model  CLR (Common.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Programming with Microsoft Visual Basic th Edition
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Visual Basic CDA College Limassol Campus COM123 Visual Programming 1 Semester B Lecture:Pelekanou Olga Week 5: Useful Functions and Procedures.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
BACS 287 Programming Fundamentals 5. BACS 287 Programming Fundamentals This lecture introduces the following topics: – Procedures Built-in Functions User-defined.
Controlling Program Flow with Decision Structures.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
National Diploma Unit 4 Introduction to Software Development Procedures and Functions.
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 5 Introduction to Defining Classes Fundamentals of Java.
Unit 2 Technology Systems
Chapter 9: Value-Returning Functions
VBA - Excel VBA is Visual Basic for Applications
Microsoft Access Illustrated
User-Defined Functions
Variables and Arithmetic Operations
Exploring Microsoft Office Access 2007
CIS16 Application Development and Programming using Visual Basic.net
CIS16 Application Development and Programming using Visual Basic.net
Chapter 8 Advanced SQL.
Presentation transcript:

VBA Modules, Functions, Variables, and Constants Chapter 7 VBA Modules, Functions, Variables, and Constants

Chapter Objectives Design VBA code that is organized into standard modules, independent class modules, and form and report class modules Determine when each type of module is appropriate Develop simple sub procedures, functions, and property procedures

Chapter Objectives Determine when a sub procedure or a function is most appropriate Declare arguments, variables, and constants Invoke procedures and pass arguments between procedures Cancel and manage events through VBA

Modules Procedure Module Collection of VBA statements combined in a manner to accomplish a processing task Module Group of procedures related to a specific process Makes maintaining the application easier in the long run

Modules Form and report class modules Include code for all event procedures triggered by events occurring on a specific form or report, or the controls on that form or report Figure 7-1 Procedures within modules

Modules Standard module Independent class modules Module you can place sub and function procedures and declarations in to make available to other procedures throughout your application Procedures can be executed whenever the application is running Independent class modules Not connected to a form or report Procedures inside an independent class module can be executed as long as the application is open

Selecting Module Types Selection of the best module type for a procedure depends on: Runtime efficiency of the application Amount of primary memory expected on the computer executing the application Number of forms and reports that use a procedure

Selecting Module Types Standard modules and independent class modules are loaded into primary memory when an application opens Use primary memory even when they are not needed, yet the time needed to load the module occurs once Form and class modules are loaded into primary memory when the form or report opens Do not use memory until form or report is opened, but the form or report takes longer to open because module must be loaded

Sub Procedures Sub procedures Containers for programming code Invoked at any time by writing a statement within a different procedure to invoke the necessary sub procedure Place its name, and if needed, the values for its arguments on a line within the invoking procedure

Functions Functions Return values Invoked by placing it in an expression or using it as a value to display in a control Application can use the value returned by a function in the expression

Property Procedures Property procedure Defines a property that is attached to an object through dot notation Activated by assigning or using the value of a property Used if the procedure will correspond to something that could be a property

Creating and Using Sub Procedures Table 7-1 Data types in VBA and their corresponding types in Access tables

Creating and Using Sub Procedures Table 7-1 Data types in VBA and their corresponding types in Access tables (continued)

Passing Arguments by Reference and by Value When arguments are processed by reference: The memory address containing the value to be used is “referenced” by the argument When arguments are processed by value: A copy of the value, instead of a reference to the memory location containing the value, is supplied to the procedure

Optional Keyword Optional keyword Named arguments approach Can be used with arguments Specifies that the invoking procedure does not need to provide a value for the argument Named arguments approach Arguments can be placed in any order and commas do not have to be used to represent blank arguments

Nuances of Argument Use A procedure must be invoked using the same number of actual arguments as formal arguments in the sub statement, unless optional arguments are used The actual arguments will supply values for the formal arguments in the order in which they are listed, so the data types must be compatible

Event Procedure Arguments Event procedures Special types of sub procedures located in form and report class modules Most do not take arguments, but some have one or more predefined arguments Flags Arguments that contain values that Access evaluates during and after the procedure executes to determine how to continue the application’s processing

Event Procedure Arguments Table 7-2 Event procedures with single arguments

Event Procedure Arguments Intrinsic constants Represent the legal values for arguments that specify actions other than canceling events True Intrinsic constant that represents value –1

Event Procedure Arguments Table 7-3 Event procedure with multiple or special arguments

Event Procedure Arguments Table 7-3 Event procedure with multiple or special arguments (continued)

Creating and Using Functions Function Procedures Similar to sub procedures Use formal arguments, can be declared as Public or Private Can contain variable and constant declaration statements In a function procedure, the function is assigned a value Assignment is fundamental difference between sub procedures and functions

Property Procedures – Let, Set, and Get Property Let and Set procedures Accept information from an invoking procedure, but do not return a value Property Get procedures Returns a value to the calling procedure Property Let and Get usually appear in pairs

Property Procedures – Let, Set, and Get Figure 7-2 Property procedures for frmIDSearchWindow

Declaring Variables Dim Statements Define variables that will be used within procedures Variables are frequently used to store immediate results as execution of the procedure moves from one line to another

Declaring Variables Figure 7-3 Part of txtPaymentNo_BeforeUpdate in frmProcessPayment

Declaring Variables Private Public Used in the Declarations section of a module to declare variables that are used only within the procedure contained by the module Public Used in the Declarations section to declare variables that can be used by any procedure in any module

Declaring Variables Static Used in a procedure to declare variables that retain their value even after the procedure finishes executing May be declared only within procedures

The Variant Data Type Variant data type VarType built-in function Can contain almost any type of data, including numbers, strings, and dates Only data type in VBA that can store null and empty values VarType built-in function Used to determine the type of data stored in a Variant

The Variant Data Type Table 7-4 Values returned by VarType and variable testing functions

The User-Defined Type VBA provides a way to create a user-defined type with a Type statement Allows you to define a complex data structure Useful when the values of many similar variables need to be copied to other variables

Single- and Multiple- Dimension Arrays Collections of variables given a single name but indexed by one or more values Array indexes begin at zero ReDim statement Can change the size of the array

Scope, Life, and the Dim Statement Where and how variables are declared determine which other procedures can use the variable Scope of a variable How long the variable actually exists Life of a variable

Scope, Life, and the Dim Statement Table 7-5 Variable and constant scope prefixes

Scope, Life, and the Dim Statement Figure 7-4 Procedure that facilitates the reuse of values

Scope, Life, and the Dim Statement Any variable declared in the Declarations section of a module maintains its stored value as long as: The application continues to run or The form or report remains open Variables declared with the Dim statement in a procedure are available to that procedure only as long as the procedure is running

Scope, Life, and the Static Statement Variables declared with the Static statement in a procedure continue to exist after the procedure finishes Programmers frequently use static variables to determine whether a newly entered value is the same as a previous value

Intrinsic and Symbolic Constants Named item that retains the same value throughout the execution of a program Intrinsic constant Integer values that have some particular meaning Makes programming easier for the developer and facilitates program maintenance

Intrinsic and Symbolic Constants Constant that is defined by the programmer Retains a constant value throughout the execution of a procedure Similar to a variable except that its value is specified as soon as the constant is declared

Intrinsic and Symbolic Constants Figure 7-5 Public constants in the Declarations section of the basUtilities module

Controlling Processing in the Declarations Section Option Compare statement Automatically included in Declarations section Defines the default method to use when comparing text data Three types of methods used when comparing text data: Binary method Comparisons will be case-sensitive

Controlling Processing in the Declarations Section Three types of methods used when comparing text data (continued): Text method Comparisons will not be case sensitive Database method Comparisons are based on character’s relative positions according to the sort order specified when the database was created or compacted

Controlling Processing in the Declarations Section If the Declarations section of the module containing the procedure contains an Option Explicit statement: Variables must be explicitly defined prior to their use If it does not: Variables do not need to be declared before they are used

Chapter Summary VBA statements are written inside functions procedures, sub procedures, and property procedures Standard modules Contain functions and sub procedures Loaded into memory as soon as the application opens Form and Report class modules Contain all three types of procedures

Chapter Summary Functions differ from sub procedures in two ways A function returns a value A function has an associated data type Functions should be written in cases where a value is being returned

Chapter Summary All procedures have arguments Argument values can be passed by reference Changes made to the value passed into the procedure are reflected in the environment where the procedure was originally invoked by value Make a copy of the actual argument value available to the procedure

Chapter Summary Variables Can be local or they can be public Usually only available while the code in which they have been declared is executing Deleted from memory unless they have been declared as static May be declared as one of a number of different data types

Chapter Summary Arrays Constants Collections of indexed variables with a common name, and user defined types, which allow programmers to create their own data structures Constants Help make the program code more understandable