Microsoft Access 2013 - Illustrated Unit N: Creating Modules and VBA
Objectives Understand modules and VBA Compare macros and modules Create functions Use If statements Microsoft Office 2013 - Illustrated
Objectives (cont’d.) Document procedures Build class modules Modify sub procedures Troubleshoot modules Microsoft Office 2013 - Illustrated
Understand Modules and VBA Module: an Access object that stores Visual Basic for Applications (VBA) programming code Visual Basic Editor (VBE): program for writing VBA code Microsoft Office 2013 - Illustrated
Understand Modules and VBA (cont’d.) Two kinds of modules in an Access database Standard modules: contain global code executable from anywhere in the database Displayed as module objects in the Navigation Pane Class modules: code stored within the form or report object itself; can only be executed in that object Microsoft Office 2013 - Illustrated
Understand Modules and VBA (cont’d.) Module: contains VBA programming code organized in procedures Procedure: contains several lines of code; each line is a statement. Comments: text that helps explain and document the code Microsoft Office 2013 - Illustrated
Understand Modules and VBA (cont’d.) Procedure: a series of VBA statements Performs an operation or calculates an answer Two types of procedures: Functions Subs (sub procedure) Microsoft Office 2013 - Illustrated
Understand Modules and VBA (cont’d.) Function: a procedure that returns a value Sub: performs a series of VBA statements to manipulate controls and objects Arguments: constants, variables, or expressions Passed to a procedure; required in order for the procedure to execute Microsoft Office 2013 - Illustrated
Understand Modules and VBA (cont’d.) Object: any item that can be identified or manipulated Includes: traditional Access objects (table, query, form, etc.) as well as other items (e.g., controls, sections, and existing procedures) that have properties Method: an action that an object can perform Microsoft Office 2013 - Illustrated
Understand Modules and VBA (cont’d.) VBE window for a standard module Microsoft Office 2013 - Illustrated
Understand Modules and VBA (cont’d.) Components and text colors for the Visual Basic Editor window Microsoft Office 2013 - Illustrated
Understand Modules and VBA (cont’d.) Components and text colors for the Visual Basic Editor window (cont’d.) Microsoft Office 2013 - Illustrated
Compare Macros and Modules Macros: easy way to handle common, repetitive, and simple tasks Examples: open and close forms, show and hide toolbars, and print reports Macros: generally easier to create than modules Macro Design View is more structured than the VBE Microsoft Office 2013 - Illustrated
Compare Macros and Modules (cont’d.) Creating a module: requires VBA programming language knowledge Must apply correct syntax (rules) for each VBA statement Macros: simpler to create VBA: more powerful Macro: must be used to make global, shortcut key assignments Microsoft Office 2013 - Illustrated
Compare Macros and Modules (cont’d.) AutoExec: special macro name Automatically executes when the database first opens Use a module to: Create a unique function Detect Access error and display custom message Apply more powerful If-Then logic Microsoft Office 2013 - Illustrated
Compare Macros and Modules (cont’d.) Use a module to (cont’d.): Declare variables Work in conjunction with SQL (Structured Query Language) to select, update, append, and delete data Microsoft Office 2013 - Illustrated
Compare Macros and Modules (cont’d.) Visual Basic Editor window for a class module Microsoft Office 2013 - Illustrated
Common VBA keywords Microsoft Office 2013 - Illustrated
Common VBA keywords (cont’d.) Microsoft Office 2013 - Illustrated
Create Functions Access supplies hundreds of functions such as: Sum, Count, IIf, First, Last, Date, and Hour Create a custom function in a module Can be used in any query, form, or report throughout the database Microsoft Office 2013 - Illustrated
Create Functions (cont’d.) End Function statement Automatically added by VBA Required statement marking the end of a function Microsoft Office 2013 - Illustrated
Create Functions (cont’d.) Common VBA programming practice: apply three-character prefixes to name objects and controls Prefix bas: short for Basic; applies to global modules Microsoft Office 2013 - Illustrated
Create Functions (cont’d.) Microsoft Office 2013 - Illustrated
Three-Character Prefix Naming Conventions Microsoft Office 2013 - Illustrated
Use If Statements If...Then...Else logic: tests logical conditions and executes statements only if the conditions are true Microsoft Office 2013 - Illustrated
Document Procedures Comment lines: statements in the code that document the code; do not affect how the code runs Start with an apostrophe and are green in the VBE Microsoft Office 2013 - Illustrated
Document Procedures (cont’d.) Adding comments to a module Microsoft Office 2013 - Illustrated
Document Procedures (cont’d.) Standard toolbar buttons in the Visual Basic window Microsoft Office 2013 - Illustrated
Build Class Modules Class modules: contained and executed within specific forms and reports Most commonly run in response to an event (a specific action that occurs as the result of a user action) Microsoft Office 2013 - Illustrated
Build Class Modules (cont’d.) Creating an event handler procedure Microsoft Office 2013 - Illustrated
Build Class Modules (cont’d.) Copying the If structure to a new event-handler procedure Microsoft Office 2013 - Illustrated
Build Class Modules (cont’d.) Branch of Service combo box is visible when the Veteran check box is checked Microsoft Office 2013 - Illustrated
Modify Sub Procedures Sub procedures: can be triggered on any event identified in the Property Sheet Examples: On Got Focus (when control gets the focus), After Update (after a field is updated), or On Dbl Click (when control is double-clicked) Item’s set of event properties: depends on the type of item Microsoft Office 2013 - Illustrated
Modify Sub Procedures (cont’d.) IntelliSense technology prompts you as you write VBA statements Microsoft Office 2013 - Illustrated
Troubleshoot Modules Several techniques in Access for debugging different types of VBA errors Types of errors Syntax error: cannot be read by the Visual Basic Editor Occurs immediately as VBA statement is written Microsoft Office 2013 - Illustrated
Troubleshoot Modules (cont’d.) Types of errors (cont’d.) Compile-time errors: incorrectly constructed code Detected as soon as code is run or when Compile option selected on the Debug menu Run-time errors: incorrectly constructed code; stops code execution Examples: attempting an illegal operation e.g., dividing by zero Microsoft Office 2013 - Illustrated
Troubleshoot Modules (cont’d.) Types of errors (cont’d.) Logic errors: code runs without obvious problems; procedure does not produce the desired result Most difficult errors to troubleshoot Microsoft Office 2013 - Illustrated
Troubleshoot Modules (cont’d.) Setting a breakpoint Microsoft Office 2013 - Illustrated
Troubleshoot Modules (cont’d.) Stopping execution at a breakpoint Microsoft Office 2013 - Illustrated