Download presentation
Presentation is loading. Please wait.
1
Microsoft Access 2003 Illustrated Complete
Creating Modules and VBA
2
Objectives Understand modules and VBA Compare macros and modules
Create functions Use If statements Creating Modules and VBA Unit O
3
Objectives Document procedures Create class modules
Create sub procedures Troubleshoot modules Creating Modules and VBA Unit O
4
Understanding Modules
A module is an Access object that stores Visual Basic for Applications (VBA) programming code VBA is written in the Visual Basic Editor Code window Two kinds of modules: Class module: contains VBA code stored in forms and reports Standard module: contains global code that can be executed from anywhere in the database Creating Modules and VBA Unit O
5
Understanding Modules
A module contains VBA code organized in procedures A procedure is several lines of VBA code, each of which is called a statement A module may contain comments A procedure performs an operation or calculates an answer A function is a procedure that returns a value A sub (sub procedure) performs a series of VBA statements that manipulates an object or control Creating Modules and VBA Unit O
6
Understanding Modules
In VBA, an object is any item that can be identified or manipulated A method is an action that an object can perform An event is a specific action that occurs on or to an object Creating Modules and VBA Unit O
7
Understanding Modules
Object list Procedure list Declaration statements Project Explorer Window VBA code in Code window Creating Modules and VBA Unit O
8
Elements in the Visual Basic Code window
Creating Modules and VBA Unit O
9
Comparing Macros and Modules
Both macros and modules help run your database efficiently A macro is best suited to handle repetitive, simple tasks such as opening and closing forms Macros are generally easier to create because you don’t have to know any programming syntax To create a module, you must know a programming language, VBA Creating Modules and VBA Unit O
10
Comparing Macros and Modules
You must use modules to create unique functions Use VBA procedures to detect Access errors VBA code stored in modules works with other Microsoft Office products Creating Modules and VBA Unit O
11
Common VBA keywords Creating Modules and VBA Unit O
12
Converting macros to Visual Basic
Click the Macros button on the Objects bar, then click the name of the macro you want to convert If the macro is stored in a form or report, open the form or report in Design View, click the Tools menu, point to Macro, then click Convert Form’s (or Report’s) Macros to Visual Basic Creating Modules and VBA Unit O
13
Creating Functions Create a function to automate and simplify the unique calculations for your organization Create a new function in a standard module so that it can be used in any query, form, or report in the database Creating Modules and VBA Unit O
14
New function, EmployeePrice
Creating Functions New function, EmployeePrice Creating Modules and VBA Unit O
15
Using IF Statements If…Then…Else logic allows you to test logical conditions and execute statements only if the conditions are true End the If…Then…Else structure with an End If statement Creating Modules and VBA Unit O
16
Using If Statements Creating Modules and VBA Unit O
17
Documenting Procedures
Comment lines are statements in the code that document the code, and do not affect how the code runs Comment lines start with an apostrophe (‘) and are green in the Code window Creating Modules and VBA Unit O
18
Documenting Procedures
Comment lines Creating Modules and VBA Unit O
19
Standard toolbar buttons in the Visual Basic window
Creating Modules and VBA Unit O
20
Creating Class Modules
Class modules are contained within forms and reports Equipment Entry Form CloseForm_Click() sub Creating Modules and VBA Unit O
21
Creating Sub Procedures
If an event procedure, the name of the sub procedure contains the name of the object and event that triggers the code Form_DblClick() PrintButton_Click() Form_Current() Creating Modules and VBA Unit O
22
Creating Sub Procedures
Form_DblClick PrintThisRecordButton_Click Creating Modules and VBA Unit O
23
Troubleshooting Modules
Three types of errors. Compile-time errors: occur as a result of incorrectly constructed code Missing code or syntax error Run-time errors: occur after the code starts to run Include attempting an illegal operation Logic errors: occur when the code runs without problems, but the procedure doesn’t produce the desired result Hardest to troubleshoot Creating Modules and VBA Unit O
24
Troubleshooting Modules
Use a breakpoint to stop code at a particular statement Creating Modules and VBA Unit O
25
Summary VBA stands for Visual Basic for Applications, the programming language that comes with Microsoft Office products Use VBA to create new functions and automate processes VBA is stored in standard and class modules Class modules are stored in forms and reports Creating Modules and VBA Unit O
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.