Sie sind auf Seite 1von 9

Ethiopian TVET System

Training, Teaching and Learning Material

Ethiopian TVET-System

INFORMATION TECHNOLOGY SUPPORT SERVICE

Level II

LEARNING GUIDE # 1

Unit of Competence: Operate Database Application


Module Title: Operating Database Application
LG Code: ICT ITS2 LO1 01
TTLM Code: ICT ITS2 TTLM 0511

LO 5: Create database Macros & Modules


TTLM Development Manual Date: 05-2011 Page 1 of 9
3rd Revision Author: ADDIS ABABA TEGBARE-ID
Ethiopian TVET System
Training, Teaching and Learning Material

Information Sheet-1 Creating Database Macros & Modules

Macros

 Macros in Access can be thought of as a simplified programming language which you can use to add
functionality to your database.
 For example, you can attach a macro to a command button on a form so that the macro runs whenever the
button is clicked.
 Macros contain actions that perform tasks, such as opening a report, running a query, or closing the database.
 Most database operations that you do manually can be automated by using macros, so they can be great time-
saving devices.
 A macro is essentially a list of actions that you apply to objects to respond to events.
 Each action carries out one task.
 You create your actions in the order you want them to execute.
 In addition, you specify the arguments of the actions, giving the program additional information as needed.
 You can set conditions for each action in a macro to determine whether it runs or not.
 Run a macro by applying it to the event property of an object.
 Once the specified event occurs the macro will run by running the all the specified actions.
 Actions that have conditions applied to them may or may not run depending on whether or not they passed the
conditional tests.
 Once you've created your macros you'll see them listed in the Macros tab in the Database window.
 This way you can attach any macro to any event property in your database.

Macro Creation

 Create and manage macros from the "Macros" section in the Access database window.
 Select an action from the "Action" drop-down menu.
 Choose your action arguments at the bottom of the window.
 This includes choosing the object the action applies to, the object name, sizes, positions, application
name, and any other arguments that apply to your selected action.
 The "Action" column is the only portion of the macro window that must be filled out. You must have at
least one action per macro.
 Create a new action by going to the next row.
 Remember that all actions in the macro will run each time the macro is run.
TTLM Development Manual Date: 05-2011 Page 2 of 9
3rd Revision Author: ADDIS ABABA TEGBARE-ID
Ethiopian TVET System
Training, Teaching and Learning Material
Actions

 Actions are the heart of the macro.


 Multiple actions are used to run common procedures or actions.
 For instance, if you would normally have a user press a button to run a report and then a separate
button to print the report, you could create a macro that automatically runs and prints the report
when a button is pushed.
 Actions include events such opening a form, report, or query, printing specified objects, closing
objects, find records, and transfer data to other applications.
 You can only alter an action if the cursor is on the row of your action.

Comments

 Though not required, comments help organize lengthy macros.


 Comments are strings of text that don't affect your actions in any way.
 However, they can tell you what the action's purpose is.
 For instance, if you have an action to open a form after a report has finished printing, you may have a
comment stating the name of the form that's opened after the name of the report has printed.
 When you have multiple actions, comments can greatly reduce troubleshooting time and help give
you a quick overview of what your macro's purpose is.

Running Macros

 Macros can be run from most objects and events.


 If you want to test your macro, click the red exclamation point in the toolbar when your macro is in
design view.
 You can also double-click any existing macro to run it.
 Right-click any object on a form, such as a button or drop-down box, and select "Properties."
 Go to the "Event" tab. Enter the name of your macro--you must be exact--in the box next to the
event you want the macro associated with.
 If you create a new object with the wizard, especially with buttons, you can select your macro
without going to the object's properties.
 Note, this is only for newly created objects.
 If you want to run a macro based on a form or report's properties,

TTLM Development Manual Date: 05-2011 Page 3 of 9


3rd Revision Author: ADDIS ABABA TEGBARE-ID
Ethiopian TVET System
Training, Teaching and Learning Material
 Open the object in design view, right-click the small square in the top left of the object, and
