<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
Saturday, November 15, 2008

Event Trapping Summary On Datasheet

How do we use Event Procedures like LostFocus(), GotFocus() on Datasheet view?


How to display Summation of numeric values on Datasheet view?


For answering both questions, we need a sample Table and a Datasheet Form.


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


  • Order_Details

  • Products



We require the Products table also, because it has a reference in the ProductID field of Order_Detail Table in a Combo Box. We don't want to leave the ProductID field empty on the Order_Detail Table, without the Products Table.


  1. To Create a Datasheet Form, click on the Order_Detail Table, select Form from Insert Menu and select Autoform: Datasheet from the displayed list of options in the Formwizard.


  2. A Datasheet form will be created and will open the Table contents in Datasheet View.

  3. Select Save As… from File Menu and give the name Order Details for the Form. The Datasheet Form will be saved, but it will not be closed.

  4. Select Design View from View Menu to change the Form in Design View mode.


The Form in Design View looks like the image given below or may be different depending on your version of MS-Access.

Datasheet Form in Design View
Doesn't matter how the Fields are arranged on the Form, whether in row format or Column Format the data will be always displayed in Datasheet format. The placement of Fields will not affect the way the data displayed on the Datasheet but the Tab Order does.


Let us try and find out how the Tab Order of Fields influences the Datasheet View.


  1. Change the Form into Datasheet View (View - - > Datasheet View) and check the order of fields appearing in there.

  2. Change the View into Design mode again and re-arrange the ProductID and UnitPrice fields by switching their places.

  3. Change the View back to Datasheet and inspect the data field order.


  4. There will not be any change in the Order of Fields displayed from previous View. If you move the Cursor using Tab Key then it moves in the same order of the fields' placement as you have seen earlier before switching the fields.


  5. Change the Form back to Design View again.

  6. Select Tab Order… from View menu. Click at the left border of the UnitPrice field on the Tab Order Control, click and drag it up and place below the OrderID field.


  7. Tip: You can click on the Auto Order Command Button to re-arrange the Tab Order according to the field placements on the Form.


  8. Open the Form in normal view now and check the change of field placements.


We will add one Unbound Text Box on the Form to calculate the Extended Price after adjusting Discounts of each item.


  1. Open the Form in design view, if you have closed it.

  2. Drag the Form Footer Section down to get more room to place another Text Box below, or you can place it to the right of the Discount Field too.


  3. Create a Text Box and write the formula =(1-[Discount])*[UnitPrice]*[Quantity] in it.


  4. While the Text Box is still in selected state display the Property Sheet (View - -> Properties).


  5. Change the Format Property value to Currency format. Change the Name Property value to Extended Price.


  6. Open the Form in normal view and check the newly added Text control's heading on the top. It will be something like Text10:.


  7. In Datasheet View of Forms MS-Access uses the Caption of the Child Labels attached to the Text Boxes as Field headings. We have changed the Name Property of the Text Box to Extended Price, but that is ignored here.


  8. Now, change the Form into design view and delete the Child Label attached to the Extended Price Text Box.

  9. Change to Datasheet view again and check the field name appearing on the top, it will be Extended Price now.



Datasheet Event Procedure

  1. To try an Event Procedure on the Datasheet view, copy and paste the following VBA Code into the Form's Code Module (View - -> Code to display the Code Module of the Form) and save the Form with the Code.


  2. Private Sub UnitPrice_LostFocus()
    Dim newUnitPrice As Double, msg As String
    Dim button As Integer

    button = vbQuestion + vbYesNo + vbDefaultButton2

    If Me![UnitPrice] <> Me![UnitPrice].OldValue Then
    msg = "Replace UnitPrice: " & Me![UnitPrice].OldValue & vbCr & vbCr
    msg = msg & "with New Value: " & Me![UnitPrice]

    If MsgBox(msg, button, "UnitPrice_LostFocus()") = vbNo Then
    Me![UnitPrice] = Me![UnitPrice].OldValue
    End If
    End If

    End Sub


    We will attempt to trap the change in the UnitPrice field and will ask the User to confirm whether to retain the change or Cancel it.


  3. Open the Form in datasheet view and make some change in the UnitPrice Field and leave the Field by pressing Tab Key or Enter key.


