Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Articles
Search: 

Using grids to show Account Statements
Rafael Copquin, August 1, 2002
Background Many VFP applications deal with accounting data in ways that, in my opinion, are not very efficient, from the program standpoint, or very clear to the user. An example of this is an account statement. Now, an account statement can be any accounting statement that portrays the tran...

Background

Many VFP applications deal with accounting data in ways that, in my opinion, are not very efficient, from the program standpoint, or very clear to the user.

An example of this is an account statement. Now, an account statement can be any accounting statement that portrays the transactions made in an account, such as a bank statement, a general ledger account, a detail of cash receipts and disbursements, a customer account, a supplier account, etc.

What all these statements have in common is the layout of the information in a columnar format, containing at least the following data:

  • Date of the transaction

  • Details of the transaction

  • If it is a positive amount, it is shown as a debit

  • If it is a negative amount it is shown as a credit

  • The balance of all transactions, which consists of adding to the balance in the previous line, the debit of the current line and subtracting the credit in the same line.

Many programs that I have seen deal with the issue of presenting this information by resorting to edit boxes, report forms (in paper or in the screen) or even combinations of @ row(), col() constructs. Sometimes, sofisticated listboxes are used, because they enable easy navigation "up and down" of the data.

In the days of Fox Dos, browse windows could be used. In fact, in my early accounting applications, written in FoxPro 2.6 for DOS, I used browse windows extensively. They can still be used in VFP, but we would be missing all the benefits of object oriented programming if we resorted to the old browse.

So, when faced with the challenge of converting my old DOS applications to VFP, I decided to use grids instead of browses in all instances. And the statements of account are a particularly challenging aspect of this conversion.

Problems involved

The problems involved in presenting the information in a grid are the following:

  • Show debits, credits and balance in different columns
  • Calculate the carrying balance and show it in the balance column

In order to understand why I mention the above as problems, let us consider the table we would need to save all the transactions and how we would retrieve the data.

Initially, I think that the most efficient table structure should contain at least the following relevant fields

Field Type

Account

C

Date

D

Details

C

Amount

C

idTransaction

I

So, if we are dealing with a customer account, an invoice would be saved as a positive amount, and its payment would be saved as a negative amount. Of course, if we were dealing with a supplier account, the invoices would be credits and the payments would be debits, but even though these are the constraints placed on us by the Accounting Science, as programmers we would pay heed to that, but from our point of view, the problems presented above still stand.

As we know from previous experience, there are many ways to solve the above problems in VFP, namely, and just to mention a few:

  • Use a filter
  • Develop an index on Account + dtos(Date) + str(IdTransaction)
  • Retrieve data with a SQL statement
  • Use a readonly view to retrieve the data

Using a filter is not a very good option, because of the punishment in performance it entails. It can be argued that with good indexes and Rushmore, filters can be used with no performance hits, but the question is: what kind of indexes do we need to filter the data ?

If we use the second solution, an index on account, date and transaction, we could select the customer table, pull the customer data relevant to the account exposure (company name, account number, address, telephone, etc) and, by establishing a relation to the accounts table, we could show the data for a particular account number in the grid. The relation would be something like:

Select customers
Set relation to account into accounts
Set skip to account

In this manner, we could filter the account data and show it in the grid.

Likewise, a better way to do this (in my opinion) is to use either a SQL statement or a read only view.

It would be something like this:

Local cAcct
Select customers
cAcct = customers.account

Select date,;
  details,;
  iif(amount > 0.00, amount, 0.00) as debit,;
  iif(amount > 0.00, abs(amount), 0.00) as credit,;
  000000000.00 as balance,;
  customer.company ;
from accounts ;
  left outer join customers on accounts.account = customers.account ;
where accounts.account = cAcct;
order by date, idTransaction;
into cursor cards readwrite

This statement uses the READWRITE clause, a new addition to VFP 7. If you are using VFP 6 you would obtain a read only cursor. By selecting this cursor again, you can make it readonly like so:

Use cards again alias thecards in 0

A read only view would use the same statement as above, but it would be parameterized, so the WHERE clause would read:

WHERE accounts.account = ?cAcct

But what do we notice in the examples above?

The balance is nowhere to be seen, except that in the case of the SQL statement, we obtained an extra empty column, the balance column, which contains only zeroes.

Therefore, we must calculate the balance. Since the cursor obtained is ordered by date and transaction, all the transactions are neatly placed in ascending date order, which corresponds to the real world.

So, all we have to do is start scanning the cursor from the top and calculate the balance, line by line in the following manner:

Local nBal

nBal = 0.00

Select cards
Go top

Scan
    nBal = nBal + debit - credit

    replace cards.balance with nBal
endscan
go top

But as pointed out before, "there are many ways to skin a fox". SQL fans would rightly say that we could use one single SQL statement, as show below, to obtain both the account data and the carrying balance:

Select ;
  t1.date,;
  t1.details,;
  t1.amount,;
  sum(t2.amount) as balance,;
  t1.idTransaction as trans1, ;
  t2.idTransaction as trans2 ;
from accounts as t1 ;
  join accounts as t2 on t1.account = t2.account and ;
  t1.idTransaction >= t2.idTransaction and ;
  t1.date >=t2.date ;
group by t1.date, ;
  t1.idTransaction;
order by t1.date, ;
  t1.idTransaction ;
where accounts.account = cAcct ;
into cursor cards readwrite

It looks convoluted, doesn't it ? Well, it is. But it does the job. This statement was suggested to me not long ago by someone from a VFP forum and of course I tried it and it works.

However, over a LAN, the combination of SQL statement and scan - endscan takes fractions of a second whereas the above statement takes almost 3 seconds, in a modern computer, and to pull some 30 records. Both ways work, but the first solution is faster.

Needles to say, if we had used the index approach mentioned in the first place, we would have needed an extra field in the table to place the account balance.

How do we show all this in a grid?

We place the grid on form and set it with the following construct:

With thisform.grid1
   .columncount = 5
   .readonly = .t.
   .recordsource = "cards"
   with .column1
      .controlsource = "cards.date"
      .header1.caption = "Date"
   endwith
        
   * (and so on)
Endwith

A finished form as an example

I use this approach to present statements of account and add lots of "perks" to my forms, such as:

  • A textbox where the user can enter the customer account, the valid of which searches the customer table, finds the account, calls the recalculate method to requery the view (yes, I use a read only view), in turn a reset_grid method is called and the data is shown in the grid
  • Add buttons to enable the user to view the invoice detailed in a particular line. This is accomplished by placing the mouse pointer on the line where the invoice details are shown, then a view selects the data of the particular invoice and shows it in another form
  • Another button shows the details of a receipt (all invoices paid in a single transaction for which a receipt was issued and posted in the account)
  • A button to show all unpaid invoices
  • A button to select all customer accounts and company names with unpaid balances
  • A button to show the aging of all unpaid invoices
  • A different color for the balance column
  • Etc, etc, etc

Conclusion

A grid can be a very good option to show account details. Not only it duplicates the real world (just compare it to your bank statement), but it enables you to be creative and efficient and your clients will love it.

Rafael Copquin, Estudio Copquin
Public Accountant and Certified Internal Auditor; Fox programmer since 1987, starting with FPD and upgrading to VFP. Vast experience in conversions from FPD to VFP, specialized in business and accounting systems. Member of the Microsoft Users Group of Argentina's Board of Directors as Treasurer.
More articles from this author
Rafael Copquin, April 1, 2006
This class is especially designed to build cursor adapters by simply passing a few parameters, such as table name, cursor name, cursor adapter object name, updatable or not, empty or not, complex or simple select statement.
Rafael Copquin, April 1, 2002
Those of us who have ever programmed FoxPro DOS applications, are marveled and at the same time overwhelmed with the possibilities of enhancement in the appearance, the performance and the different ways of doing the things that were difficult, or outright impossible to do in DOS, th...
Rafael Copquin, June 1, 2002
In the April 2002 issue, I showed the way to use a grid as a picklist, and in order to make the example clear and readily understandable, I did not worry about code reusability. The example simply showed how to program methods in the form containing the picklist grid in order to enable th...
Rafael Copquin, October 1, 2002
Background Early in my development experience, back in the days of Fox Dos, I had to develop a better way to make invoices than the one I had been using. What I had been doing was using the SCROLL statement to make the screen move upwards every time a new item was added. Whenever the user w...
Rafael Copquin, March 1, 2006
In October 2002 the UTMag published the first article on the subject of making invoices with grids. Ever since, I have received numerous emails from readers asking me how to save the invoice thus created in the server tables. This article explains how to use cursor adapters to save the data.
Rafael Copquin, July 1, 2006
When we are examining data from a table, using generally a grid control, even if we did it with a browse window, we are in reality viewing a small part of a, perhaps, enormous quantity of data. We are literally viewing only the amount of records that fit in the grid or the browse window. If we wa...
Rafael Copquin, June 5, 2013
When Microsoft discontinued development of Visual FoxPro, many would-be writers on VFP issues stopped sending articles to the UniversalThread and as a result, no more articles on VFP were published, with a few exceptions. Over the years, I kept receiving requests from many readers for a continu...
Rafael Copquin, March 1, 2007
Accounting and computer systems should not be tied to any one language in particular. You can design a computer system to be written in Fortran, Pascal, Cobol, VFP or Visual Basic.NET. However, the love of my life as far as computer languages are concerned is Visual FoxPro and that is why the rest o...
Rafael Copquin, September 1, 2006
The title sounds quizzical, doesn’t it? What has the Visual FoxPro menu system in common with XML? Not much. However, it is possible to use XML as a means to generate separate menus for every user. It consists of a series of case statements that would define pads, popups or menu bars, according to t...