Create an ETB application in MS Access - Part 4 1.1 Introduction This series of short tutorials is designed to cover some of the issues that you might come across when creating a small database application. The intention is not to create a database that is the last word in elegance and sophistication, but rather to demonstrate some useful techniques and procedures as part of a usable application. If you have never set up a database before, you will hopefully find the tutorials interesting, but we haven’t included complete explanations or step by step guidance. There are other resources on AccountingWEB that will guide you through these areas in greater detail. In the first instalment we dealt mainly with setting up the main tables for our database, and in the second with the creation of our main form. In the third we added more controls to our main form and set up some of the other forms for our database. In the final instalment we will create more forms and reports to work with the information in our application. 1.2 Trial balance One of the key forms in the application is the trial balance form – frmTB. This displays (as its name suggests) a trial balance. This is not as simple as it appears. Because of the concept of double entry bookkeeping, each transaction in the transaction table has to be included twice in the query that calculates the trial balance totals – once for its source and once for its analysis, with the value being added in as a positive for one and a negative for the other. This is achieved by a series of queries that build up to the trial balance total query. 1.2.1 QryTransactionsFull This query includes the transactions table and the table ‘tblDrCr’. The latter table includes only one field - ‘Type’. This table contains just two records, one with the value 1 and the other with the value –1. The most important aspect of the query is that the two tables are not linked. The effect of including fields from unlinked tables in query is to repeat each record in the first table for every record in the second table. We have it on good authority that this is called a cartesian query, presumably because the number of output records increases geometrically. Obviously it needs to be used with care if incredibly long and time consuming queries are to be avoided. For example if table one contained 5,000 records and table two contained 2,000 records, there would be 10,000,000 output lines! In our case tblDrCr has only two records, therefore the query will output twice as many records as there are in tblTransaction. We have included the following fields as the outputs: TransactionID TransactionDate TransactionNumber TransactionDescription Type Value Code From TransactionType Ref Let’s look at the three fields that are entered as expressions, rather than coming straight from our tables: 1.2.1.1 Value The value field is multiplied by the Type from tblDrCr, so for the two records generated by our Cartesian query for each record in tblTransaction one will be multiplied by 1 and the other by –1, hence the debit and credit. Value: [transactionvalue]*[Type] 1.2.1.2 Code and From We need to create two nominal codes for each ‘half’ of our transaction. These will effectively be a mirror image of each other. Each field uses the ‘IIF’ function to return the field TransactionAnalysisCode or Transaction analysisSource: Code: IIf([Type]=-1,[TransactionAnalysisCode],[TransactionAnalysisSource]) From: IIf([Type]=-1,[TransactionAnalysisSource],[TransactionAnalysisCode]) 1.2.1.3 Summary By including each transaction twice in this way we can ensure that the single value in each record in tblTransactions is included both as a debit and a credit and with a source and analysis code, thus ensuring a balancing trial balance. If we were to display the results of our query we will see two lines for each record in tblTransactions. Each pair of results shows the value as a positive and a negative, with the source and analysis nominal codes also ‘mirrored’: 2 1.2.2 qryDetailedNominal This query takes the results of qryTransactionsFull and links the ‘from’ field to tblNominalCodes and the ‘code’ field to a second instance of the same table in order that each part of the transaction includes the nominal code description, or any other field we need from the table tblNominalCode. We can include two instances of the same table simply by selecting it twice from the ‘Show table’ dialog. The first instance just has its own normal table name, other instances have the suffice _1, _2 etc. added so that you can distinguish which table you want the fields to come from. The output fields are similar to qryTransactionsFull, but we have added a NominalDescription, an AnalysisCode and AnalysisDescription field. The NominalDescription comes from the tblNominalCode that is linked to our ‘Code’ field in qryTransactionsFull, and the AnalysisCode and AnalysisDescription fields come from the tblNominalCode_1 that is linked via the ‘From’ field: AnalysisCode: NominalCode AnalysisDescription: NominalDescription Running this query gives us full details of both ‘sides’ of our transactions: 3 1.2.3 qryTrialBalanceTotals All that now remains to be done is to summarise the list of transactions by nominal code in order to produce a trial balance. This is achieved by grouping the transactions in a Totals query. The query uses the following fields from the previous query: qryDetailedNominal: NominalID NominalCode NominalDescription Value Clicking on the totals button on the toolbar displays the total row for the query. Using this totals row, we ‘Group by’ NominalID, and for NominalCode and NominalDescription we use ‘First’ as these fields will obiously have the same value for each NominalID. Finally we ‘Sum’ the value. We also add a ‘<>0’ criteria to the value field to suppress any accounts with zero balances. Running our query displays a list of nominal IDs, codes, descriptions and values: 1.2.4 qryTrialBalance Finally, we could take our qryTrialBalanceTotals fields and use them as the basis for another query that splits the value into a Debit or Credit column depending on whether it is positive or negative. 4 We just use the first three fields as normal. Note that NominalCode and NominalDescription become ‘FirstOfNominalCode’ and ‘FirstOfNominalDescription’ as the output of the previous Totals query. Instead of a single ‘Value’ field, we have added a ‘Debit’ and a ‘Credit’ field using the IIF statement again: Debit: IIf([SumofValue]>=0,[Sumofvalue],0) Credit: IIf([SumofValue]<0,[Sumofvalue],0) We also choose to sort on the ‘FirstOfNominalCode’ field in Ascending order. Now our query will generate our nominal code list in the traditional two column format: 1.2.5 FrmTB 1.2.5.1 Introduction Now let’s do something useful with our query – we’ll use it as the basis for a form that displays the trial balance and lets us ‘drill down’ to the detailed transactions that make up each nominal balance. Using the form wizard based on qryTrialBalance, we’ll create a form that includes the ‘FirstOfNominalCode’, ‘FirstOfNominalDescription’ and the Debit and Credit columns. 1.2.5.2 Form header We will need to do a bit of work on the form. First we need to tidy up the headings and add a ‘Trial Balance’ title in the form header area. 1.2.5.3 Form footer We will also add some text boxes containing totals in the form footer area. These should be the same width as the Debit and Credit fields and positioned beneath them. The formulae will be: =Sum([Debit]) =Sum([Credit]) Having created these totals fields we need to set the formatting for both the individual nominal totals and the overall totals. Select both the Debit and Sum([Debit]) fields and set the format as follows: [Blue]#,##0.00;[Red]-#,##0.00;[Blue]5 Now do the same with the Credit fields, but set the format for these to: [Blue]-#,##0.00;[Red]#,##0.00;[Red]The number formats in Access are very similar to those in Excel with three main sections separated by semi-colons. We have included a negative format in the debit column, and a positive format in the credit column although we should, of course, only end up with debits in the debit column and credits in the credit column. It’s a good idea to make the unexpected obvious in case we have made a mistake elsewhere. Also, the credit column formats credits without a minus as we know that everything in that column should be a negative anyway. 1.2.5.4 Detail All we need to do in the detail area is to drag the left hand edge of the Nominal Code field and label to allow us room to enter a small button to the left hand side of the details area which we will later use to drill down to the nominal details. 1.2.5.5 Conclusion Finally, we will select all out text fields and change the Data properties to Locked: “Yes” as the fields are not editable. We have chosen to leave the fields enabled, as this lets the user click in them to search for a particular nominal code for example. We could also change the Special Effect to flat to make the fields look less ‘editable’. When we display our form we should see a trial balance with totals for the two columns (hopefully the same totals!). The debit column figures should be blue and the credit column red. 6 Now we close and save our trial balance form for the moment. Change its name to frmTrialBalance. 1.2.6 Nominal details form Using similar techniques to those we used to create our trial balance form we will create a form that displays all the transactions for a particular nominal code. We will use the following fields from qryDetailedNominal: TransactionID TransactionDate TransactionNumber TransactionDescription AnalysisCode AnalysisDescription Value We need to do a bit more work on this form. First we haven’t got the debit and credit fields that we had for our trial balance, so we will need to replace our value field with two new fields that use IIF statements to put positive values in the debit column and negative values in the credit column, Name these fields as txtDebit and txtCredit respectively. =IIf([Value]>=0,[Value],0) =IIf([Value]<0,[Value],0) We will add totals fields in the form footer as we did for the trial balance form. Call them txtDebitTotal and txtCreditTotal: =Sum(IIf([Value]>=0,[Value],0)) =Sum(IIf([Value]<0,[Value],0)) To the right of the credit total, add a field to hold the overall total. This will contain the formula: =[txtDebitTotal]+[txtCreditTotal] Format the debits and the grand total using the format string we used for the trial balance debits and use the credit format string for the credits. As we did for the trial balance fields, select them all and set the ‘Locked’ property to ‘Yes’ and the special effect to flat. You will also need to adjust the width and placement of your fields and heading labels to ensure they display their contents correctly, and add a ‘Nominal details’ heading in the form header area. 7 Close and save our form and then change its name to frmNominalDetails. 1.2.7 Drill down 1.2.7.1 Introduction In this section we will add a button to frmTrialBalance that will open frmNominalDetails and only display those transactions that relate to a particular nominal code. Open frmTrialBalance in design view. Make sure the ‘Control Wizards’ button on the toolbox is selected and click on the Command Button tool. Draw a small button just to the left of the Nominal code field in the Detail area. This will launch the Command Button Wizard. We need a Form Operation, and the Open Form Action. We wish to open frmNominalDetails and we need to ‘Open the form and find specific data to display’. We need to match FirstofNominalCode with NominalCode. Our button won’t have much room for text, so you can click on the ‘Show all pictures’ option and choose whichever picture you think most suitable. Finally, call the button ‘cmdDetails’. Close and save the trial balance form. Now open it in form view and try out the new button. 1.3 Detailed nominal report 1.3.1 Introduction Finally for our short series of Access tutorials we will use the Access report wizard to create a report that prints all our transactions grouped by nominal code. 1.3.2 Report wizard From the ‘Queries’ section of our database, click once on qryDetailedNominal, then on the ‘New object’ drop down. Choose the New report option, and then choose Report Wizard. The source should show as qryDetailedNominal. Click OK to launch the Report Wizard. On the first screen we need to select our fields in the order in which they will appear on the report. We will include the Nominal ID, code and description fields first as these will be our group headings. Next come our four Transaction fields, then the Analysis Code and Description fields, followed by TransactionTypeRef and finally the all important value field. 8 Onto the next step. Here we set the grouping levels. We do not want to group by TransactionTypeRef so we use the left chevron to remove this and replace it with NominalID. We only need one grouping level, bit we could use up to four, and we could set grouping options e.g. if we were grouping by a date field, we could click the ‘Grouping Options’ button to group by month or year. Next for the sorting and summary options. We will sort by TransactionID (remember we are already grouping by NominalID). We could add another three sorting levels if we wanted to. Be careful not to forget the Summary Options button – this will allow you to add totals fields to your report automatically. We will calculate a Sum value, and show the detail and summary. We don’t need a percent of total for each sum. 9 Next we will choose the ‘Align Left 1’ layout for our report. Choose whichever style you fancy on the next screen – we went for Soft Gray, in spite of the incorrect spelling. Finally, give your report the title Nominal details, and choose to Modify the report’s design when you click the Finish button. 10 1.3.3 Editing a report We should now see the design of our form. We need to do some re-arranging. Drag the nominal code and description fields from the detail section into the NominalID header area. Adjust the widths of the ID, code and description fields to fit the likely contents, and change the NominalID label to ‘Nominal code’. Delete the Nominal code and description headings from the NominalID Header area and rearrange the remaining headings and fields to allocate adequate width – leave some room to the right hand side of the report. You will probably find it a good idea to delete the border lines in the heading area, as their absence makes it much easier to select the fields and heading labels. We are going to add a running totals field to the right of the value field. Click on the existing ‘Value’ field and choose Edit, Duplicate from the menu. Drag the copy of the field to the right of the original. In the Data properties of the new field choose ‘Over Group’ in the Running Sum property. This should give us a running total for each nominal code. Now arrange, align and format the headings and fields the way you want them. You might want to use the ‘debit’ number format that we used for the trial balance form. 11 Save your report. Now look at the preview of the report. You might need a few adjustments until it is exactly as you want it. It tends to be a bit of a struggle getting all the information to fit elegantly. 12 1.3.3.1 Sorting and grouping One of the most significant features of a report is the ability to group information together and to create totals for those groups. The Report Wizard helped us set the grouping for our report but if we needed to change this we could do so using the sorting and grouping screen. For our report we are grouping on the nominal ID, and sorting within nominal ID by Transaction ID. It is possible to group on up to 4 different fields with corresponding sub-totals. For each group a header and/or a footer can be included. In our case we include a header in which we print the nominal account name, and in the footer we have included the nominal account total. In this example we want a new group for each nominal code. The ‘Group on’ and ‘Group interval’ properties allow us to group items in different ways dependant on the field type. Text fields can be grouped by one or more prefix characters (this could be useful if you are using a hierarchical coding structure), number fields can be grouped by hundreds or thousands for example, and date fields by a whole range of different time types, from minutes to multiple years. The ‘Keep together’ property controls pagination and allows us to keep the whole group together or just prevent the first detail being left on its own on a page. 1.3.3.2 Report and page sections In addition to the headers and footers that can be set up as a result of using grouping, each report can have a page header and footer and an overall report header and footer. The report header includes the title and the report footer the Grand total. The report footer is probably not all that useful in this case, so we could change the ‘Visible’ property for this section to ‘No’. Just click on the section title bar and change the required property. The page footer includes the expression ‘=Now()’ – this displays the current date and/or time depending on the setting of the control’s format property. The page footer goes a little beyond simply displaying the page number and includes the total number of pages that comprise the report e.g.: page 1 of 9. The detail section includes each individual transaction with the NominalID footer section including the overall total for that code. This is achieved using the expression: =Sum([Value]) Each section has a ‘visible’ property that enables it to be suppressed from the report. So if you want only to see summarised information you could set the visible property of the detail section to ‘No’. 13 1.4 Conclusion Well, that’s about it, I hope you found some or all of this four part marathon useful. You might want to finish things off by adding buttons to the ETB form to open the trial balance form and maybe print the nominal details report. Hopefully you should now feel confident to attempt this on your own. Good luck! 14