click "Properties."
 Add the macro name to the appropriate event.
 The macro will then run when the event occurs.

Modules
 Modules, like macros, are objects you can use to add functionality to your database.
 Whereas you create macros in Access by choosing from a list of macro actions, you write modules in
the Visual Basic for Applications (VBA) programming language.
 A module is a collection of declarations, statements, and procedures that are stored together as a unit.
 A module can be either a class module or a standard module.
 Class modules are attached to forms or reports, and usually contain procedures that are specific to the
form or report they're attached to.
 Standard modules contain general procedures that aren't associated with any other object.
 Standard modules are listed under Modules in the Navigation Pane, whereas class modules are not.
 Now that you know how to put together a program, you are ready to give it a try.
 To create a VBA procedure, you follow many of the same steps you follow when you created macros.
The general steps in VBA programming are as follows:

1. Identify the task you want to accomplish.


2. Plan the steps needed to accomplish that task.
3. Create the programming code necessary to implement the steps.
4. Test the program.
5. Refine the program.
6. Repeat steps 4 and 5 until the program works correctly.

 You create VBA programming code by using the VBA Editor, which is described in the following
section.

TTLM Development Manual Date: 05-2011 Page 4 of 9


3rd Revision Author: ADDIS ABABA TEGBARE-ID
Ethiopian TVET System
Training, Teaching and Learning Material
What is the VBA Editor?

 You create VBA programs using the VBA Editor. To start the VBA Editor,
 First click your mouse on the Modules button in the Database window.
 Then, click your mouse on the New button. Access, in turn, displays the VBA Editor, as shown in figure

Notice

 There are several different parts to the VBA Editor.


 In the upper left corner is what the editor refers to as the Project window.
 This is where you can see the different elements of your project and any modules that have been
defined in the workbook.
 Just below the Project window is the Properties window.
 Here you can specify different attributes of whatever you have selected in the Project window.
 For most simple development needs, you will never do much with the Properties window.
 To the right of the Properties window, and at the very bottom of the screen, is the immediate window.
 This is where you can either test parts of your procedures during development or you can find the
immediate results of various commands.
 The Immediate window comes in very handy during testing and debugging, when they are necessary.

Note: If a window is not displayed within the VBA Editor, you can display it by choosing one of the
options from the View menu. For instance, if you want to display the Project window, choose
Project Explorer from the View menu.

TTLM Development Manual Date: 05-2011 Page 5 of 9


3rd Revision Author: ADDIS ABABA TEGBARE-ID
Ethiopian TVET System
Training, Teaching and Learning Material
 The Module window, which is the largest window on the screen, is where you do your programming.
 At the top of the Module window is the two drop-down lists.
 The one on the left is called the Object box.
 The one on the right is the Procedure box.
 You use the Object box to select which object you want to work with.
 When you first create a module, the object is set to the word General, meaning you are working on a
general module, not on one associated with a particular object in a form or report.
 The Procedure box is where you indicate the name of the procedure on which you want to work.
 If you choose or specify a different procedure in this box, the information Access shows in the Module
window changes to reflect the VBA statements you have assigned to that procedure.
 The top level of a module is the Declarations section; it begins with the procedure name you indicate
in the Procedure box when you first create a module. Take a look at the Module window.
 It contains the programming code already defined for the declarations section.
 In this instance, there is only one line of code defined--a statement that indicates the database's
default sort order.
 To enter programming statements into a procedure, you type them in the Module window.
 As you enter information, Access checks to make sure it can understand what you type.
 In other words, Access checks the syntax of what you enter.
 You use the correct syntax when you follow the VBA rules of grammar.
 You can cut, copy, and paste sections of code using standard Windows mouse or keyboard techniques.
 You can perform these operations either in the same procedure or between different procedures.

Data Access Pages

 A data access page is a special type of Web page designed for viewing and working with data from the
