DATABASE AUTOMATION USING VBA (ADVANCED MICROSOFT ACCESS, X405.6)

Similar documents
Introduction. C provides two styles of flow control:

PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND T-SQL)

Programming Basics and Practice GEDB029 Decision Making, Branching and Looping. Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029

Flow Control. CSC215 Lecture

VBA Handout. References, tutorials, books. Code basics. Conditional statements. Dim myvar As <Type >

Language Fundamentals

DroidBasic Syntax Contents

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Microsoft Visual Basic 2015: Reloaded

d2vbaref.doc Page 1 of 22 05/11/02 14:21

Chapter 2: Functions and Control Structures

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.

VARIABLES. 1. STRINGS Data with letters and/or characters 2. INTEGERS Numbers without decimals 3. FLOATING POINT NUMBERS Numbers with decimals

NOTES: Procedures (module 15)

Boolean evaluation and if statements. Making decisions in programs

Unit E Step-by-Step: Programming with Python

OpenOffice.org 3.2 BASIC Guide

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

SPARK-PL: Introduction

Objectives. Introduce the core C# language features class Main types variables basic input and output operators arrays control constructs comments

JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB PROGRAMMING, X452.1)

Software Design & Programming I

Chapter 4 The If Then Statement

LESSON 3. In this lesson you will learn about the conditional and looping constructs that allow you to control the flow of a PHP script.

IDENTIFYING UNIQUE VALUES IN AN ARRAY OR RANGE (VBA)

Microsoft Visual Basic 2005: Reloaded

Introduction to Visual Basic and Visual C++ Arithmetic Expression. Arithmetic Expression. Using Arithmetic Expression. Lesson 4.

DEVELOPING DATABASE APPLICATIONS (INTERMEDIATE MICROSOFT ACCESS, X405.5)

CSE 230 Intermediate Programming in C and C++ Functions

Syntax. Table of Contents

Agenda & Reading. VB.NET Programming. Data Types. COMPSCI 280 S1 Applications Programming. Programming Fundamentals

Decision Making in C

Microsoft Visual Basic 2005: Reloaded

Chapter 8 Statement-Level Control Structures

Array. Prepared By - Rifat Shahriyar

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

8. Control statements

Function: function procedures and sub procedures share the same characteristics, with

PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND T-SQL)

Chapter 3: Programming with MATLAB

20. VB Programming Fundamentals Variables and Procedures

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

Sub Programs. To Solve a Problem, First Make It Simpler

Structure of Programming Languages Lecture 5

Please answer questions in the space provided. Question point values are shown in parentheses.

Java Primer 1: Types, Classes and Operators

Petros: A Multi-purpose Text File Manipulation Language

Review for Programming Exam and Final May 4-9, Ribbon with icons for commands Quick access toolbar (more at lecture end)

Programming Language 2 (PL2)

Java Methods. Lecture 8 COP 3252 Summer May 23, 2017

The SPL Programming Language Reference Manual

VBA Collections A Group of Similar Objects that Share Common Properties, Methods and

egrapher Language Reference Manual

Control Structures. Control Structures 3-1

Java Bytecode (binary file)

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

Language Proposal: tail

Fundamentals of Programming Session 13

The following expression causes a divide by zero error:

Procedures in Visual Basic

Control Structures. CIS 118 Intro to LINUX

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

Creating a C++ Program

Java+- Language Reference Manual

PL / SQL Basics. Chapter 3

PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND T-SQL)

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

6.1. Chapter 6: What Is A Function? Why Functions? Introduction to Functions

G Programming Languages - Fall 2012

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

Iteration -- Once Is Not Enough

The PCAT Programming Language Reference Manual

Introduction to Programming Using Java (98-388)

Full file at

Introduction to C/C++ Lecture 3 - Program Flow Control

Excel VBA Variables, Data Types & Constant

Sprite an animation manipulation language Language Reference Manual

NOTE: Answer ANY FOUR of the following 6 sections:

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Unit 7. Functions. Need of User Defined Functions

CPSC 3740 Programming Languages University of Lethbridge. Control Structures

