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.
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.
- 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.
A Datasheet form will be created and will open the Table contents in Datasheet View.
- 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.
- 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.
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.
- Change the Form into Datasheet View (View – – > Datasheet View) and check the order of fields appearing in there.
- Change the View into Design mode again and re-arrange the ProductID and UnitPrice fields by switching their places.
- Change the View back to Datasheet and inspect the data field order.
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.
- Change the Form back to Design View again.
- 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.
Tip: You can click on the Auto Order Command Button to re-arrange the Tab Order according to the field placements on the Form.
- 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.
- Open the Form in design view, if you have closed it.
- 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.
- Create a Text Box and write the formula =(1-[Discount])*[UnitPrice]*[Quantity] in it.
- While the Text Box is still in selected state display the Property Sheet (View – -> Properties).
- Change the Format Property value to Currency format. Change the Name Property value to Extended Price.
- Open the Form in normal view and check the newly added Text control’s heading on the top. It will be something like Text10:.
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.
- Now, change the Form into design view and delete the Child Label attached to the Extended Price Text Box.
- Change to Datasheet view again and check the field name appearing on the top, it will be Extended Price now.
Datasheet Event Procedure
- 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.
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.
- 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
We will attempt to answer the second Question we have raised on top of this page.
- Open the Order_Details Form in Design View.
- Drag the Form Footer Section down to get enough room to place two Text Boxes. Create two Text Boxes in the Form Footer Section.
- Write the formula =Sum([Quantity]) in the first Text Box.
- Display the Property Sheet of the Text Box and change the Name Property value to TOTALQTY.
- Write the formula =Sum((1-[Discount])*[UnitPrice]*[Quantity]) in the second Text Box.
- Change the Name Property Value to TOTALVALUE.
When we open the Order_Details Form in Datasheet View it will calculate the Summary V
alues 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.
- Copy and paste the following Code into the Form’s Code Module and save the Form:
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
- Open the Form in Datasheet View.
- Double-Click on the Record Selector at the left border of the Form.
A Message Box pops up with the Summary Values from the Text Boxes in the Form Footer Section.
- Make some changes on the Quantity/UnitPrice Fields and try Step-3 again. The change of Value will reflect on the Message Box.
- 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.
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!.
- Open the Form in Design View.
- Display the Property Sheet (View – – > Properties).
- Select the Mouse Move Property and select Event Procedure from the drop down control.
- Click on the build (. . .) button at the right side of the Property to open the Form's Code Module.
- Cut the Code lines from within the Form_DblClick() Event Procedure:
Private Sub Form_DblClick(Cancel As Integer) End Sub
Leave the above lines alone and paste the Code into the Form_MouseMove() Event Procedure.
- Change the line that reads:
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
- 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.
- Check the Title Area of the Datasheet View and you will find the Summary information is displayed there. A sample image is given below:
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?