<body><script type="text/javascript"> function setAttributeOnload(object, attribute, val) { if(window.addEventListener) { window.addEventListener("load", function(){ object[attribute] = val; }, false); } else { window.attachEvent('onload', function(){ object[attribute] = val; }); } } </script> <iframe src="http://www.blogger.com/navbar.g?targetBlogID=34083602&amp;blogName=LEARN+MS-ACCESS+TIPS+AND+TRICKS&amp;publishMode=PUBLISH_MODE_FTP&amp;navbarType=BLUE&amp;layoutType=CLASSIC&amp;searchRoot=http%3A%2F%2Fblogsearch.google.com%2F&amp;blogLocale=en_US&amp;homepageUrl=http%3A%2F%2Fwww.msaccesstips.com%2F" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" height="30px" width="100%" id="navbar-iframe" allowtransparency="true" title="Blogger Navigation and Search"></iframe> <div></div>
www.msaccesstips.com

LEARN MS-ACCESS TIPS AND TRICKS


International Response Fund

LEARN MS-ACCESS TIPS AND TRICKS

↑ Grab this Headline Animator

Your Ad Here
Friday, July 24, 2009

Msaccess Report and Page Totals

Ms-Access Functions Sum, Count, Avg etc. cannot be used in Page Header or Footer Areas of Reports for creating Page-wise Summary Values. These will work in Report Group Header/Footer or in Report Header/Footer areas only.


That doesn't mean that you cannot do any calculations on Page Header/Footer Areas. You can write expressions to print useful information without using the above categories of functions.


For Example: When you design Reports with built-in Report Wizard you have seen MS-Access uses expressions in the Page Footer Area to display Date/Time and Page Numbers.


The Report Wizard uses the Function =Now() in a Text Box to print Date and Time and the Expression ="Page " & [Page] & " of " & [Pages] to display Page Number information in the Page Footer area.


Even though we cannot use this method to calculate and print Page-wise Total Values, we can use it to print the Running Sum values on every page in Page Header and Page Footer Areas.


We don't have to struggle with any VBA Code for this.


By going through the contents of this Website one gets the general impression that if you want to do something in MS-Access then you must know and use VBA Code. This is not true. You can do plenty of task automation with Macros. The first Ms-Access Application that I have developed in 1997 for use in our Department is without a single line of VBA Code and it is still in use. The only change that I have made on that Application is to convert it from MS-Access Version 2 to MS-Access97, later to MS-Access2000 and modified the Main Switch Board Form to give it a better look.


You can get tons of materials from Internet on basic usage of MS-Access (or that was the general impression that I had when I started this Website) and quickly attain the basic knowledge on Table Design, Relationships Design, Queries, Forms, Reports and Macros. Once you are through with the basics and on the look out for something different, advanced, interesting and need more exciting experience with MS-Access usage then this Website is primarily targeting Readers like you.


The majority of the crowd may be on the other side now but I am sure they will be on the look out for something better all the time. That is why VBA Code is the main driving force behind all the examples given on this Site.