Pace University. Fundamental Concepts of CS121 1

Introduction to Programming

<excelunusual.com> Easy Zoom -Chart axis Scaling Using VBA - by George Lungu. < 1. Introduction: Chart naming: by George Lungu

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

BM214E Object Oriented Programming Lecture 4

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

DATABASE AUTOMATION USING VBA (ADVANCED MICROSOFT ACCESS, X405.6)

Chapter 8. Statement-Level Control Structures

Programming Lecture 4

Copyright (c) by Matthew S. Harris

C-LANGUAGE CURRICULAM

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax

Lesson 06 Arrays. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

Transcription:

Technology & Information Management Instructor: Michael Kremer, Ph.D. Database Program: Microsoft Access Series DATABASE AUTOMATION USING VBA (ADVANCED MICROSOFT ACCESS, X405.6)

AGENDA 3. Executing VBA 4. Declaring Variables

Executing VBA 3.

3.1 CONTROL STRUCTURES 31 VBA statements are processed from left to right and top to bottom. No GoTo statement! Most commonly used control structures: Decision structures Loop structures Decision structures: Define groups of statements that may or may not be executed, depending of the value of an expression. Loop structures: Define groups of statements that executes repeatedly

3.2 DECISION STRUCTURES 32 If Statement An expression is evaluated, if True then statements within the If block are processed. Simple If End If Enhanced structure: If..Else End If. If expression evaluates to False, then Else block is processed. Select Case End Case is another decision structure. Basic If statement (single line):

3.2 DECISION STRUCTURES Multiple-Line If structure: Start IF 33 If condition True Statements Use indentation to improve readability. False End IF

3.2 DECISION STRUCTURES 34 Condition is usually a comparison, such as A < 10. However, it can contain an expression that evaluates to a numeric expression. 0 is considered false and -1 is true. A Yes/No field is a perfect candidate.

3.2 DECISION STRUCTURES 35 Else ElseIf

3.2 DECISION STRUCTURES 36

3.2 DECISION STRUCTURES 37 Select Case statement

3.2 DECISION STRUCTURES 38 SELECT CASE Statement Only one test expression at the top, then compare to multiple values.

3.2 DECISION STRUCTURES 39 Use To keyword for ranges, commas to separate multiple values. When using comparison operators, use the Is keyword. Select Case statement can be replaced with traditional If statement. However, Select Case is more efficient and readable. As with If statement, only one block is processed. Select Case statement works only with one test expression.

3.2 DECISION STRUCTURES 40

3.3 LOOP STRUCTURES 41 Loop structures are designed to execute code repetitively. Two Loop structures: Do Loop For Next Do Loop structure uses conditional processing to determine whether a loop is to be processed. For Next loop repeats a specific number of times. Start Loop Condition False True Statements End Loop

3.3 LOOP STRUCTURES 42 Do Loop

3.3 LOOP STRUCTURES Another variation is shown below: 43 At least one loop is executed since the condition is placed at the bottom.

3.3 LOOP STRUCTURES 44 Looping while a conditions remains true:

3.3 LOOP STRUCTURES 45 For Next Loop No testing of conditions necessary, very efficient. Nesting of loops very common.

3.3 LOOP STRUCTURES 46 This example will append the alphabet into a string and display it in a message box.

3.4 EXITING A CONTROL STRUCTURE 47 Sometimes it is desirable to exit a loop structure when you are done with a certain task. Syntax: Exit Do Exit For Exit Sub Exit Function Generally, you have to test for some condition using an If statement inside your loop.

3.4 EXITING A CONTROL STRUCTURE 48

3.5 MESSAGE BOX FUNCTION/STATEMENT 49 Message box statement: Msgbox prompt, buttons, title Prompt: Message to be displayed Buttons: 3 different parts: Buttons, Icon Style, Default button Title: Title of message box window

3.5 MESSAGE BOX FUNCTION/STATEMENT 50 Message box function returns a value which button the user pressed. Only difference in syntax is to enclose the arguments in parentheses.

