Download presentation
Presentation is loading. Please wait.
1
Microsoft Access 2013 - Illustrated
Unit N: Creating Modules and VBA
2
Objectives Understand modules and VBA Compare macros and modules
Create functions Use If statements Microsoft Office Illustrated
3
Objectives (cont’d.) Document procedures Build class modules
Modify sub procedures Troubleshoot modules Microsoft Office Illustrated
4
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 Illustrated
5
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 Illustrated
6
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 Illustrated
7
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 Illustrated
8
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 Illustrated
9
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 Illustrated
10
Understand Modules and VBA (cont’d.)
VBE window for a standard module Microsoft Office Illustrated
11
Understand Modules and VBA (cont’d.)
Components and text colors for the Visual Basic Editor window Microsoft Office Illustrated
12
Understand Modules and VBA (cont’d.)
Components and text colors for the Visual Basic Editor window (cont’d.) Microsoft Office Illustrated
13
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 Illustrated
14
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 Illustrated
15
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 Illustrated
16
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 Illustrated
17
Compare Macros and Modules (cont’d.)
Visual Basic Editor window for a class module Microsoft Office Illustrated
18
Common VBA keywords Microsoft Office Illustrated
19
Common VBA keywords (cont’d.)
Microsoft Office Illustrated
20
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 Illustrated
21
Create Functions (cont’d.)
End Function statement Automatically added by VBA Required statement marking the end of a function Microsoft Office Illustrated
22
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 Illustrated
23
Create Functions (cont’d.)
Microsoft Office Illustrated
24
Three-Character Prefix Naming Conventions
Microsoft Office Illustrated
25
Use If Statements If...Then...Else logic: tests logical conditions and executes statements only if the conditions are true Microsoft Office Illustrated
26
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 Illustrated
27
Document Procedures (cont’d.)
Adding comments to a module Microsoft Office Illustrated
28
Document Procedures (cont’d.)
Standard toolbar buttons in the Visual Basic window Microsoft Office Illustrated
29
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 Illustrated
30
Build Class Modules (cont’d.)
Creating an event handler procedure Microsoft Office Illustrated
31
Build Class Modules (cont’d.)
Copying the If structure to a new event-handler procedure Microsoft Office Illustrated
32
Build Class Modules (cont’d.)
Branch of Service combo box is visible when the Veteran check box is checked Microsoft Office Illustrated
33
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 Illustrated
34
Modify Sub Procedures (cont’d.)
IntelliSense technology prompts you as you write VBA statements Microsoft Office Illustrated
35
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 Illustrated
36
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 Illustrated
37
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 Illustrated
38
Troubleshoot Modules (cont’d.)
Setting a breakpoint Microsoft Office Illustrated
39
Troubleshoot Modules (cont’d.)
Stopping execution at a breakpoint Microsoft Office Illustrated
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.