SlideShare a Scribd company logo
AVB201 Visual Basic For Applications MS Access, Beginning Course P.O. Box 6142 Laguna Niguel, CA 92607 949-489-1472 http://www.dhdursoassociates.com
AVB201 Contact Information Bookstore2 SQL212  Module 2 P.O. Box 6142 Laguna Niguel, CA 92607 949-489-1472 http://www.d2associates.com [email_address]   Copyright 2001-20011 All rights reserved.
AVB210 Notes The original version of these slides was prepared by Sven Homan of Dinamika, Inc. They have since been updated somewhat but the focus on VBA as a programming language has been retained. This course is available on a regular schedule in San Juan Capistrano, California, and by private arrangement. See the contact information  on the previous slide. Sales AVB201 Module 1
AVB201 Resources This course is based on a simple product sales database. Click  here  to download it from www.box.net. Slides can be viewed at the SlideShare link below.  http://www.slideshare.net/OCDatabases   Bookstore SQL212  Module 1
OUTLINE SESSION 1: Event driven Programming in Access (3 hours). SESSION 2: VBA Language Constructs and Programming Techniques (3 hours). AVB202: Working Programmatically with Data (3 hours). Sales AVB201 Module 1
Product Sales Sample Database Sales AVB201 Module 1
Session 1: Event Driven Programming in Access Why VBA ? Using events and event procedures. Using VBA Editor. Event types in Access. Differentiating between functions and sub procedures. Organizing VBA code into modules. Using Variables, Scope, and Data types. Exploring and using built-in functions. Sales AVB201 Module 1
1.1 Why VBA ? Greatly extends the functionality of Macros. Allows customized interactions with users. Enables complex operations on records. Enables complete control over the Access Object Model Capable of making reports and forms that can make decisions based on user choices, database data, and user supplied values. Offers improved error handling capabilities. Programming is FUN !! Sales AVB201 Module 1
1.2 Events and Event Procedures Event is when something happens to any object in your application. Ex: User clicks a button (object) on a form (also an object). Event Procedure is a piece of VBA code that is associated with this object and this event. Since event code is programmed the event can trigger any number of actions. Ex: deleting a record and closing the form. In such event driven programming model, the user is in control of when and what should happen next in the application. Sales AVB201 Module 1
1.3 Using VBA Editor VBA editor is the place where you write your VBA code. Enter via code builder, CTRL G, etc. VBA editor keeps your code organized into modules, functions, procedures, and/or classes (more on module types later). You can write, document, test, compile, and debug your code with supplied tools >>  explore  VBA menus. We will use the VBA editor for the rest of this course. Sales AVB201 Module 1
1.3 Using VBA Editor Demonstration:  Make “Add New” command button on a Products form. Sales AVB201 Module 1
..cont Exercise: Add “Delete” button to Products form and check the procedure. Explore changing the code and see how VBA automatically notifies you of certain errors. Run procedure by clicking the “Delete” command button. Sales AVB201 Module 1
1.4 Types of Events in Access Events in Access can be categorized into several groups: Windows (Form, Report): opening, closing, resizing. Data: making current, deleting, updating. Focus: activating, entering, exiting. Keyboard: pressing or releasing a key. Mouse: clicking a mouse button. Print: formatting, printing. Error and timing: when error occurs in code or some time passes. Sales AVB201 Module 1
Commonly Used Events Events are normally associated with a form, report, or some control on a form. Some common form events are: On Open, On Load, Before Update, On Unload, On Current On Delete Before Delete. Sales AVB201 Module 1
Common Control Events On Click Before Update After Update On Double Click. Sales AVB201 Module 1
Common Report Events On Open On Activate On Close On No Data. Sales AVB201 Module 1
Event Flow NOTE: A single user action can also trigger several events that then run in succession. For example when the user opens a form all of the following events occur: Open >> Load >> Resize >> Activate >> Current Sales AVB201 Module 1
1.5 Differentiating between Functions and Sub Procedures. Functions return a value to the calling code, usually providing the result of the function’s operation. Sub Procedures execute the code in the procedure but do not return any values. Both, functions and procedures, can accept multiple input values from the calling program that can be used inside their respective operations. Sales AVB201 Module 1
..cont Function Syntax: Function FunctionName(passed arguments) As “Data Type of Return” ..some code.. FunctionName = “Return Value” End Function Sales AVB201 Module 1
..cont Sub Procedure Syntax: Sub SubName(passed arguments) ..some code.. End Sub Sales AVB201 Module 1
1.6 Organizing VBA Code into Modules All event procedures and any other VBA code are grouped and placed into “Modules”. MS Access has 4 types of modules: Standard, Form, Report, and Class. Each form and report has its own form or report module. All event procedures associated with a particular form or report should reside in their respective module. Standard module holds any other common or non form/report specific code. Class modules support object oriented programming approach (OOP) to development. Sales AVB201 Module 1
..cont Exercise:  Access any of the module types. In all cases you work with the same VBA editor. Sales AVB201 Module 1
..cont This is the place where we start writing some of our own code. Interactive  Exercise : Create new standard module. Type in the following code: Function Area (Height As Double, Width As Double) As Double Area = Width * Height End Function Optionally compile code (under Debug menu). Run code in Immediate window: ?Area(3,4) Sales AVB201 Module 1
..cont And now try the following procedure: Sub MyLoop() Dim loopcount As Integer, i As Integer loopcount = 3 For i = 1 To loopcount Debug.Print "Loop ", i Next i Beep End Sub More about “For” statement in Session 2. Sales AVB201 Module 1
1.7 Variables, Scope, and Data Types Variables are containers that hold your data in programs. Each variable should have a type. VBA supports a wide range of data types that are not identical to data types declared on fields in tables. Knowing both groups and how to map them is essential for good programming. If you forget to assign type to a variable in VBA, then the variable is treated as a “Variant”. Variants assume data type when data is loaded in to the variable. NOTE:   !! Avoid using Variants”. They slow the execution and make for poor documentation. Sales AVB201 Module 1
Some often used data types in VBA Boolean (True or False) Currency (formatted number $56.67) Double, Single (51.145678) Integer (321) Date (#8/28/04#) String (“Any word or sentence”) Q: What are their equivalents in database tables? Sales AVB201 Module 1
Scope of Variables (and Procedures) Scope determines where can variable be used. This depends on the place where and also how it was declared. Variables declared inside functions or subs must be declared with a Dim statement. They are always local to that sub or function.. Sales AVB201 Module 1
… cont Variables declared outside any function or sub are always global to that module. Whether they are also available to other modules, depends on what you precede their declarations with: “ Private Area As Double” would be same as “Dim Area As Double”. “ Public Area As Double” on the other hand is global to the application. NOTE: !!You can not use Public declaration inside function or sub. Use Dim ONLY. Sales AVB201 Module 1
..cont Functions and Procedures also have a scope. Preceding function or procedure declaration with a word “Public” or “Private” makes that function or sub available “in the module where declared only” OR they can be called (used) from any place in the application. Ex: Procedure in one module can call Public functions declared in any other module.  Public and Private declarations are useful to avoid confusion with keeping track of multiple names used for variables, functions, and procedures. Similar operations on two different forms can use procedures with same name as long as they are both declared as private in their respective modules. Sales AVB201 Module 1
..cont Exercise: Modify the MyLoop procedure to accept the number of loops. Call the new one  MyAreaLoop Call the Area function procedure from the MyAreaLoop procedure and pass it a length 0f 10 and width of I. (You will have to set up a variable of type double for area.) Run the MyAreaLoop procedure. Sales AVB201 Module 1
..cont Exercise: Sub MyAreaLoop( numloops As Integer) Dim loopnum As Double, i As Integer For i = 1 To numloops loopnum = i Debug.Print "Area ", i, Area(10, loopnum) Next i Beep End Sub Sales AVB201 Module 1
..cont Mini-Quiz: If you change the Area function to private does myarealoop still work? Why? How could you use global variables? Should you? Sales AVB201 Module 1
1.8 Built-in Functions and Procedures Beside custom made functions and procedures there are some that are built-in and part of the Access VBA language. We will see several examples in the following Sessions. “ Beep” is an example of built-in procedure and performs a single operation: It makes computer beep. Once. Built-in functions can be used anywhere in your program. Knowing what is available is essential for efficient programming so we don’t go and try to reinvent the wheel.  Sales AVB201 Module 1
Some Common built-in Functions Today = Date() returns today’s date. UCase(String) converts all letters in string to upper case. Round(3.89) rounds a value in parenthesis to nearest integer (4 here). Len(String) returns number of characters in a string. NOTE:  There are close to 100 built in functions available for almost any area of data manipulation. Check some reference book for complete list or consult an Online Help in Access. We will introduce and use several in the hours ahead. Sales AVB201 Module 1

More Related Content

PPT
AVB201.1 MS Access VBA Module 1
PPT
AVB201.2 Microsoft Access VBA Module 2
PPTX
E learning excel vba programming lesson 3
PPTX
Vba Class Level 3
PDF
Excel VBA programming basics
PPTX
Vba introduction
PPTX
Vba Class Level 1
PPTX
VBA
AVB201.1 MS Access VBA Module 1
AVB201.2 Microsoft Access VBA Module 2
E learning excel vba programming lesson 3
Vba Class Level 3
Excel VBA programming basics
Vba introduction
Vba Class Level 1
VBA

What's hot (20)

PPTX
Introduction to Excel VBA/Macros
PPTX
Unit 1 introduction to visual basic programming
DOCX
Excel vba
PPTX
Vba part 1
PPTX
E learning excel vba programming lesson 4
PDF
Visual Basics for Application
PDF
Online Advance Excel & VBA Training in India
PPTX
Cordova training : Day 3 - Introduction to Javascript
PDF
2016 Excel/VBA Notes
PPT
Introduction to programming using Visual Basic 6
PPT
Visual Basic Programming
PPT
Visual basic 6.0
PDF
Programming concepts By ZAK
PPTX
Intro to Excel VBA Programming
PPTX
User define data type In Visual Basic
PDF
Introduction to programming by MUFIX Commnity
PDF
Vb6 ch.8-3 cci
PPTX
Program by Demonstration using Version Space Algebra
PDF
problem solving and design By ZAK
PDF
Prg 211 prg211
Introduction to Excel VBA/Macros
Unit 1 introduction to visual basic programming
Excel vba
Vba part 1
E learning excel vba programming lesson 4
Visual Basics for Application
Online Advance Excel & VBA Training in India
Cordova training : Day 3 - Introduction to Javascript
2016 Excel/VBA Notes
Introduction to programming using Visual Basic 6
Visual Basic Programming
Visual basic 6.0
Programming concepts By ZAK
Intro to Excel VBA Programming
User define data type In Visual Basic
Introduction to programming by MUFIX Commnity
Vb6 ch.8-3 cci
Program by Demonstration using Version Space Algebra
problem solving and design By ZAK
Prg 211 prg211
Ad

Viewers also liked (20)

PPT
Reporting for Online:
Tools for Building Trust and Relevance
PPT
Allemand
PPT
Filoarnaucadell
PDF
My Web Portfolio
PDF
Managing Your Career In Tough Times 102308
PPTX
Vida rural .Herramientas y enseres.
PPTX
Operació t4 i josef mengele
PPT
Displays
PPT
Thom Point of View on Segmentation
PPT
Teknologi dalam pendidikan
PPT
Preparing Students
PPTX
Vhag profile 2013
PPT
Test 1
PPT
SQL302 Intermediate SQL Workshop 1
PPT
AIA101.1.MS Access Tables & Data
PPT
AIA101.0.Aia101
PDF
成衣服飾產業發展趨勢-創業懶人包-青年創業及圓夢網
PPT
AIN102D Access date functions sample queries
Reporting for Online:
Tools for Building Trust and Relevance
Allemand
Filoarnaucadell
My Web Portfolio
Managing Your Career In Tough Times 102308
Vida rural .Herramientas y enseres.
Operació t4 i josef mengele
Displays
Thom Point of View on Segmentation
Teknologi dalam pendidikan
Preparing Students
Vhag profile 2013
Test 1
SQL302 Intermediate SQL Workshop 1
AIA101.1.MS Access Tables & Data
AIA101.0.Aia101
成衣服飾產業發展趨勢-創業懶人包-青年創業及圓夢網
AIN102D Access date functions sample queries
Ad

Similar to AVB201.1 Microsoft Access VBA Module 1 (20)

PPTX
Chapter 6 slides
PPTX
008.module
PDF
Programming with Microsoft Visual Basic 2015 7th Edition Zak Solutions Manual
PPT
Visual basics
PDF
Unit iii vb_study_materials
PPTX
Presentation on visual basic 6 (vb6)
PPTX
Using general sub procedures
PPTX
Excel VBA.pptx
PPT
Intro_to_VB.PPT
PDF
Programming inexcelvba anintroduction
PDF
Getting started with Microsoft Excel Macros
PDF
Vba part 1
PPT
visual basic v6 introduction
PDF
Visualbasic tutorial
PDF
VB PPT by ADI PART2.pdf
PPTX
Variables and constants
DOC
Excel Vba Basic Tutorial 1
PPT
Introduction to VB.Net By William Lacktano.ppt
Chapter 6 slides
008.module
Programming with Microsoft Visual Basic 2015 7th Edition Zak Solutions Manual
Visual basics
Unit iii vb_study_materials
Presentation on visual basic 6 (vb6)
Using general sub procedures
Excel VBA.pptx
Intro_to_VB.PPT
Programming inexcelvba anintroduction
Getting started with Microsoft Excel Macros
Vba part 1
visual basic v6 introduction
Visualbasic tutorial
VB PPT by ADI PART2.pdf
Variables and constants
Excel Vba Basic Tutorial 1
Introduction to VB.Net By William Lacktano.ppt

More from Dan D'Urso (20)

PPT
SQL201S Accelerated Introduction to MySQL Queries
PPT
LCD201d Database Diagramming with Lucidchart
PPTX
Database Normalization
PPT
VIS201d Visio Database Diagramming
PPT
PRJ101a Project 2013 Accelerated
PPT
PRJ101xl Project Libre Basic Training
PPT
Introduction to coding using Python
PPTX
Stem conference
PDF
SQL200A Microsoft Access SQL Design
PPTX
Microsoft access self joins
PDF
SQL302 Intermediate SQL
PDF
AIN106 Access Reporting and Analysis
PPT
SQL302 Intermediate SQL Workshop 3
PPT
SQL302 Intermediate SQL Workshop 2
PDF
Course Catalog
PDF
SQL212 Oracle SQL Manual
PDF
SQL201W MySQL SQL Manual
PDF
AIN100
PPT
SQL206 SQL Median
PPT
SQL202.3 Accelerated Introduction to SQL Using SQL Server Module 3
SQL201S Accelerated Introduction to MySQL Queries
LCD201d Database Diagramming with Lucidchart
Database Normalization
VIS201d Visio Database Diagramming
PRJ101a Project 2013 Accelerated
PRJ101xl Project Libre Basic Training
Introduction to coding using Python
Stem conference
SQL200A Microsoft Access SQL Design
Microsoft access self joins
SQL302 Intermediate SQL
AIN106 Access Reporting and Analysis
SQL302 Intermediate SQL Workshop 3
SQL302 Intermediate SQL Workshop 2
Course Catalog
SQL212 Oracle SQL Manual
SQL201W MySQL SQL Manual
AIN100
SQL206 SQL Median
SQL202.3 Accelerated Introduction to SQL Using SQL Server Module 3

Recently uploaded (20)

PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
A Presentation on Artificial Intelligence
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
cuic standard and advanced reporting.pdf
PPTX
Machine Learning_overview_presentation.pptx
PPTX
MYSQL Presentation for SQL database connectivity
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Encapsulation theory and applications.pdf
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPT
Teaching material agriculture food technology
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Machine learning based COVID-19 study performance prediction
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Big Data Technologies - Introduction.pptx
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
Cloud computing and distributed systems.
Per capita expenditure prediction using model stacking based on satellite ima...
A Presentation on Artificial Intelligence
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
cuic standard and advanced reporting.pdf
Machine Learning_overview_presentation.pptx
MYSQL Presentation for SQL database connectivity
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Encapsulation theory and applications.pdf
MIND Revenue Release Quarter 2 2025 Press Release
Unlocking AI with Model Context Protocol (MCP)
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Teaching material agriculture food technology
A comparative analysis of optical character recognition models for extracting...
Machine learning based COVID-19 study performance prediction
Network Security Unit 5.pdf for BCA BBA.
Big Data Technologies - Introduction.pptx
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Chapter 3 Spatial Domain Image Processing.pdf
Cloud computing and distributed systems.

AVB201.1 Microsoft Access VBA Module 1

  • 1. AVB201 Visual Basic For Applications MS Access, Beginning Course P.O. Box 6142 Laguna Niguel, CA 92607 949-489-1472 http://www.dhdursoassociates.com
  • 2. AVB201 Contact Information Bookstore2 SQL212 Module 2 P.O. Box 6142 Laguna Niguel, CA 92607 949-489-1472 http://www.d2associates.com [email_address] Copyright 2001-20011 All rights reserved.
  • 3. AVB210 Notes The original version of these slides was prepared by Sven Homan of Dinamika, Inc. They have since been updated somewhat but the focus on VBA as a programming language has been retained. This course is available on a regular schedule in San Juan Capistrano, California, and by private arrangement. See the contact information on the previous slide. Sales AVB201 Module 1
  • 4. AVB201 Resources This course is based on a simple product sales database. Click here to download it from www.box.net. Slides can be viewed at the SlideShare link below. http://www.slideshare.net/OCDatabases Bookstore SQL212 Module 1
  • 5. OUTLINE SESSION 1: Event driven Programming in Access (3 hours). SESSION 2: VBA Language Constructs and Programming Techniques (3 hours). AVB202: Working Programmatically with Data (3 hours). Sales AVB201 Module 1
  • 6. Product Sales Sample Database Sales AVB201 Module 1
  • 7. Session 1: Event Driven Programming in Access Why VBA ? Using events and event procedures. Using VBA Editor. Event types in Access. Differentiating between functions and sub procedures. Organizing VBA code into modules. Using Variables, Scope, and Data types. Exploring and using built-in functions. Sales AVB201 Module 1
  • 8. 1.1 Why VBA ? Greatly extends the functionality of Macros. Allows customized interactions with users. Enables complex operations on records. Enables complete control over the Access Object Model Capable of making reports and forms that can make decisions based on user choices, database data, and user supplied values. Offers improved error handling capabilities. Programming is FUN !! Sales AVB201 Module 1
  • 9. 1.2 Events and Event Procedures Event is when something happens to any object in your application. Ex: User clicks a button (object) on a form (also an object). Event Procedure is a piece of VBA code that is associated with this object and this event. Since event code is programmed the event can trigger any number of actions. Ex: deleting a record and closing the form. In such event driven programming model, the user is in control of when and what should happen next in the application. Sales AVB201 Module 1
  • 10. 1.3 Using VBA Editor VBA editor is the place where you write your VBA code. Enter via code builder, CTRL G, etc. VBA editor keeps your code organized into modules, functions, procedures, and/or classes (more on module types later). You can write, document, test, compile, and debug your code with supplied tools >> explore VBA menus. We will use the VBA editor for the rest of this course. Sales AVB201 Module 1
  • 11. 1.3 Using VBA Editor Demonstration: Make “Add New” command button on a Products form. Sales AVB201 Module 1
  • 12. ..cont Exercise: Add “Delete” button to Products form and check the procedure. Explore changing the code and see how VBA automatically notifies you of certain errors. Run procedure by clicking the “Delete” command button. Sales AVB201 Module 1
  • 13. 1.4 Types of Events in Access Events in Access can be categorized into several groups: Windows (Form, Report): opening, closing, resizing. Data: making current, deleting, updating. Focus: activating, entering, exiting. Keyboard: pressing or releasing a key. Mouse: clicking a mouse button. Print: formatting, printing. Error and timing: when error occurs in code or some time passes. Sales AVB201 Module 1
  • 14. Commonly Used Events Events are normally associated with a form, report, or some control on a form. Some common form events are: On Open, On Load, Before Update, On Unload, On Current On Delete Before Delete. Sales AVB201 Module 1
  • 15. Common Control Events On Click Before Update After Update On Double Click. Sales AVB201 Module 1
  • 16. Common Report Events On Open On Activate On Close On No Data. Sales AVB201 Module 1
  • 17. Event Flow NOTE: A single user action can also trigger several events that then run in succession. For example when the user opens a form all of the following events occur: Open >> Load >> Resize >> Activate >> Current Sales AVB201 Module 1
  • 18. 1.5 Differentiating between Functions and Sub Procedures. Functions return a value to the calling code, usually providing the result of the function’s operation. Sub Procedures execute the code in the procedure but do not return any values. Both, functions and procedures, can accept multiple input values from the calling program that can be used inside their respective operations. Sales AVB201 Module 1
  • 19. ..cont Function Syntax: Function FunctionName(passed arguments) As “Data Type of Return” ..some code.. FunctionName = “Return Value” End Function Sales AVB201 Module 1
  • 20. ..cont Sub Procedure Syntax: Sub SubName(passed arguments) ..some code.. End Sub Sales AVB201 Module 1
  • 21. 1.6 Organizing VBA Code into Modules All event procedures and any other VBA code are grouped and placed into “Modules”. MS Access has 4 types of modules: Standard, Form, Report, and Class. Each form and report has its own form or report module. All event procedures associated with a particular form or report should reside in their respective module. Standard module holds any other common or non form/report specific code. Class modules support object oriented programming approach (OOP) to development. Sales AVB201 Module 1
  • 22. ..cont Exercise: Access any of the module types. In all cases you work with the same VBA editor. Sales AVB201 Module 1
  • 23. ..cont This is the place where we start writing some of our own code. Interactive Exercise : Create new standard module. Type in the following code: Function Area (Height As Double, Width As Double) As Double Area = Width * Height End Function Optionally compile code (under Debug menu). Run code in Immediate window: ?Area(3,4) Sales AVB201 Module 1
  • 24. ..cont And now try the following procedure: Sub MyLoop() Dim loopcount As Integer, i As Integer loopcount = 3 For i = 1 To loopcount Debug.Print "Loop ", i Next i Beep End Sub More about “For” statement in Session 2. Sales AVB201 Module 1
  • 25. 1.7 Variables, Scope, and Data Types Variables are containers that hold your data in programs. Each variable should have a type. VBA supports a wide range of data types that are not identical to data types declared on fields in tables. Knowing both groups and how to map them is essential for good programming. If you forget to assign type to a variable in VBA, then the variable is treated as a “Variant”. Variants assume data type when data is loaded in to the variable. NOTE: !! Avoid using Variants”. They slow the execution and make for poor documentation. Sales AVB201 Module 1
  • 26. Some often used data types in VBA Boolean (True or False) Currency (formatted number $56.67) Double, Single (51.145678) Integer (321) Date (#8/28/04#) String (“Any word or sentence”) Q: What are their equivalents in database tables? Sales AVB201 Module 1
  • 27. Scope of Variables (and Procedures) Scope determines where can variable be used. This depends on the place where and also how it was declared. Variables declared inside functions or subs must be declared with a Dim statement. They are always local to that sub or function.. Sales AVB201 Module 1
  • 28. … cont Variables declared outside any function or sub are always global to that module. Whether they are also available to other modules, depends on what you precede their declarations with: “ Private Area As Double” would be same as “Dim Area As Double”. “ Public Area As Double” on the other hand is global to the application. NOTE: !!You can not use Public declaration inside function or sub. Use Dim ONLY. Sales AVB201 Module 1
  • 29. ..cont Functions and Procedures also have a scope. Preceding function or procedure declaration with a word “Public” or “Private” makes that function or sub available “in the module where declared only” OR they can be called (used) from any place in the application. Ex: Procedure in one module can call Public functions declared in any other module. Public and Private declarations are useful to avoid confusion with keeping track of multiple names used for variables, functions, and procedures. Similar operations on two different forms can use procedures with same name as long as they are both declared as private in their respective modules. Sales AVB201 Module 1
  • 30. ..cont Exercise: Modify the MyLoop procedure to accept the number of loops. Call the new one MyAreaLoop Call the Area function procedure from the MyAreaLoop procedure and pass it a length 0f 10 and width of I. (You will have to set up a variable of type double for area.) Run the MyAreaLoop procedure. Sales AVB201 Module 1
  • 31. ..cont Exercise: Sub MyAreaLoop( numloops As Integer) Dim loopnum As Double, i As Integer For i = 1 To numloops loopnum = i Debug.Print "Area ", i, Area(10, loopnum) Next i Beep End Sub Sales AVB201 Module 1
  • 32. ..cont Mini-Quiz: If you change the Area function to private does myarealoop still work? Why? How could you use global variables? Should you? Sales AVB201 Module 1
  • 33. 1.8 Built-in Functions and Procedures Beside custom made functions and procedures there are some that are built-in and part of the Access VBA language. We will see several examples in the following Sessions. “ Beep” is an example of built-in procedure and performs a single operation: It makes computer beep. Once. Built-in functions can be used anywhere in your program. Knowing what is available is essential for efficient programming so we don’t go and try to reinvent the wheel. Sales AVB201 Module 1
  • 34. Some Common built-in Functions Today = Date() returns today’s date. UCase(String) converts all letters in string to upper case. Round(3.89) rounds a value in parenthesis to nearest integer (4 here). Len(String) returns number of characters in a string. NOTE: There are close to 100 built in functions available for almost any area of data manipulation. Check some reference book for complete list or consult an Online Help in Access. We will introduce and use several in the hours ahead. Sales AVB201 Module 1
  • 35. ..cont Built-in functions are grouped into several categories: Conversion (converts one data type to another. UCase, LCase, etc..) Date/Time Financial Mathematical String Manipulation (extract parts of string, truncate, etc..) Programming (check for missing values, determine data types etc..) Domain (work on several values at once and perform statistical operations such as average, finding maximum / minimum value etc..) Sales AVB201 Module 1
  • 36. ..cont Try a couple from the immediate window: Sales AVB201 Module 1
  • 37. AVB201 – Introduction to VBA Sales AVB201 Module 1 Session 1 END