3.6 ARRAYS 51 It is common to store records in one variable. An index is used to tell the different records apart. First Name Last Name Record 1 1,1 1,2 Record 2 2,1 2,2 Record 3 3,1 3,2 Record 4 4,1 4,2 Record 5 5,1 5,2 Record 6 6,1 6,2 Record 7 7,1 7,2 Record 8 8,1 8,2 Record 9 9,1 9,2 Record 10 10,1 10,2

3.6 ARRAYS 52 Arrays have upper and lower bounds to indicate the range of index numbers. Once an array is declared, use LBound and UBound functions to retrieve the upper and lower bounds.

3.6 ARRAYS 53

3.6 ARRAYS 54

3.7 NULLS IN VBA 55 What is Null? Currently unknown or missing data Not applicable data

3.7 NULLS IN VBA 56 In VBA, only one data type can hold Nulls, that is the Variant data type. Comparison expressions using Nulls. Do not use = Null to test for Nulls!

Declaring Variables.

4.1 OVERVIEW OF VARIABLES 57 Variables hold temporarily data while the application is running. Also used for storing references to other objects for quick access. Variables are declared for four reasons: To assign a variable name To designate the variable s scope (visibility) To designate the lifetime To specify a data type The Dim statement:

4.2 THE DIM STATEMENT 58 Most commonly used data types: String for text values. Date for date/time values. Single/Double/Integer/Long Integer for number values. Boolean for yes/no type of data. Variant for any kind of data. Object for object references, such as a form or a report.

4.3 IMPLICIT/EXPLICIT VARIABLE DECLARATION If no data type is specified, the Variant data type is used. Variant is the least stringent because it can hold all types of data. Once a variable is declared, a default value is assigned: String = (zero-length string) Number = 0 Date/time = 12:00:00 AM Variant = Empty Object = Nothing Variables in code can be used without being declared. Not recommended! 59

4.3 IMPLICIT/EXPLICIT VARIABLE DECLARATION 60 Option Explicit statement means that every variable must be declared. Sometimes, especially during development, you may want to turn this off..

4.3 IMPLICIT/EXPLICIT VARIABLE DECLARATION 61 Once you turn on Option Explicit, the following compile error is displayed:

4.3 IMPLICIT/EXPLICIT VARIABLE DECLARATION 62 The Option Explicit statement must be included in all modules in order to be effective for the entire application. Turn on Require Variable Declaration in Tools, Options. Does not change existing modules! Must manually include Option Explicit

4.4 SCOPE OF VARIABLES 63 Depending on how a variable is declared, it may be only accessible in the procedure it is declared in. Or only in the module the procedure belongs to. Or in the entire database application. Three different scopes: Procedure-level Private-module level Public-module level Using Dim in a procedure creates Procedure-level scope. Also declaring an argument in in the argument list of a procedure is a Procedure-level scope.

4.4 SCOPE OF VARIABLES 64 When a variable is declared in the declaration section of a module using the Dim or Private statement, the scope is Private-module level. Module can be a class or standard module. When a variable is declared in the declaration section of a module using the Public statement, its scope is Public-module level. Procedure-level variable is destroyed Private Module-level once the procedure Public Module-level is finished. Procedure-level There may be more than one variable strtemp.

4.4 SCOPE OF VARIABLES 65

4.4 SCOPE OF VARIABLES 66

4.4 SCOPE OF VARIABLES 67

4.4 SCOPE OF VARIABLES 68 Database Module 1 Public gstrtemp As String Procedure Dim strtemp As String Procedure-level Module 2 Option Compare Database Option Explicit Dim mstrtemp As String Procedure Public Module-level Private Module-level

4.5 LIFETIME OF VARIABLES 69 Values of module-level variables (Public and Private) are preserved as long as the database remains open. Procedure-level variable exists only as long as the procedure is running. There is no such statement to kill a variable in VBA, therefore use module-level variables sparingly. One lifesaver for procedure-level variables: Static Such a variable preserves its value in memory, but it is only accessible in the procedure it was declared. An entire procedure can be declared using the Static keyword: All variables become static:

4.5 LIFETIME OF VARIABLES 70