Scope Lifetime Modules Procedures. Scope? Where can your variables be seen? Where used? Where abused (reseting the value)? Local and Global = Private.

Slides:



Advertisements
Similar presentations
Chapter 3: Using Variables and Constants Programming with Microsoft Visual Basic 2005, Third Edition.
Advertisements

AE6382 VBA - Excel l VBA is Visual Basic for Applications l The goal is to demonstrate how VBA can be used to leverage the power of Excel u VBA syntax.
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
VBA Modules, Functions, Variables, and Constants
String Variables Visual Basic for Applications 4.
Scope of Variables and Constants A Variable or Constant may exist and be Visible for an entire project, for only one form, or for only one procedure Therefore,
Data Types and Operations Programming Fundamentals (Writing Code)Programming Fundamentals (Writing Code)
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
Chapter 1 Program Design
5.05 Apply Looping Structures
VBA & Excel Barry L. Nelson IEMS 465 Fall Quarter 2003.
Chapter Three Using Variables and Constants Programming with Microsoft Visual Basic th Edition.
Creating Embedded Formative Assessment Dr. Steve Broskoske Misericordia University EDU 533 Computer-based Education.
Chapter 3: Using Variables and Constants
Programming with Microsoft Visual Basic th Edition CHAPTER THREE USING VARIABLES AND CONSTANTS.
Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #4: Working with Variables and User Interfaces IE 212: Computational Methods for Industrial Engineering.
Tutorial 11 Using and Writing Visual Basic for Applications Code
Chapter 3 Programming Fundamentals Writing Code 3 Exploring Microsoft Visual Basic 6.0 Copyright © 1999 Prentice-Hall, Inc. By Carlotta Eaton.
1 VBA – podstawowe reguły języka Opracowanie Janusz Górczyński wg Microsoft Help.
CHAPTER THREE Representing Data: Constants and Variables.
Chapter 17: Arrays Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 Microsoft Office Excel Copyright © 2008 Pearson Prentice Hall. All rights reserved
Lecture Set 5 Control Structures Part D - Repetition with Loops.
06/10/ Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
1 Chapter 5 - General Procedures 5.1 Function Procedures 5.2 Sub Procedures, Part I 5.3 Sub Procedures, Part II 5.4 Modular Design.
Access VBA Programming for Beginners - Class 2 - by Patrick Lasu
VBA Lab 2 I ns.Samia Al-blwi. Visual Basic Grammar Object: Visual Basic is an object-oriented language. This means that all the items in Excel are thought.
30/10/ Iteration Loops Do While (condition is true) … Loop.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Visual Basic Programming
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Overview of VBA Programming & Syntax. Programming With Objects u Objects –Properties: attributes or characteristics of an object (e.g., font size, color,
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.
JavaScript, Fourth Edition
ME 142 Engineering Computation I Using Subroutines Effectively.
Lab 6 (2) Arrays ► Lab 5 (1) Exercise Review ► Array Concept ► Why Arrays? ► Array Declaration ► An Example of Array ► Exercise.
Chapter 3 w Variables, constants, and calculations DIM statements - declaration temporary memory locations identifier, data type, scope data types - values.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Three Using Variables and Constants.
CompMathBSc, English 5 October 2006 Programming basics — continued  Arrays  Cycle Statements: Loops  Control Structures vs Conditions  Subs: Procedures.
Advanced Programming Strings Arrays Arguments Modulus.
ME 142 Engineering Computation I Using Subroutines Effectively.
Debugging, Static Variables, ByRef, ByValue Chapt. 6 in Deitel, Deitel and Nieto.
Programming with Microsoft Visual Basic th Edition
Chapter 4 Getting Started with VBA. Subroutines Subroutine is the logical section of code that performs a particular task. Subroutine is also called a.
CHAPTER THREE Representing Data: Constants and Variables.
Controlling Program Flow with Decision Structures.
More on Variables and Subroutines. Introduction Discussion so far has dealt with self- contained subs. Subs can call other subs or functions. A module.
CIS 338: VB Variables Dr. Ralph D. Westfall April, 2011.
Making Interactive Programs with Visual Basic .NET
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
COMPREHENSIVE Excel Tutorial 12 Expanding Excel with Visual Basic for Applications.
Visual Basic I Programming
Unit 2 Technology Systems
Chapter 9: Value-Returning Functions
IE 8580 Module 4: DIY Monte Carlo Simulation
A variable is a name for a value stored in memory.
Test 2 Review Outline.
VBA - Excel VBA is Visual Basic for Applications
VBA - Excel VBA is Visual Basic for Applications
CHAPTER 4 CLIENT SIDE SCRIPTING PART 3 OF 3
2. Understanding VB Variables
Objectives Learn about Function procedures (functions), Sub procedures (subroutines), and modules Review and modify an existing subroutine in an event.
Tonga Institute of Higher Education
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
Chapter 8 - Functions and Functionality
3.2 Working with Data Scope of variables 29/07/2019.
Presentation transcript:

Scope Lifetime Modules Procedures

Scope? Where can your variables be seen? Where used? Where abused (reseting the value)? Local and Global = Private and Public

The usual idea of scope …limit variables to their own procedures Only your procedure can have full control of these little variables AKA “procedure-level variables” Dim intTemp as integer each time the procedure is run the Dim resets the contents of the variables

The main idea of lifetime …carry over values of variables to the next time the procedure is called. Static intTemp as integer Keep a counter going… …or remember the last thing you did for the calling program

Another idea of scope …share variables among your procedures in your module. Scope extends outside Procedure but still limited to within a Module AKA “module-level variables” Do the following in the Declarations Private m_intGeneral as integer

Yet another idea of scope …share variables among modules in your Application Known as Public variables or globals Dangerous in large projects Use Functions instead Do the following in the Declarations Public g_intMajorGeneral as integer

Scope of Constants …share constants among modules. Constants are static and cannot be changed Do the following in the Declarations Public Const cstrWho as String=“Jim”

Scope of Procedures Limited use of procedures in the Module where they are located Private Function LocalStar() Unlimited use of general utility procedures in the Application (standard modules) Public Function Popular()

Modules Where do you put VBA programs?

Module Choices Form Class Modules Standard Modules Class Modules

Form Class Modules Automatically created for you when you have wizard-written controls Best location for form-related procs Main location for event procs

Standard Modules Best location for general utility procs Only location for nonForm procs Best location for constants and custom data structures: modGlobals

Class Modules Custom objects Objects properties and methods These modules will be covered later

More Standard Modules Private Publi

Modularity 301 For variables Private Public Dim Static For Procs Private Public Sub Function

Warnings Duplicate proc names can be OK Duplicate module names can be OK Duplicate variable names can be OK Global variables are rarely Ok (a global would be declared as Public in a standard module's declaration section)

Procedures AKA PROCS

Subs Great for processes! Ok for return values, but not great Know where to file them Example: Assignment 2 (Security)

Functions Great for return values! Ok for processes, but not great Know where to file them Example: Assignment 2 (Security)

Arguments - 1 Not required, but preferred for communication between the caller and the coded procedure. Without args you have only public variables for communication, a weak practice. Use named arg syntax or position syntax for args

Arguments - 2 Calling syntax 1: Named args (MsgBox) vbAnswer=MsgBox(vbMsgBoxStyle:=vbOKOnly,_ Prompt:=strQuestion) Calling syntax 2: Position args (MsgBox) vbAnswer = MsgBox(strQuestion,vbOKOnly)

Arguments - 3 Coding syntax: use optional args Optional args must be at end of arg list and must be variant Sub Test (Optional varTimes as Variant) If IsMissing(varTimes) then varTimes = 10 Or Sub Test (Optional intTimes as Integer = 10) …etc

Arguments - 4 Coding syntax: args as values or as addresses Args as values cannot be changed by proc Args as addresses can be changed by proc Public Function Area(ByVal height As Double, ByRef width As Double) As Double Area = height * width End Function

Arguments - 5 Calling syntax: forcing args as (values) Args as values cannot be changed by proc dblMyArea = Area (dblheight, (dblwidth))

Functions Must be typed when coded Function Msg(strMsg) as vbMsgBoxResult Useful as replacements for: –Calculated fields (normalization) –Global variables –Repetitive calls to MsgBox, etc…

Assignment 2: Sign On Tasks Get user security code (S or V) use VBA functions: InputBox and Ucase Adjust frmContribution to the user's security level use AllowEdits, AllowDeletions, cmdDelete.Enabled

Assignment 2 Pseudo Code Loop while security input Ask for security password input If (Supervisor or Volunteer or Cancel) exit loop Ask if user wants to try again If not to try again abort form open End loop Case Supervisor Allow data entry, record deletions, edits to stored records Case Volunteer Allow data entry Case Cancel Abort form open

Assignment 2 High-level Code Private Sub Form_Open(Cancel As Integer) Dim strLevel As String Dim blnNotOK As Boolean strLevel = GetSecurityInformation ‘ get user's level Call SetUp(strLevel, blnNotOK) ‘ setup form for level If blnNotOK Then Cancel = True ‘ cancel open End Sub

Pop Quiz 1.Public Sub Test() –Describe the possible scope of this procedure 2.modUtilities –Describe the scope of procedures in this module 3.Form_frmContribution –Describe the scope of procedures in this module 4.Public intCounter as Integer –Describe the possible scope of this variable