![]() |
Printing |
|
Table, Query, and Form Printing |
|
Introduction to Printing |
![]() |
Printing consists of writing or drawing, on a piece of paper, the values held by an object of a database. This means that you can print from a table, a query, or a form. Before doing this, of course the object must exist and it must have records. To print from a table, a query, or a form, in the appropriate section of the database window, right-click the object and click Print... When you do this, the document is sent directly to the printer. |
The tables, queries, and forms provide a fast means of printing records without having to do any particular design. Instead of directly sending a document to a printing, you can choose what records to print. To do this, you must first open the table, query, or form. To print a particular record, if you are using a table or a query, click any cell in that record. To print a record from a table, first navigate to that record. In both cases, on the main menu, click File -> Print... This would display the Print dialog box. From it, you can click the Selected Record(s) radio button:
To print more than one record, first select them and then proceed in the same way.
Like a table or a form, there are various ways you create a report. From the main menu, you can click Insert -> Report. From the Reports section of the Database window, you can click the New button or, on the Database toolbar, you can click the arrow of the New Object button and click Report. Any of these actions would display the New Report dialog box:
You can create a report that would be used to print simple text that is not related to any data source. To create such a report, when in the New Report dialog box, you can click Design View, make sure the combo box is empty, and click OK. Otherwise, the New Report displays the same options as the New Form dialog box. After creating a report, as we will see in the next few sections, to keep it in your database, you must save it by giving it a name. The name of a report follows the rules and suggestions we have applied to tables, queries, and forms so far.
To programmatically create a report, you can call the CreateReport() method of the Application object. The syntax of this method is: CreateReport([database[, reporttemplate]]) Both arguments of this method are optional. Here is an example of calling it: Private Sub cmdReport_Click()
Dim rptMaintenance As Report
Set rptMaintenance = CreateReport
End Sub
If you call the method like this, it generates a temporary report named Report1 but it doesn't save it. After the report has been generated, it appears in Microsoft Access. If you want to keep it, you can save it. If fact, if you try closing it, you would be asked whether you want to save it. If yes, you would be asked to give it a name. The first argument of this method is the name of the database that will receive the report. If the report will be added to the current database, you can omit this argument. The second argument is the name of an existing report that you want to use as template. If you specify this argument, you must make sure that you provide the name of a report. Otherwise, you can omit it.
At first glance, the design of a report follows the same approach as that of a form. Both use the same Windows controls either from the Toolbox or from the More Controls button. The techniques of selecting, copying, moving, and resizing controls are the same.
As compared to a form, a report has some custom properties to accomplish its role. For example, while a form can be equipped with one Form Footer and one Form Header sections, a report can have as many sections as you judge necessary. This is because a report uses a characteristic known as grouping. Imagine that you want to print a list of students with selected information such as their names. To better organize the list, you can divide the group by gender. To create a grouping in a report, if you are using the
Report Wizard, after selecting the columns, in the second page of the wizard,
select a column in the left list and click the button that selects one item If you are designing the report, to create a grouping, right-click it and click Sorting and Grouping. This would display the Sorting and Grouping window. Using the combo box under Field/Expression, select a column and, using the corresponding combo box under Sort Order, specify how to sort the records, in ascending or in descending order. After selecting a grouping, set the Group Header property to Yes to create the section on the report. If you also want the new section to close with its own footer bar, set the Group Footer property to Yes. When a report with grouping comes up, it display the title of the grouping followed by all records that follow the rule(s) of the grouping. At the end of those records, it immediately starts with the second list of records that follow the next rule of the grouping. As an alternative, you can start each group of records on its own page. To do this, in the Sorting and Grouping window, after selecting the column, set the Keep Together property to Whole Group.
To perform an operation on a report, you may need to select it first. To do this, in the Reports section of the Database window, you can simply click it, once. To programmatically select a report, you can use the DoCmd object that is equipped with the SelectObject() method. The syntax to use would be: DoCmd.SelectObject acReport, [objectname][, indatabasewindow] The first argument must be acReport to indicate that the object you are selecting is a report. The second argument is the name of the report you want to select. To select but only highlight the report in the Database window, you can pass the third argument as True. If the report is already opened and it is displaying, and if you omit the third argument or pass it as False, the report would be displayed in the foreground. If the report is not opened and you omit the third argument or pass it as False, you would receive an error.
When you start Microsoft Access and open a database, if it has some reports, obviously they would be closed. To use a report, you can open it first. A report can be opened in Design View or in Print Preview. If you (or the user) double-click(s) a report in the Reports section of the Database window, it opens in Print Preview. This views allows the user to review the document before printing. By default, the view may appear blurred to show as much of its area as possible. To be able to read it, you can click the body of the report to zoom. To print it, you can click the Print button on the toolbar to send the document to the printer. A report can also display in Design View. To show it, if the report is currently closed, you can right-click it and click Design View. You can also select it first, then click the Design button under the title bar of the Database window. If the report is already opened, to display it in Design View, as done for the form, you can click the View button on the toolbar. You can also click View -> Design on the main menu.
To programmatically open a report, you can call the OpenReport() method of the DoCmd object. Its syntax is: DoCmd.OpenReport(ReportName, View, FilterName, WhereCondition, WindowMode, OpenArgs) The first argument of this method is the name of the report that you want to open. The second argument is a constant value that can be one of the following:
This third argument, optional, is the name of a query in the database. The fourth argument, also optional, allows you to specify what record would be printed. If you omit this argument, all records of the Record Source value of the report would be printed. If you want to print only one or a few selected records, you can create a WHERE statement and pass it as this argument. The fifth argument specifies how the report should be displayed. It is a constant value that can be acDialog, acHidden, acIcon, or acWindowNormal. This argument is almost never used as it has little to no effect. In most cases, instead of writing the code manually, you can use the Command Button Wizard to select the report to print and how you want the printing to be done.
After using a report, you (or the user) can close it. To close a report, you can click its system Close button To programmatically close a report, you can call the Close() method of the DoCmd object whose syntax is the same we saw for a form. Here is an example: Private Sub cmdCloseStafMembers_Click()
DoCmd.Close acReport, "StaffMembers", acSavePrompt
End Sub
When this code runs, if a report named StaffMembers is opened, it would be closed. If there is no report opened by that name, nothing would happen (Nice!).
As done for tables, part of your job as a database developer consists of maintaining your reports. This include renaming, copying, or deleting the reports. Microsoft Access supports all of the necessary operations. As mentioned for a report, make sure that you need to perform the maintenance operation. If you perform an operation by mistake but have completed it, you cannot reverse it at will. You may have to recreate the object.
You can rename a report if you judge this necessary. As mentioned for a table, you cannot rename a report if it is opened: you would receive an error. To rename a report in the Database window, first click the Reports button that leads to its section. Once in the appropriate section, you can right-click the report and click Rename. This would put the name in edit mode, allowing you to type the new name and press Enter. To programmatically rename a report, you can call the Rename() method of the DoCmd object. The syntax to use is: DoCmd.Rename(NewName, acReport, OldName) The first argument is the name that the new or other report will have. The second argument must be acReport. The third argument is the name of the existing report that you want to rename. The object must exist in the Database window's section as specified by the second argument.
Instead of renaming a report, you can copy it and keep the original. To copy an existing report using the Microsoft Windows Save As routine, in the Reports section of the Database window, you can right-click the report and click Save As... This would open the Save As dialog box that allows you to enter the desired name of the copied report. Alternatively, you can right-click the report, click Copy, then right-click an empty area of the same section of the Database window and click Paste. This would open the Paste Report As dialog box in which you can enter the new name of the copied object. To programmatically copy a report, you can call the CopyObject() method of the DoCmd object using the following syntax: DoCmd.CopyObject [destinationdatabase][, newname], acReport, sourceobjectname] The destinationdatabase argument is the name or path of the database where the copied report would be sent to. If you are copying the report in the same database, you can omit this argument. The newname argument is the name that you want the new report to hold. The third argument must be acReport. The last argument is the name of the existing report.
If you find out that you don't need a particular report anymore, you can delete it from the database. As mentioned already, when in doubt, don't delete it. To visually delete an object, in the Reports section of the Database window, right-click the report and click Delete. You would receive a warning before the report is actually deleted. To programmatically delete a report, you can call the DeleteObject() method of the DoCmd object using the following syntax: DoCmd.DeleteObject acReport, [objectname] The first argument must be acReport. When this method is called, if the report is already selected in the Database window, you can omit the second argument and the selected report would be deleted. Otherwise, to specify the report you want to delete, pass its name as the second argument of the method.
|
|
|
||
| Previous | Copyright © 2005 Yevol | Next |
|
|
||