You are on page 1of 25

Event and Macro

Pengantar Basis Data


Event and Macro Sunday, February 28, 2021
Programming in Access

 Access applications are event-driven


 Object in Access response to many types of events
 An action can trigger event that run a Macro or event
procedure
 Access provides a programming language called Visual basic
for applications edition (VBA), which extends the
capabilities of Access, offering power beyond the scope of
macros.
 All forms, reports, and their controls can have event
procedures or a macro associated with their events

Event and Macro Sunday, February 28, 2021


Event in Access

 Windows (Form, report) Events: opening, closing, and


resizing
 Data events: Making current, deleting, or updating
 Focus events: Activating, entering, and existing
 Keyboard events: KeyDown, KeyPress, KeyUp
 Print events: Formatting and printing
 Error and timing events: Happening after an error has
occurred or some time has passed
 Mouse Events: Click, DblClick, MouseDown, MouseMove,
MouseUp
Event and Macro Sunday, February 28, 2021
Event and Macro Sunday, February 28, 2021
Event and Macro Sunday, February 28, 2021
Macro

 A macro : set of one or more actions that each


perform a particular operation, such as opening a
form or printing a report. Macros can help you to
automate common tasks. For example, you can
run a macro that prints a report when a user
clicks a command button.
 Can also set conditions under which the macro
action is to be performed; display a message box
if a field contains a certain value or is blank.
Event and Macro Sunday, February 28, 2021
The process of Using Macros

 The process of using macros to automate an


application is a simple one, composed of these
steps:
 Create and save the macro
 Open the form or report to which the macro applies and
select the specific control
 Open the property sheet for the control and click the event
tab
 Click the desired event property and choose the macro
name from the drop down list
 Close and save the form or report
Event and Macro Sunday, February 28, 2021
Macro in Access 2010

In Microsoft Access 2010, macros that are attached to


user interface (UI) objects such as command buttons,
text boxes, forms, and reports are known as UI macros.
This distinguishes them from data macros, which are
attached to tables.
UI Macros

 How To Create UI Macros ?


1.Create a standalone macro
 This procedure creates a standalone macro object that will
appear under Macros in the Navigation Pane. Standalone macros
are useful when you want to reuse the macro in many places of
the application. By calling the macro from other macros, you can
avoid duplicating the same code in multiple places.
 On the Create tab, in the Macros & Code group, click Macro.
Access opens the Macro Builder.

 On the Quick Access Toolbar, click Save.


 In the Save As dialog box, type a name for the macro, and then
click OK.
 Continue with the section Add actions to a macro.
UI Macros (Cont.)

2.Create Embedded Macro


 This procedure creates a macro that is embedded in an event property of
an object. Such a macro does not appear in the Navigation Pane, but can
be called from events such as On Load or On Click.
 Because the macro becomes part of the form or report object, embedded
macros are recommended for automating tasks that are specific to a
particular form or report.
 In the Navigation Pane, right-click the form or report that will contain the
macro, and then click Layout View.
 If the property sheet is not already displayed, press F4 to display it.
 Click the control or section that contains the event property in which you
want to embed the macro. You can also select the control or section (or
the entire form or report) by using the drop-down list under Selection
Type at the top of the property sheet.
 In the Property Sheet task pane, click the Event tab.
Add Action to a Macro
Add Action to a Macro
Example

 Create macro MDelete :


Example (Cont.)

 Add a “Delete” button to your form


 Open“Delete” button property sheet. For “On click”
event , choose : MDelete macro
 Run your form
Example (Cont.)

 Result :
Macro and Switchboard Sunday, February 28, 2021
Macro and Switchboard Sunday, February 28, 2021
Macro and Switchboard Sunday, February 28, 2021
Data Macro

 Data macros are a new feature of Access 2010 which enable you to
add logic to events that occur in tables, such as adding, updating, or
deleting data. They are similar to "triggers" in Microsoft SQL Server.
 Data macros are managed from the Table tab while viewing a table
in Datasheet view, and do not appear under Macros in the
Navigation Pane.
 you can use data macros to validate and ensure the accuracy of data
in a table.
 There are two main types of data macros
 those that are triggered by table events (also called "event-driven" data
macros),
 and those that run in response to being called by name (also known as
"named" data macros).
Data Macro (Cont.)

1
Data Macro (Cont.)

1
Example

 Pada tabel “Order Details”.


 Pilih “Before Change” pada tab “Before Events”
 Buat macro sebagai berikut :
Example (Cont.)

 Result:
Thank You

Macro and Switchboard Sunday, February 28, 2021

You might also like