![Page 1: XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications](https://reader034.vdocument.in/reader034/viewer/2022042703/56649e495503460f94b3cfa6/html5/thumbnails/1.jpg)
New Perspectives on Microsoft Office Access 2003 Tutorial 11
1
XP
Microsoft Office Access 2003
Tutorial 11 – Using and Writing Visual Basic for Applications Code
![Page 2: XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications](https://reader034.vdocument.in/reader034/viewer/2022042703/56649e495503460f94b3cfa6/html5/thumbnails/2.jpg)
New Perspectives on Microsoft Office Access 2003 Tutorial 11
2
XPLearn 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.
![Page 3: XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications](https://reader034.vdocument.in/reader034/viewer/2022042703/56649e495503460f94b3cfa6/html5/thumbnails/3.jpg)
New Perspectives on Microsoft Office Access 2003 Tutorial 11
3
XPFunction 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.
![Page 4: XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications](https://reader034.vdocument.in/reader034/viewer/2022042703/56649e495503460f94b3cfa6/html5/thumbnails/4.jpg)
New Perspectives on Microsoft Office Access 2003 Tutorial 11
4
XPThe structure of a VBA module
![Page 5: XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications](https://reader034.vdocument.in/reader034/viewer/2022042703/56649e495503460f94b3cfa6/html5/thumbnails/5.jpg)
New Perspectives on Microsoft Office Access 2003 Tutorial 11
5
XPLearning 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.
![Page 6: XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications](https://reader034.vdocument.in/reader034/viewer/2022042703/56649e495503460f94b3cfa6/html5/thumbnails/6.jpg)
New Perspectives on Microsoft Office Access 2003 Tutorial 11
6
XPReview 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.
![Page 7: XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications](https://reader034.vdocument.in/reader034/viewer/2022042703/56649e495503460f94b3cfa6/html5/thumbnails/7.jpg)
New Perspectives on Microsoft Office Access 2003 Tutorial 11
7
XPUse 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.
![Page 8: XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications](https://reader034.vdocument.in/reader034/viewer/2022042703/56649e495503460f94b3cfa6/html5/thumbnails/8.jpg)
New Perspectives on Microsoft Office Access 2003 Tutorial 11
8
XPEvent properties for a form
![Page 9: XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications](https://reader034.vdocument.in/reader034/viewer/2022042703/56649e495503460f94b3cfa6/html5/thumbnails/9.jpg)
New Perspectives on Microsoft Office Access 2003 Tutorial 11
9
XPThe Visual Basic Code window
![Page 10: XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications](https://reader034.vdocument.in/reader034/viewer/2022042703/56649e495503460f94b3cfa6/html5/thumbnails/10.jpg)
New Perspectives on Microsoft Office Access 2003 Tutorial 11
10
XPRecognize 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
![Page 11: XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications](https://reader034.vdocument.in/reader034/viewer/2022042703/56649e495503460f94b3cfa6/html5/thumbnails/11.jpg)
New Perspectives on Microsoft Office Access 2003 Tutorial 11
11
XPA function that modifies an object’s color
![Page 12: XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications](https://reader034.vdocument.in/reader034/viewer/2022042703/56649e495503460f94b3cfa6/html5/thumbnails/12.jpg)
New Perspectives on Microsoft Office Access 2003 Tutorial 11
12
XPRGB values for some colors
![Page 13: XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications](https://reader034.vdocument.in/reader034/viewer/2022042703/56649e495503460f94b3cfa6/html5/thumbnails/13.jpg)
New Perspectives on Microsoft Office Access 2003 Tutorial 11
13
XPCreate Function proceduresin 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.
![Page 14: XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications](https://reader034.vdocument.in/reader034/viewer/2022042703/56649e495503460f94b3cfa6/html5/thumbnails/14.jpg)
New Perspectives on Microsoft Office Access 2003 Tutorial 11
14
XPAssign 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.
![Page 15: XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications](https://reader034.vdocument.in/reader034/viewer/2022042703/56649e495503460f94b3cfa6/html5/thumbnails/15.jpg)
New Perspectives on Microsoft Office Access 2003 Tutorial 11
15
XPDefine a function in the Code window
![Page 16: XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications](https://reader034.vdocument.in/reader034/viewer/2022042703/56649e495503460f94b3cfa6/html5/thumbnails/16.jpg)
New Perspectives on Microsoft Office Access 2003 Tutorial 11
16
XPName 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.
![Page 17: XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications](https://reader034.vdocument.in/reader034/viewer/2022042703/56649e495503460f94b3cfa6/html5/thumbnails/17.jpg)
New Perspectives on Microsoft Office Access 2003 Tutorial 11
17
XPUse comments in your functions
![Page 18: XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications](https://reader034.vdocument.in/reader034/viewer/2022042703/56649e495503460f94b3cfa6/html5/thumbnails/18.jpg)
New Perspectives on Microsoft Office Access 2003 Tutorial 11
18
XPCreate 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
![Page 19: XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications](https://reader034.vdocument.in/reader034/viewer/2022042703/56649e495503460f94b3cfa6/html5/thumbnails/19.jpg)
New Perspectives on Microsoft Office Access 2003 Tutorial 11
19
XPAdd 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.
![Page 20: XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications](https://reader034.vdocument.in/reader034/viewer/2022042703/56649e495503460f94b3cfa6/html5/thumbnails/20.jpg)
New Perspectives on Microsoft Office Access 2003 Tutorial 11
20
XPUse 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.
![Page 21: XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications](https://reader034.vdocument.in/reader034/viewer/2022042703/56649e495503460f94b3cfa6/html5/thumbnails/21.jpg)
New Perspectives on Microsoft Office Access 2003 Tutorial 11
21
XPAn example of an If statement
![Page 22: XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications](https://reader034.vdocument.in/reader034/viewer/2022042703/56649e495503460f94b3cfa6/html5/thumbnails/22.jpg)
New Perspectives on Microsoft Office Access 2003 Tutorial 11
22
XPThe function execution process
![Page 23: XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications](https://reader034.vdocument.in/reader034/viewer/2022042703/56649e495503460f94b3cfa6/html5/thumbnails/23.jpg)
New Perspectives on Microsoft Office Access 2003 Tutorial 11
23
XPAn example of an event procedure
![Page 24: XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications](https://reader034.vdocument.in/reader034/viewer/2022042703/56649e495503460f94b3cfa6/html5/thumbnails/24.jpg)
New Perspectives on Microsoft Office Access 2003 Tutorial 11
24
XPDeclare variables in functions and procedures
![Page 25: XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications](https://reader034.vdocument.in/reader034/viewer/2022042703/56649e495503460f94b3cfa6/html5/thumbnails/25.jpg)
New Perspectives on Microsoft Office Access 2003 Tutorial 11
25
XPPrimary VBA Data Types
![Page 26: XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications](https://reader034.vdocument.in/reader034/viewer/2022042703/56649e495503460f94b3cfa6/html5/thumbnails/26.jpg)
New Perspectives on Microsoft Office Access 2003 Tutorial 11
26
XPCompile 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.
![Page 27: XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications](https://reader034.vdocument.in/reader034/viewer/2022042703/56649e495503460f94b3cfa6/html5/thumbnails/27.jpg)
New Perspectives on Microsoft Office Access 2003 Tutorial 11
27
XPModify 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.
![Page 28: XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications](https://reader034.vdocument.in/reader034/viewer/2022042703/56649e495503460f94b3cfa6/html5/thumbnails/28.jpg)
New Perspectives on Microsoft Office Access 2003 Tutorial 11
28
XPHide 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.
![Page 29: XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications](https://reader034.vdocument.in/reader034/viewer/2022042703/56649e495503460f94b3cfa6/html5/thumbnails/29.jpg)
New Perspectives on Microsoft Office Access 2003 Tutorial 11
29
XPHiding and Changing Display Colors
![Page 30: XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications](https://reader034.vdocument.in/reader034/viewer/2022042703/56649e495503460f94b3cfa6/html5/thumbnails/30.jpg)
New Perspectives on Microsoft Office Access 2003 Tutorial 11
30
XPAn event procedure to modify properties