Photo by sure2talk
I covered Hand Coded WinDev reports in a previous article, now we are going to extend that with custom level breaking and sub-totaling. Don’t worry, even though the report is “Hand-Coded” all of the standard tools are still available, no need to completely hand code just because you have to do some hand coding!!!!
This is based on an example I did for the upcoming wxDevCon. We are preparing an application to be used for non-profits and tracking donations is very important. With many charity events, there are levels of donation that get you greater access etc. This example uses the following Levels:
- Bronze = $0 – $249.99
- Silver = $250 – $499.99
- Gold = Anything over $500
The report will use the level name (Gold, Silver, Bronze) as the break header, print each level on a separate page, and provide a sub-total by level and a grand total. A list of requirements like that used to mean a completely hand coded report, creating all of the level break logic, variables for sub-totaling, custom blocks for sub-totals and headers, and code to control when to print everything. But with WinDev that is not the case. This technique can be used with a standard report or a hand coded report.
The first step is to create a report, the same as any other report, with the Page Header, Body, etc. Again this could be a standard WinDev report or a hand coded one like the last article.
At this point the report should run and print fine, it just lacks any breaks and sub-totaling. Add a new break by right clicking on the Body Block and choosing “New Break”
This will bring up a “List of breaks” window, which at this point will be empty. Press the “Green Plus” button to create a new break, since we will be controlling the break via programming, the Item will be set to “(No Item)”
When returning to the report there is now a Break Header and Break Footer Block.
Add an Item to the Header Block to display the Level.
Set The Break Footer Block to have a Page break, so each level will print on its own page.
To add the sub-total field, you add a new “Calculated Control”
This will take you through a wizard to configure the control. You want the calculation to be on a control of the report.
The type of calculation will be “Sum on Break”. If you take a look at the options here you will notice that WinDev offers quite a few choices.
Link it to the Amount control from the Body Block.
Set the mask appropriately, for this example that would be currency.
And that’s it, the sub-total is completed! Even though our report is hand coded and we are going to be doing some special coding for the level breaks, the sub-total can still be done via the standard WinDev tools!!
Adding a grand total to the End of Document Block is the same steps except the Type of Calculation should be set to “Sum” instead of “Sum on Break”.
All that’s left to do is the coding for the level break. The description tab of the Break Header gives you a hint on how to do programmatic breaks, but it took me some effort to translate this bit of Frenglish into something I could understand.
If you look at the code for the Break Header you will see a Break? event. That is where the code goes. The code should return a value, and anytime that value changes, a level break will be triggered. Since the Break Header needs to be set to Gold, Silver or Bronze, I accomplish both of these with the following code.
This code sets the Item_Level control (that was placed in the Header Body) to the appropriate value based on the donation amount, so the header will display the correct Level. It then returns that value. So as the report runs it starts returning “Bronze” once it reaches the first donation over $249.99 it resets the Item_Level control to “Silver” and returns the new value. That triggers a Level break, the Break Footer Block is automatically printed with the sub-total, a page break is preformed, and a new Break Header is printed with the new tittle of “Silver”, once completed the end of document prints with the grand total and we are done!
A great mix of a little hand coding without losing all of the built-in functionality. Its features like this that make the WX environment so productive. Before WinDev, I hated reports as much as the next developer, but after completing over 25 customized reports in under a week for a project I am now a Report Evangelist.