So, let us try out part of this experiment without VBA Code. We need a simple Report to do that.


  1. Import the following Tables from Northwind.mdb sample database (C:\Program Files\Microsoft Office\Office11\Samples\Northwind.mdb):


    • Order Details

    • Products


  2. Open a new Query in Design View, do not select a Table or Query from the displayed list and click Close.

  3. Select SQL View from View Menu to display SQL Editing Window of the Query.

  4. Copy and Paste the following SQL String and save the Query with the name Products_ListQ:



  5. SELECT [Order Details].OrderID, Products.ProductName, [Order Details].Quantity
    FROM [Order Details] INNER JOIN Products ON [Order Details].ProductID = Products.ProductID
    WHERE ((([Order Details].OrderID) Between 10248 And 10300));


    We have selected only few records from the Order Details Table; from OrderID numbers between 10248 and 10300; so that the sample Report will have only about five or six pages in Portrait Mode.

  6. Click on the Product_ListQ Query and select Report from Insert Menu.

  7. Select Auto Report: Tabular from the displayed list and click OK to create the Report.

  8. Save the Report with the name Products_List.

  9. Open the Report in Design View and change the design to look like the sample image given below.


  10. Sample Report in Design View


  11. Add the Red colored Text Boxes and Labels to the Design of the Report as explained below:


    • Make a copy of the Quantity field in the Detail Section and position it to the right as shown.

    • Display the Property Sheet (View- ->Properties) of the copied control and change the Name Property value to QtyRunningSum.

    • Change the Running Sum Property Value to Over All.

    • Create a Text Box on the Page Header of the Report and change the Name property value of the Text Box to BF (stands for Brought Forward), the Border Color Property Value to 128 and the Font Weight Property Value to Bold.

    • Change the Caption value of the Child Label to B/F: .

    • Create a Text Box on the Page Footer of the Report, change the Name Property Value to PageTotal, Border Color Property Value to 128 and the Font Weight Property Value to Bold.

    • Change the Caption of the attached Child Label to Page Total: as shown on the sample design above.

    • Create another Text Box to the right, change the Name Property Value to CF (stands for Carry Forward), Border Color Property Value to 128 and the Font Weight Property Value to Bold.

    • Change the Name Property Value of the child label to lblCF and the Caption property value to C/F: .



    We have changed this Label's Name property value to lblCF so that we can address it in Program to change the Caption value to TOTAL at the last Page of the Report. At that point it is not appropriate to show the label as C/F: (Carry Forward). We will do this with the Program while creating Page Totals on the Report.


    Before that we are trying to display values into the other Text Boxes we have created for Running Sum, BF and CF values without VBA Program.


    At a later stage we will hide the Quantity field copied for calculating Running Sum but we will keep it visible now and use it for calculating Values on the other two controls BF and CF on the Page Header and Page Footer Sections of the Report respectively.


  12. Save the Report.


  13. Open the Report in Print Preview and check how the Values are appearing in the copied Quantity Field with the Running Sum Property Value set to Over All.


  14. Each Quantity value is summing up in this control and the last line has the total so far value on the Page. If we put a reference to the QtyRunningSum Text Box in the CF Text Box in the Page Footer area we can transfer this Value into that Control showing the cumulative total value at the end of each page.


  15. Open the Report in design view and write the following expression in the Control Source Property of the CF Text Box:


  16. =[QtyRunningSum]

  17. Now, preview the Report again and check whether the last total value is appearing in the Page Footer area in CF Text Box or not. Advance to the next page and check the value appearing there too.


  18. Next step is to create the Page Header Control BF Value. If you are smart then you might have already made the change in the BF control by now without reading further down.


    You have two choices to put the Value in this Control but both have a little problem to take care of to get it right.


    The first choice is to put a reference to the CF Text Box control in the BF Text Box like =[CF] to bring the Total Value from the previous page to the current Page Header.


    The second one is to put a reference to the QtyRunningSum Text Box, like =[QtyRunningSum] . This is also logically correct assuming that the BF control will take the total cumulative value from the previous page because the QtyRunningSum control in the Detail Section on the current page comes after the Page Header Section Format and Print Events.


    Read the following Articles where we have discussed the Format and Print Events of the Reports and to learn few other Tricks we have tried earlier:


    Hiding Report Lines Conditionally
    Hiding Report Lines Conditionally-2
    Hiding Report Lines Conditionally-3
    Detail and Summary from same Report

  19. Choose one of the two options I have presented above and change the Control Source Property Value with either the expression =[CF] or with =[QtyRunningSum] . If you ask me I prefer the first one, because we are going to delete the QtyRunningSum Text Box when we use Program for Page Total.

  20. Open the Report in Print Preview and check the Page Header Section BF Text Box Value.


  21. In both cases you will get the same result, the starting running sum value of the first line on the Detail Section on the current page. In other words the Value in the BF control is equal to the cumulative Value of the previous page plus the Quantity Value in the first line of the current page. This is because even after the Page Header/Footer control values undergoes the Format and Print Events; the controls CF & BF internally keeps on populating with changed Running Sum values. If this was not the case we could have easily calculated the Page Total value by finding the difference between CF & BF control values on the same page.


    This is the reason why we have to depend on VBA Code to solve the Page Total problem.


    So, if we subtract the first line Quantity value from the first line running sum QtyRunningSum or previous Page CF Text Box Value we will get the BF Text Box value correctly.


  22. Change the expression in the BF Text Box in the Page Header Section as follows:



    • =[CF] – [Quantity]

    • Or
    • =[QtyRunningSum] – [Quantity]



    We don't need the QtyRunningSum display now and we will hide it from the Detail Section.


  23. Click on the QtyRunningSum Text Box, display the property sheet and change the Visible property value to No.


  24. Save the Report and open it in Print Preview.



Now, the Page Header Text Box BF and Page Footer Text Box CF Values are showing correctly. Move to the next page and check the values appearing there.


We will make a copy of this Report for our Page Total example. We will use VBA Code to calculate and update Page Total and CF values on the new Report.



  1. Create a copy of the Product_List Report and name it as Product_List2.

  2. Open the Report in Design View.

  3. Modify the expression in the Control Source Property of BF Text Box in the Page Header as =[CF].

    We will calculate and update only the CF Text Box Value through Program at the Page Footer Section.


    Since, we are not depending on the Running Sum value we don't have to worry about the side effect we have experienced in the earlier method. The correct value will be picked from the previous page control CF Text Box.


  4. Delete the QtyRunningSum Text Box from Detail Section.

  5. Select Code from View Menu to display the Class Module of the Report.

  6. Copy and paste the following Code into the Class Module, Save and Close the Report.



  7. Option Compare Database

    Dim x_pagetotal As Long, x_CF As Long

    Private Sub Detail_Print(Cancel As Integer, PrintCount As Integer)
    If PrintCount = 1 Then
    x_pagetotal = x_pagetotal + [Quantity]
    If Retreat = False Then
    x_CF = x_CF + [Quantity]
    End If
    End If
    End Sub


    Private Sub PageFooterSection_Print(Cancel As Integer, PrintCount As Integer)
    If PrintCount = 1 Then
    [PageTotal] = x_pagetotal
    x_pagetotal = 0
    [CF] = x_CF
    End If
    If [Page] = [Pages] Then
    Report.Controls("lblCF").Caption = "TOTAL:"
    End If
    End Sub


    We have declared two Variables x_pagetotal and x_CF at the global area of the Module. At the Detail Section Print Event Procedure we are adding up the Quantity Values in both Variables. At the Page Footer Print Event Procedure we are updating the PageTotal, CF Text Box values and resetting x_pagetotal Variable to zero. Here, we are checking whether the current Page Number is equal to the Last Page Number, if so then changes the lblCF Label Caption to TOTAL: .


  8. Open the Report in Print Preview and check the Page Total, BF and CF Text Box Values. Move to the next page and check the control values there too.



