- In this lesson you will learn the following concepts
- Creating the "Order form" report
- Creating the query
- Creating the report based on a query
- Modifying the "Order form" report
- Displaying the printed report from a menu option
- Creating the context menu
- Associating the context menu with the Table control
- Print test
Lesson 4.5. Printing an order
In this lesson you will learn the following concepts
- Creating a report based on a query.
- Printing a report based on a query with parameters.
Estimated time: 30 mn
We will now give the user the possibility to print the details of the requested order. The report can be directly printed via the context menu of the Table control.
- Open the project you used in the previous lesson if necessary.
- Go to the WINDEV home page (Ctrl + <).
- On the home page click "Tutorial", then in "Part 4 - Full application with data", double-click "Full application (Exercise)".
- A dialogue box prompts you to open the project you worked on in the previous lesson. You can open the local copy or the original project. Select "Open the local copy".
|A corrected project is available. This project contains the different reports created in this lesson. To open the corrected project, go to the home page and click "Tutorial", then in "Part 4 - Full application with data", double-click "Full application (Answers)".|
Creating the "Order form" report
First of all, we are going to list the information that will be displayed in the report:
- The order characteristics: date and order number.
- The personal customer details: name, address, zip code, city and country.
- The characteristics of order lines:
- Ordered quantity,
- Product reference,
- Product caption,
- Total BT,
- Total IOT.
To easily create this report, the data to print will be grouped in a query. This query can be used by the report or by any other element of the WINDEV project (Table control, Looper control, etc.).
|WINDEV proposes to create reports from several data sources: data files, queries, controls, text files, etc.|
In most cases, we advise you to group the data to print via a query and to create a report based on this query. To add information to the report, simply add the corresponding item to the query.
The reports based on data files must be simple reports, which means reports used to display data coming from a single data file.
Creating the query
- We will use the query editor to create the base query of the report.
- Click among the quick access buttons. The element creation window appears: click "Query". The query creation wizard starts.
- Select the "Select" option.
Indeed, this query will be used to select the records that will be printed in the report. Go to the next step.
- The query description window appears. To build the query, we are going to select the elements that will be displayed in the result.
- Double-click the items in the analysis, to the left of the description window. The items taken into account are displayed in the middle of the screen.
We want to print in the report:
- information regarding the customer. In the "Customer" data file, double-click the FullName, Address, ZipCode, City, and Country items.
- information regarding the order. In the "Order" data file, double-click the OrderID and Date items.
- information regarding the product. In the "Product" data file, double-click the Reference, Caption, and PriceBT items.
- information regarding the order line. In the "OrderLine" data file, double-click the Quantity and TotalBT items.
- information regarding the grand total of order. In the "Order" data file, double-click the TotalBT and TotalIOT items.
|The order in which the items are inserted into the query is important. Indeed, this order corresponds to the order used to display the data in the report. The creation of the corresponding report will be simplified if this order is properly defined.|
The query description window is as follows:
At this time, this query is used to select all orders and the corresponding order lines.
We want to select the data corresponding to a single order whose identifier is known. Therefore, we are going to define the order number in parameter.
- To manage the "Order identifier" parameter:
- Select the Order.OrderID item (in the middle of the screen).
- Expand "Selection condition" and select "New condition".
- In the window that is displayed, we are going to specify that the selection condition corresponds to a parameter: Perform the following operations:
- Select "Is equal to".
- Check "the parameter".
- The name of the parameter is automatically proposed: "ParamOrderID".
- Validate the condition description window. The number "1" appears to the right of the Order.OrderID item, indicating that a selection condition was defined.
- Give a name to the query: type "QRY_OrderForm" instead of "QRY_NoName1" in the "Query name" area:
- Validate the query description window ("OK" button).
- The save window is displayed. Validate the proposed information.
- The graphic query representation is displayed:
Creating the report based on a query
- To create a report:
- Click among the quick access buttons.
- The element creation window appears: click "Report" then "Report". The report creation wizard starts.
- The report creation wizard proposes several types of reports:
- Select "Table". Go to the next step.
- Select the data source of report. The report will be based on the query that was just created. Select "From a data file or existing query".
Go to the next step.
- In the list of data files and queries, select the "QRY_OrderForm" query. Go to the next step.
- The wizard asks you to specify whether a break is required. No break will be used in this report. This concept will be presented later in this tutorial. Answer "No". Go to the next step.
- You are going to specify the order in which the items will be printed and how they will be distributed in the different blocks. In the wizard, the items are listed according to the order defined in the query:
- The items regarding the customer will be displayed in the "Page header" block. Indeed, this information must not be repeated on each order line.
For the FullName, Address, ZipCode, City and Country items, click the line corresponding to the item. In the "Block" column, expand the combo box and select "Page header".
- The items regarding the order will also be displayed in the "Page header" block. Indeed, this information must not be repeated on each order line. For the OrderID and Date items, click the line corresponding to the item. In the "Block" column, expand the combo box and select "Page header".
- Furthermore, we are going to modify the caption of "OrderID" item.
- Select the line containing the OrderID item.
- Click the Caption column and type "Order #".
- The items regarding the order lines will be displayed in the report body. These items will be displayed for all the order lines.
- The items regarding the order totals will be displayed in the page footer. Indeed, this information must not be repeated on each order line. For the TotalBT_Or and TotalIOT items, click the line corresponding to the item. In the "Block" column, expand the combo box and select "Page footer".
The following table presents the different assignments of items in the order presented in the wizard:
- Go to the next step.
- The wizard proposes to create a counter, a sum or an average on the numeric items found in the report. In this report, the calculations are performed by the query. Click the "No calculation" button. Go to the next step.
- This step is used to define the report layout.
We will keep the default values with the "Portrait" orientation.
When choosing the print margins, don't forget to take into account the physical margins of printers. The physical margins of printers are margins where no print is allowed. Furthermore, the physical margins differ according to the type of printer.
- Go to the next step.
- This step allows you to select the skin template used for the report. We recommend that you use the same skin template as the one used for the windows. In our case, select the "Phoenix" skin template for example and go to the next step.
- All we have to do now is give a name and a title to the report.
- Type the title : "Order form".
- The "RPT_Order_form" name is automatically proposed.
|The option "Report modifiable in Reports and Queries" is not available in the Express version.|
- Validate ("Finish").
- The report being too large to be printed in Portrait mode, the report editor proposes to use the landscape mode, to reduce the table or to print the table on several pages. In our case, accept the landscape mode.
- Accept to reduce the table if necessary.
- The window for saving the report is displayed. Validate the save information.
- The report is displayed in edit in the report editor:
- The different order lines are grouped in a table.
- Run this report by clicking among the quick access buttons.
- The report editor asks for the print destination. The print destination can be: Select "Report viewer" and validate.
- The report editor asks for the parameters of query used by the report. Don't forget that a parameter was used to specify the number of the order to print. For the example, type the test value "1". Validate.
- The report is displayed as requested in the report viewer.
- Print the current page or the entire document by clicking the printer.
- Create a duplicate copy.
- Select a different zoom value.
- Save the report as a Word document (in RTF format).
- Save the report in HTML format.
- Save the report in PDF format.
- Save the report in XML format.
- Create an email with the report in HTML in the message body.
- Create an email with the report in PDF as attachment.
- Annotate in the document.
- Perform a search in a document.
- Add watermarks.
Modifying the "Order form" report
We are going to perform modifications regarding the layout in the report that was just created.
- Delete the number of pages displayed in the report:
- Select the [PAGENUM]/[NBPAGES] control.
- Press the Del key on the keyboard.
- We want to position the information regarding the customer and the order in the page header:
- Delete the "Order form" caption found in page header.
- Delete the text in front of the customer information (Full name, etc.).
- Position the item containing the city beside the zip code.
- Select the customer details and move them (with the mouse) to the right of report.
- Move the order number and the order date up (to the top of "Page header" block).
- We are going to position the totals properly in the page footer:
- Select the controls (captions and items) corresponding to the totals found in the page footer.
- Move these controls to the bottom right corner of the table.
- Modify the print options of "Page footer" block:
- Open the description window of the "Page footer" block:
- Click the "Page footer" block.
- Right-click to open the context menu and select "Block description".
- In the "General" tab, check the following options:
- Print the "End of document" block AFTER the "Page footer" block on the last page.
- The "Page footer" block is not printed at the bottom of the page, it is stuck to the previous blocks.
- Validate the description window.
- The report is displayed in the report editor:
- Save the report by clicking among the quick access buttons.
Our report is created.
Displaying the printed report from a menu option
In our application, the "RPT_Order_form" report will be printed from an option found in the popup menu of the Table control used to list the requested orders.
As already seen, the Table control proposes an automatic menu. We want to keep the options of this automatic menu and add an option that will be used to print the selected order form.
The principle is as follows:
- We are going to create a new context menu. This menu will contain the WLanguage code used to print the report.
- We are going to link this popup menu to the Table control and specify that the default popup menu will be displayed just after the popup menu defined for the Table control.
Creating the context menu
- To create a context menu in the "WIN_Menu" window:
- Open the "WIN_Menu" window in the editor if necessary (double-click its name in the "Project explorer" pane for example).
- On the "Window" pane, in the "Bars and menus" group, expand "Popup menus" and select "New popup menu".
- A new context menu appears in the editor. This menu option includes a single option named "Menu option".
- Open the context menu description:
- Select "Menu option".
- Right-click to open the context menu.
- Select "Popup menu description".
- Give a name to the context menu ("MENU_Order") and validate.
|Context menus and windows|
A context menu is associated with a window. If a window includes multiple context menus (window, control, etc.), they are saved with the window.
- We are now going to define the option caption and write its WLanguage code.
- Select "Menu option".
- Press the Space key on the keyboard: the caption becomes editable. Type the new caption ("Print the order form") and validate.
- Display the WLanguage events associated with the option:
- Select the option.
- Right-click to open the context menu and select "Code".
- Write the following WLanguage code:
In this code:
// Print in the report viewer
// Initializes the report query
// Starts printing the report
- iDestination is used to specify that the report will be printed in the report viewer.
- <Report>.InitQuery is used to specify the parameters expected by the query associated with the report. In our case, the query expects the order number in parameter. This order number is in the COL_OrderID column of the TABLE_QRY_FindOrders Table control for the current row.
- <Report>.Print is used to print the specified report (the RPT_Order_form report in our case).
- Close the code window and the window containing the context menu.
- Save the "WIN_Menu" window.
Associating the context menu with the Table control
- Last step: we are going to link the context menu to the Table control.
- In the "WIN_Menu" window, display the "Finding orders" tab.
- Open the Table control description ("Table description" in the context menu of the control).
- In the "UI" tab, expand the "Popup menu" combo box.
- Check "Add a popup menu", select the "MENU_Order" menu and select "At the beginning" to specify that the menu is placed before the system menu.
- Click inside the window to validate the context menu.
- Now, all we have to do is run a real test:
- Run the test of "WIN_Menu" window.
- Select the "Finding orders" tab pane.
- Specify the criteria and start a search.
- Select one of the orders displayed in the Table control.
- Print the order via the popup menu.
- Close the report viewer and the test window.