0% found this document useful (0 votes)
59 views

Access: Introduction To Macros and SQL: What Is A Macro?

This document discusses macros and SQL in Access. It provides examples of creating stand-alone macros to automate tasks, embedded macros attached to controls like buttons, and data macros for table events. It also demonstrates using a SQL SELECT statement as a record source in a report to filter data and ensure only the desired records are retrieved.

Uploaded by

S P
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Access: Introduction To Macros and SQL: What Is A Macro?

This document discusses macros and SQL in Access. It provides examples of creating stand-alone macros to automate tasks, embedded macros attached to controls like buttons, and data macros for table events. It also demonstrates using a SQL SELECT statement as a record source in a report to filter data and ensure only the desired records are retrieved.

Uploaded by

S P
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Access: Introduction to Macros and SQL

What is a macro?
A macro is a series of actions programmed to automate repetitive tasks.

Macros in Access
Access provides two categories of macros: stand-alone macros and embedded macros.

A stand-alone macro is a separate database object. A stand-alone macro appears as an object in the
Navigation Pane and it can be run independently. It is also available to any control or object in the
database.

An embedded macro is embedded in an object or a control. With an embedded macro, an event occurs
that is attached to a control or an object and the event triggers the embedded macro. An example of
an event would be the On Click event that occurs when the user clicks on a button or the Before Change
event that is triggered each time a user enters or edits data in a field in a table.

Using the Macro Builder


In Access, one way to create and edit macros is by using the Marco Builder.

Creating a Stand-Alone Macro


You will create a stand-alone macro to automatically open 2 reports.

1. Open the accessMacros_Practice database.


2. Click the CREATE tab and then click Macros in the Macros & Code group.
3. Click the Add New Action arrow and then select OpenReport.
4. Select Locations as the Report Name argument and Report as the View argument.

Scottsdale Community College, CIS 105, Access 1


5. Click the Add New Action arrow a second time and then select OpenReport.
6. Select Employee Compensation as the Report Name argument and Report as the View
argument.
7. Save the macro as Open2Reports. The Open2Reports macro now appears in the Macros Group
in the Navigation Pane.

8. Close the macro. Double-click the Open2Reports macro to test that it executes properly. Both
reports should open.
9. Close both the reports.
10. Save the database.

Scottsdale Community College, CIS 105, Access 2


Creating an Embedded Macro
You will create and attach an embedded macro to the On Click event for a button. Note: there are 2
buttons on the Main Menu form. You will be adding a 3rd button and attaching an embedded macro to
the button.

1. Double-click on the Main Menu form in the Navigation Pane. Click the Open Locations Form
button on the Main Menu form. The Locations form should open. Close the Locations form.
2. Click the Exit button on the Main Menu form. This button will close your database and exit
Access.
3. Reopen the accessMacros_Practice database.
4. Click on the Main Menu form and display it in Design View. Click on the Button control in the
DESIGN tab of the FORM DESIGN TOOLS Ribbon.

5. Click and drag to size a button directly below the Open Locations Form button and above the
Exit button.
6. When the Command Button Wizard appears, select Report Operations from Categories and
Open Report from Actions. Click the Next > button.

Scottsdale Community College, CIS 105, Access 3


* If Command Button Wizard does not appear, click on the Controls More arrow to ensure the
Use Control Wizards option is turned on.

7. Then select Employee Compensation as the report to open. Click the Next > button.
8. Click the Text radio button and type Open Employee Report as the text to display on the button.

9. Click the Next > button.


10. Name your button cmdReport and then click the Finish button.
11. Save your Main Menu form.
12. Display your Main Menu form in Form View.

Scottsdale Community College, CIS 105, Access 4


13. Click on your new button to test that it works properly. The Employee Compensation Report
should open.
14. Close the report and Menu Form and save the database.

Creating a Data Macro


A data macro is a specific type of embedded macro that is only used with table events. A table event
occurs when a user adds, edits, or deletes table data. You will create a data macro for a Before Change
event.

1. Open the Employees table in Datasheet View. Note: the InsuranceDate field for every record is
currently blank.
2. Switch to Design View.
3. Click Create Data Macros in the Field, Record & Table Events group on the Design tab, and then
click the Before Change event.

Scottsdale Community College, CIS 105, Access 5


4. Click the drop down arrow and select Set Field. In the Name argument type InsuranceDate and
in the Value argument type [HireDate] + 60 to set the date for insurance eligibility to be 60 days
after the employee’s hire date.

5. Save and close the data macro.


6. Save your Employees table.
7. Reopen your Employees table in Datasheet View and navigate to the hire date for the first
employee, Tom Lacher. Note: the InsuranceDate field is blank. Change the HireDate value to
3/20/2013. Using the down arrow, navigate to the next employee record. The InsuranceDate
for Tom Lacher will now show as 5/19/2013 because your data macro was executed on the
Before Change event.

8. Change the HireDate for Laurie Fantis to 1/11/2013 and navigate to the next record. The
InsuranceDate for Laurie Fantis will now show as 3/12/2013.
9. Save and close your database.

Using Structured Query Language


Structured Query Language (SQL) is the industry standard language for defining, manipulating, and
retrieving data in a relational database.

An SQL SELECT Statement is used to retrieve specific data from the database. Keywords in the SELECT
statement have specific meaning and purpose:

• SELECT phrase – specifies the field(s) to be retrieved


• FROM clause – specifies table(s) or queries containing the data to be returned
• WHERE clause – sets the criteria (qualifies) for the data to be returned
• ORDER BY clause – determines how the data returned will be sorted (ascending or descending)

Scottsdale Community College, CIS 105, Access 6


An SQL SELECT Statement can be used as a Record Source in forms or reports to ensure the proper data
is retrieved from the database.

Add an SQL Record Source to a Report


You will create a new report and specify an SQL Record Source.

1. With the Employees table selected in the Navigation Pane, create a new report.
2. Save this new report as Employees. Scroll to the bottom of the report to note there are 311
records.
3. Display your Employees report in Design View.
4. On the Property Sheet, display the Data Tab.
5. Notice Employees shows in the Record Source. Employees is the name of the table you used to
create this report.

There are 4 job titles in this company (T01, T02, T03, and T04). You are designing this report to only
display employees with the title of “T01”.

6. Make sure your cursor is in the Record Source textbox. Right-click or press Shift+F2 to display
the Zoom window. Type the following SQL SELECT statement in the Zoom window:
SELECT * FROM Employees WHERE Title=”T01”;

Scottsdale Community College, CIS 105, Access 7


Note: The (*) is used to select ALL fields in the Employees table. The WHERE clause limits the
selection to only records with a title of “T01”.

7. Click the OK button.


8. Save the Employees Report.
9. Display the report in Report View. Notice there are now 190 records in the report and these are
only the employees with the title of “T01”.
10. Save your database as accessMacros_Practice_LastFirst . Close your database. Submit your
database file per your instructor’s directions.

Scottsdale Community College, CIS 105, Access 8

You might also like