Internet or an intranet— data that is stored in a Microsoft Access database or a Microsoft SQL Server
database.
 The data access page may also include data from other sources, such as Microsoft Excel.
 Using a data access page is similar to using a form: You can view, enter, edit, and delete data in a
database.
 However, you can also use a page outside a Microsoft Access database, so users can update or view
data over the Internet or an intranet.

TTLM Development Manual Date: 05-2011 Page 6 of 9


3rd Revision Author: ADDIS ABABA TEGBARE-ID
Ethiopian TVET System
Training, Teaching and Learning Material
Parts of a data access page

 The body - The body is the basic design surface of a data access page.
 On a page that supports data entry, you can use it to display informational text, controls bound
to data, and sections.
 Sections - You use sections to display text, data from a database, and toolbars.
 Two types of sections are typically used on pages that support data entry:
 Group header and Record navigation sections.
 A page can also have footer and caption sections.

 Group header and footer: Used to display data and calculate values.
 Record navigation: Used to display the record navigation control for the group level.
o A record navigation section for a group appears after the group header section.
o You can't place bound controls in a record navigation section.
 Caption: Used to display captions for text boxes and other controls. It appears immediately
before the group header.
o You can't place bound controls in a caption section.

 Each group level in a data access page has a record source. The name of the record source is displayed
on the section bar for each section used for a group level.

Designing different types of data access pages

 You design data access pages in Design view in Microsoft Access.


 The page is a separate file that is stored outside Access; however, when you create the file, Access
automatically adds a shortcut to the file in the Database window.
 Designing a data access page is similar to designing forms and reports— you use a field list, the
toolbox, controls, and so on.
 However, there are some significant differences in the way that you design and interact with data
access pages as opposed to forms and reports.
 How you design the page depends on what it will be used for:

TTLM Development Manual Date: 05-2011 Page 7 of 9


3rd Revision Author: ADDIS ABABA TEGBARE-ID
Ethiopian TVET System
Training, Teaching and Learning Material
 Interactive reporting: This type of data access page is often used to combine and group
information that is stored in the database, and then publish summaries of the data.
 For example, a page might publish the sales performance for each region in which you
do business. Using expand indicators, you can go from a general summary of the
information, such as a list of all the regions and their combined sales total, to specific
details on individual sales within each region.
 The data access page might provide toolbar buttons for sorting and filtering the data, as well as
for adding, editing, and deleting the data in some or all group levels.

 Data analysis: This type of data access page may include a PivotTable list, similar to a Microsoft
Excel PivotTable report, that lets you reorganize the data to analyze it in different ways.
 The page might contain a chart that you can use to analyze trends, detect patterns, and compare
data in your database. Or it might contain a spreadsheet, in which you can enter and edit data and
use formulas to calculate as you do in Excel.

Using data access pages in Internet Explorer

 A data access page is connected directly to a database.


 When users display the data access page in Internet Explorer, they are viewing their own copy of the
page.
 That means any filtering, sorting, and other changes they make to the way the data is displayed—
including changes they make within a PivotTable list or spreadsheet— affect only their copy of the
data access page.
 However, changes that they make to the data itself— such as modifying values, and adding or deleting
data— are stored in the underlying database, and therefore are available to everyone viewing the
data access page.

Note :- To view and work with the data access page on the Internet or an intranet, users need
Microsoft Internet Explorer 5.01 with Service Pack 2 (SP2) or later.

TTLM Development Manual Date: 05-2011 Page 8 of 9


3rd Revision Author: ADDIS ABABA TEGBARE-ID
Ethiopian TVET System
Training, Teaching and Learning Material
Using data access pages in Microsoft Access

 You can also work with a data access page in Page view in Access.
 Data access pages can supplement the forms and reports that you use in your database application.
 When deciding whether to design a data access page, form, or report, consider the tasks that you want
to perform.

TTLM Development Manual Date: 05-2011 Page 9 of 9


3rd Revision Author: ADDIS ABABA TEGBARE-ID

Das könnte Ihnen auch gefallen