XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications.

Slides:



Advertisements
Similar presentations
Integrated Business Applications with Databases (D3) Jenny Pedler
Advertisements

Developing an Excel Application
Tutorial 8: Developing an Excel Application
XP New Perspectives on Microsoft Excel 2003, Second Edition- Tutorial 8 1 Microsoft Office Excel 2003 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 for MS Excel Hamze Msheik. Open the Visual Basic Editor in Excel 2007 Click on the Microsoft Office button in the top left of the Excel window and.
With Microsoft Excel 2010 © 2011 Pearson Education, Inc. Publishing as Prentice Hall1 PowerPoint Presentation to Accompany GO! with Microsoft ® Excel 2010.
Chapter 6 Multiform Projects Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
Exploring Microsoft Access Chapter 8 Creating More Powerful Applications: Introduction to VBA By Robert T. Grauer Maryann Barber.
VBA Modules, Functions, Variables, and Constants
Automating Tasks With Macros
Using Visual Basic 6.0 to Create Web-Based Database Applications
String Variables Visual Basic for Applications 4.
Microsoft Excel 2003 Illustrated Complete with Excel Programming.
Exploring Office Grauer and Barber 1 Creating More Powerful Applications: Introduction to VBA(Wk9)
Automating Tasks With Macros. 2 Design a switchboard and dialog box for a graphical user interface Database developers interact directly with Access.
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.
Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with.
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.
1 Chapter 1 Tour of Access. 1 Chapter Objectives Start and exit Microsoft Access Open and run an Access application Identify the major elements of the.
Adding Automated Functionality to Office Applications.
A First Program Using C#
05/09/ Introducing Visual Basic Sequence Programming.
Project 9 Using Visual Basic for Applications (VBA) to Customize and Automate Excel Jason C. H. Chen, Ph.D. Professor of Management Information Systems.
XP New Perspectives on Microsoft Office Access 2003 Tutorial 12 1 Microsoft Office Access 2003 Tutorial 12 – Managing and Securing a Database.
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
Enhancing User Interaction Through Programming
Using Visual Basic 6.0 to Create Web-Based Database Applications
® Microsoft Access 2010 Tutorial 11 Using and Writing Visual Basic for Applications Code.
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 Microsoft Office Excel Copyright © 2008 Pearson Prentice Hall. All rights reserved
06/10/ Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and.
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.
Visual Basic for Applications Macro Programming For Microsoft Office.
XP New Perspectives on Microsoft Office Access 2003, Second Edition- Tutorial 2 1 Microsoft Office Access 2003 Tutorial 2 – Creating And Maintaining A.
Microsoft Visual Basic 2012 CHAPTER THREE Program Design and Coding.
OCC Network Drives  H:\  P:\ 
Date Variables Visual Basic for Applications 5. Objectives n In this tutorial, you will learn how to: n Reserve a Date variable n Use an assignment statement.
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 6 Multiform Projects.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Exploring Microsoft Access Chapter 8 Creating More Powerful Applications: Introduction to VBA.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Access Lesson 11 Creating and Running Macros Microsoft Office 2010 Advanced Cable / Morrison 1.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.
XP New Perspectives on Microsoft Office Access 2003 Tutorial 10 1 Microsoft Office Access 2003 Tutorial 10 – Automating Tasks With Macros.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations.
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.
COMPREHENSIVE Access Tutorial 11 Using and Writing Visual Basic for Applications Code.
Creating New Forms Projects can appear more professional when using different windows for different types of information. Select Add Windows Form from.
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.
Chapter 2: The Visual Studio .NET Development Environment
Microsoft Access 2003 Illustrated Complete
Microsoft Access Illustrated
Microsoft Office Illustrated
Microsoft Excel 2003 Illustrated Complete
Microsoft Office Access 2003
Objectives Learn about Function procedures (functions), Sub procedures (subroutines), and modules Review and modify an existing subroutine in an event.
Exploring Microsoft Office Access 2007
Microsoft Office Excel 2003
Tutorial 11 Using and Writing Visual Basic for Applications Code
Presentation transcript:

XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications Code

XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 2 Learn about VBA When you work in Access, in the background, Visual Basic for Applications (VBA) code is being created. You can also write your own VBA code that will alter the properties of objects, perform calculations, and many other custom actions. Recall that an event is something that happens while using the database. You can write a statement that, when an event occurs, responds by executing a series of VBA statements.

XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 3 Function procedures, Sub procedures, and modules Statements are grouped together into procedures, which can be either a function or a Sub procedure. Common procedures are usually stored together into a module. A module can be a standard module (stored in memory with other database objects) or it can be a class module (stored in association with a particular form or report). With class modules, the procedures are available by the form or report for which the class module was created.

XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 4 The structure of a VBA module

XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 5 Learning VBA takes time and effort Learning to write VBA code efficiently and accurately is the hardest part of learning to use Access. In this tutorial, you are getting just a small view of VBA code. You should keep in mind that you will not be considered a proficient VBA programmer following this tutorial. Rather, if you want to be a database developer, you should probably take another course geared towards VBA programming.

XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 6 Review and modify an existing Sub procedure in an event procedure When a class module already exists, you can view the code for the procedures in the VBA editor: –Open the property sheets for the object and then locate the event for which the procedure is written –Click the Build button to open the Visual Basic window, which reveals the code stored in the class module The window in which the code appears is called the Code window.

XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 7 Use assignment statements Procedures are enclosed between the Sub statement and the End Sub statement. Within a procedure, you will see statements that work together called control structures. Assignment statements assign a value to a field or property. In some cases, an assignment statement might assign a new value to an object property. You can make changes to VBA code in the Code window.

XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 8 Event properties for a form

XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 9 The Visual Basic Code window

XP New Perspectives on Microsoft Office Access 2003 Tutorial Recognize VBA statements In the Code window (shown in the previous slide) you would view, edit, and test your VBA statements. You will probably not understand the statements that appear in this window. However, there are a couple of statements that you will recognize: –The first statement is the Private Sub statement –The last statement is the End Sub statement In this sample code, the ForeColor is changed under certain circumstances. –The code used the RGB function, which will return a specified colored by using combinations of Red, Green, and Blue

XP New Perspectives on Microsoft Office Access 2003 Tutorial A function that modifies an object’s color

XP New Perspectives on Microsoft Office Access 2003 Tutorial RGB values for some colors

XP New Perspectives on Microsoft Office Access 2003 Tutorial Create Function procedures in a standard module To create new procedures click Modules on the objects bar and then click New. This will open the Code window in the Visual Basic window. To write a Sub procedure,begin with the Sub statement. To write a Function procedure, begin with the Function statement. The Function will end with the End Function statement.

XP New Perspectives on Microsoft Office Access 2003 Tutorial Assign a name to a function The Function statement is then followed with the name of the function. If you will be passing values to the Function, the Function name is followed by a Parameter list (which could be just one item). When you pass a value to a Function, it is called an argument. When naming a Sub procedure or a function, you must follow the rules for naming objects.

XP New Perspectives on Microsoft Office Access 2003 Tutorial Define a function in the Code window

XP New Perspectives on Microsoft Office Access 2003 Tutorial Name and save your module Once you have written your Function, you will need to save the module that holds the function. When you click the Save button, you will be prompted to supply a name for the module. This is not the same as naming the Function and you do not have to follow the name rules mentioned previously. Recall that a module will hold a collection of procedures so you will want to name the module something that will indicate what is inside the module.

XP New Perspectives on Microsoft Office Access 2003 Tutorial Use comments in your functions

XP New Perspectives on Microsoft Office Access 2003 Tutorial Create event procedures VBA is an event-driven language, meaning that when events takes place, procedures are triggered. All event procedures are Sub procedures. Access will automatically name the event in a standard way, which includes: –The name of the control –An underscore –The event name

XP New Perspectives on Microsoft Office Access 2003 Tutorial Add an event procedure To add an event procedure to a class module: –Open the object to which it will be attached –Open its property sheet –On the Event tab, select the event that will trigger the function This will open the code window for the object you have selected. Before you run your procedure, it must first be compiled. The process of compilation is the process of converting the code to a format the computer can understand.

XP New Perspectives on Microsoft Office Access 2003 Tutorial Use the If statement A commonly used control structure is the If statement The If statement provides a condition and then if the condition is true, a set of statements is executed; if it is false, a different set of statements is executed. The If statement created in your tutorial incorporates the IsNull function into it. The IsNull function returns a True value if the argument is empty (no text) and it returns a False value if the argument is not empty.

XP New Perspectives on Microsoft Office Access 2003 Tutorial An example of an If statement

XP New Perspectives on Microsoft Office Access 2003 Tutorial The function execution process

XP New Perspectives on Microsoft Office Access 2003 Tutorial An example of an event procedure

XP New Perspectives on Microsoft Office Access 2003 Tutorial Declare variables in functions and procedures

XP New Perspectives on Microsoft Office Access 2003 Tutorial Primary VBA Data Types

XP New Perspectives on Microsoft Office Access 2003 Tutorial Compile and test Function procedures, Sub procedures and event procedures When you compile a module, Access will look for errors in syntax. If any errors are found, an error message will display. You will need to fix all syntax errors before the modules can complete compilation. When no errors exist, Access will translate the procedure. You should compile a module any time you make changes to make sure there are no syntax errors in its procedures. You should also test each procedure to make sure it performs as you expect.

XP New Perspectives on Microsoft Office Access 2003 Tutorial Modify object properties in VBA code Sometimes you will want values in your database to appear differently in certain circumstances. For example, you might want unpaid balances to appear in red and paid balances to appear in green. You might also want to hide a control under certain circumstances but display it in other circumstances. These kinds of things can be accomplished with VBA statements.

XP New Perspectives on Microsoft Office Access 2003 Tutorial Hide text and change display colors The ForeColor property of an object represents the color of the text in the object. You can write statements that will alter this property when a certain condition arises The Visible property of an object is set to True when you want the object to be displayed and False when you want it hidden. This property can be altered by writing VBA statements to change the property under certain circumstances. Finally, you can use the DoCmd statement to execute an action in a function.

XP New Perspectives on Microsoft Office Access 2003 Tutorial Hiding and Changing Display Colors

XP New Perspectives on Microsoft Office Access 2003 Tutorial An event procedure to modify properties