The Report image with cut Sections of Page Header/Footer areas showing the Control Totals on page number three is given below.


Report Preview with Page Totals


NB: Here, I would like to remind you about the other serious side effect in this method which I have already explained earlier. Since, the Program is depending on the Page by Page calculations to arrive at the correct Control Totals on each page, if you jump from one page to a distant page and skip one or more pages in between, by typing the Page Number in the Page Number Control below, then these control values will not be correct.


If the Report is printed directly to the Printer, without Previewing and moving between pages as explained above, it will print with correct values on each page.




StumbleUpon Toolbar



MS-Access and Reference Library
InputBox and Simple Menus
Change Form Modes On User Profile
Positioning POPUP Forms
Synchronized Floating Popup Form

Labels:

0 Comments:

Post a Comment

Note:Comments subject to Review by Blog Author before displaying.

Links to this post:

Create a Link

<< Home


Creative Commons License
Learn MS-Access Tips and Tricks by msaccesstips.com is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 2.5 India License.



This Page is best viewed with 1280 x 1024 Resolution

   FEATURED LINKS
SITEMAP
Command Button Animation
3D Headings on Forms
MsgBox & Office Assistant
Reminder Ticker
MS-Access & E-Mails
Automated E-Mail Alerts
MsgBox with Options Menu
Colorful Command Buttons
Configure Lotus Notes
Alerts through Network
Running this site has become a costly affair as the revenue from Ads is not sufficient to support it. If you find these pages informative & useful and would like to extend a helping hand, then please do it here.





Link Back to us with this Button

Learn MS-Access

Copy and Paste this HTML Code in your Webpage


Add to Technorati Favorites

Programming Blogs - Blog Catalog Blog Directory
Powered by FeedBurner
Add to Google

Software
Computers blogs
TopOfBlogs




AddMe - Search Engine Optimization Submit Your Site Free!
Go BlogZ Ave Blogs
eBlogzilla Changing LINKS
LS Blogs Blogarama
blog search directory BlogUniverse
Find Blogs in Directory RSS Directory
blogskinny.com ShowcaseBlogs.com
Amfibi

Search Engine Optimization and SEO Tools
Dmegs Web Directory Takeaway for Sale Businesses For Sale
Free Submission Directory Free site submission

Free Listing
 





Free Page Rank Checker

AddThis Social Bookmark Button

Enter your email address:

Delivered by FeedBurner



Top Blogs


Microsoft Access is the Jewell among MS-Office suite of Applications. Its Security features are excellent and works fine in Network environment. MS-Access can link/upload data from any Data Source. Applications that you design should be user-friendly and visually pleasing too. Here I would like to share my experience in Microsoft Access Programming with you and I am sure that you will find them interesting too.

My Photo
Name: Ramachandran Pillai
Location: Cochin, India

I am not an Access Guru and not through MS-Access yet. More to learn and I don’t think that aspect has any end because others have their own style of using this tool. We can learn lot more tricks, other than what we already know, from others too. My programming skills in COBOL, BASIC, Turbo-C, dBase, FoxPro, Visual Basic & Basic HTML attained through self-learning. I wrote my first COBOL Program in 1975 for ICL1901, 3rd Generation Main Frame Computer. Worked as a Computer Operator (NCR VRX8555 Mainframe Machine upto 1990) with M/s. Y.B.A. Kanoo, Saudi Arabia. Started using MS-Access Ver.2 in 1996, when dBase III+ and Foxbase (later version Foxpro) were my favorite DBMS. During Last 13 Year period I have developed more than 45 In-House Applications (medium & small) under MS-Access for our Organization, a leading Automotive Company in Oman. All the Applications are fully Secured and runs under Windows Network. It is my pleasure to share my experience with others. Anything interesting that you would like to share with me, please do. My E-mail Address: aprpillai@msaccesstips.com


If you need a Demo of any of the Topic explained here, send me an E-mail to: aprpillai@msaccesstips.com
with the Topic Description, I shall try to send a sample database to you.


Access Tips | Email | Reports | Report Tricks | Graphs | Forms | Menus | Animation | Security | Internet | How TOs | Linking | Query | Progress Meter | Alerts | Process Tips | Access Functions |




Site Designed by:www.msaccesstips.com