A Message Box will appear asking for permission to retain the change or to cancel it.


Datasheets can be programmed with Event Procedures (Field level or Form level) for validation checks and display of information.


Display of Summary Information

Method-1

We will attempt to answer the second Question we have raised on top of this page.


  1. Open the Order_Details Form in Design View.

  2. Drag the Form Footer Section down to get enough room to place two Text Boxes. Create two Text Boxes in the Form Footer Section.

  3. Write the formula =Sum([Quantity]) in the first Text Box.

  4. Display the Property Sheet of the Text Box and change the Name Property value to TOTALQTY.

  5. Write the formula =Sum((1-[Discount])*[UnitPrice]*[Quantity]) in the second Text Box.

  6. Change the Name Property Value to TOTALVALUE.


When we open the Order_Details Form in Datasheet View it will calculate the Summary Values in TOTALQTY and TOTALVALUE Text Boxes on the Footer of the Form, but we must do something to display it.


The first thought that comes into one's mind is to create a MsgBox and display the results in it on some Event Procedure of the Form. Besides, changes may takes place on the records and they should reflect in the result summary values and we must be able to refresh the change before displaying it again.


We will implement this method before we settle down with a better one.


  1. Copy and paste the following Code into the Form's Code Module and save the Form:


  2. Private Sub Form_DblClick(Cancel As Integer)
    Dim msg As String
    Me.Refresh
    msg = "Total Quantity = " & Me![TOTALQTY] & vbCr & vbCr
    msg = msg & " | Total Value = " & Format(Me![TOTALVALUE], "Currency")

    MsgBox msg
    End Sub


  3. Open the Form in Datasheet View.

  4. Double-Click on the Record Selector at the left border of the Form.


  5. A Message Box pops up with the Summary Values from the Text Boxes in the Form Footer Section.


  6. Make some changes on the Quantity/UnitPrice Fields and try Step-3 again. The change of Value will reflect on the Message Box.

  7. You can filter the Data on ProductID or on OrderID by Right-Clicking on these fields and selecting Filter by Selection or other Options available on the displayed Shortcut Menu and by executing Step-3 to get the Summary of selected records.



Method-2

After trying out the above method your response may be something like "Yah.. it serves the purpose, but it doesn't give the impression of a sophisticated method. After all it takes so many clicks and pop-up Message Boxes". I agree with you too.


With a small change of the above Code we can make the results the way you like it, I hope!.


  1. Open the Form in Design View.

  2. Display the Property Sheet (View - - > Properties).

  3. Select the Mouse Move Property and select Event Procedure from the drop down control.

  4. Click on the build () button at the right side of the Property to open the Form's Code Module.

  5. Cut the Code lines from within the Form_DblClick() Event Procedure:



  6. Private Sub Form_DblClick(Cancel As Integer)

    End Sub


    Leave the above lines alone and paste the Code into the Form_MouseMove() Event Procedure.


  7. Change the line that reads:


  8. MsgBox msg

    to

    Me.Caption = msg

    After the change the Code will look like the following:



    Private Sub Form_DblClick(Cancel As Integer)

    End Sub


    Private Sub Form_MouseMove(button As Integer, Shift As Integer, X As Single, Y As Single)
    Dim msg As String
    Me.Refresh
    msg = "Total Quantity = " & Me![TOTALQTY] & vbCr & vbCr
    msg = msg & " | Total Value = " & Format(Me![TOTALVALUE], "Currency")
    Me.Caption = msg
    End Sub


  9. Open the Form in Datasheet View and move the Mouse over into the data area by crossing the Field Headings or the Record Selectors at the left side.

  10. Check the Title Area of the Datasheet View and you will find the Summary information is displayed there. A sample image is given below:


Summary Info in Datasheet Title Area

Now you can try changing the field values or filtering the data and moving the Mouse over the Field Headings or Record Selectors at the left to get the result on the Title Bar instantly. No Message Boxes or Double-Clicks and what you say about that?


StumbleUpon Toolbar



Source Connect Str Property and ODBC
Access Live Data in Excel-2
MS-Access Live Data in Excel
Database Connection String Properties
Opening Excel Database Directly

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