1. CREATING A REPORT
There are many ways to create a report in Access 2010, including the following:
Using Report Designer:
Using the Report Designer gives you the most control over creating
reports. You can place any type of control on the design surface and
bind the report to a table, query, or SQL statement. The Report Designer
also enables you to add VBA code behind the report.
Using Layout view:
The Report Designer offers a powerful way to create reports. However,
as you design your reports, you may frequently need to switch between
Print Preview and report design because data from the tables is not
shown at design time. Layout view, as mentioned earlier, was created to
solve this. With Access 2010, you can add different types of controls,
apply formatting, and add sorting, grouping, and totals, all while
viewing the actual data from a table or query.
Using Report Wizard:
The Report Wizard walks you through a report creation process with a
number of options from which to choose. The options include data
settings such as data source; grouping and sorting; and formatting
options, such as layout, orientation, and style. Using the wizard is
also an easy way to create reports using multiple tables.
Programmatically: You can also create reports with code. Access provides a method on the Application object called CreateReport
that can be used to create a report. This method is largely used when
you want to create reports using your own mechanism for gathering input
from the user.
Using Quick Reports:
Click the Report button in the Ribbon's Create tab to create a new
report with a tabular layout. The report includes common elements such
as header and footer sections, and a count of the number of records.
2. WORKING WITH VBA IN REPORTS
More specifically, you'll explore some examples
that use specific events and properties such as CanGrow and CanShrink,
and then move into some of the more common types of reports that you
can create with just a little VBA. Let's start with some of the basics.
To add a module to a report, simply add an event handler for an event such as Open, or set the HasModule property of the report to Yes.
2.1. Control Naming Issues
As you add controls, or even
when you name fields, there are a few things to keep in mind that make
the process of creating queries, forms, and reports much easier. The
first is to avoid the use of spaces or punctuation in field and control
names. Spaces may make your field names easier to read, but they are not
user-friendly when it comes to queries or code. Consider a field named Last Name
with a space in the middle. This field name must be bracketed when used
in a query. Without the space, brackets are not required (although
Access may add them). It turns out that brackets are also required when
you refer to the field in code, so instead of referring to the field
like this:
Me!LastName
you must refer to it like this:
Me![Last Name]
As you can see, using the space
in the name makes it harder to read and write in code. Instead, don't
use spaces in names; use the Caption property of a field or label control to change the text that is displayed.
Another issue that causes
problems is circular references. A circular reference occurs when a
control uses itself in an expression. Say, for example, you drag a field
named LastName to a report, and then decide to change the ControlSource property for the control to:
=Left([LastName], 1)
Unless you rename this control, the LastName control will try to use LastName
in the expression, which is itself an expression — in other words, a
circular reference. To avoid circular references, be sure to rename the
control when you modify the ControlSource. In this example, you might consider renaming LastName to txtLastName.
2.2. The Me Object
The Me keyword is used in class modules in VBA. For forms and reports, Me returns the current form or report object. For a standalone class module, the Me
object returns the current running instance of the class. This is
particularly useful for writing generic code. For example, if you have a
procedure that changes the caption of a report based on the user who is
currently logged on, and you want to write the routine so it can be
used on any report, you might write this:
Public Sub SetReportCaption(objReport as Report)
' Set the report caption to the user name
objReport.Caption = Environ$("USERNAME")
End Sub
To pass a Report
object from the report that is currently opened, pass the Me keyword as
the parameter for the procedure. The following example uses the new Load event for a report to set the caption:
Private Sub Report_Load()
SetReportCaption Me
End Sub