19.12.2012 Views

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

■ CONTROL BREAK PROCESSING<br />

Suppose a small real estate company stores its sales data for a year in a sequential file in which<br />

each record contains four fields: month of sale, day of sale (1 through 31), address, <strong>and</strong> price.<br />

Typical data for the sales of the first quarter of a year are shown in Figure 7-2. The records are<br />

ordered by date of sale.<br />

Month Day Address Price<br />

January 9 102 Elm Street $203,000<br />

January 20 1 Main Street $315,200<br />

January 25 5 Maple Street $123,450<br />

February 15 1 Center Street $100,000<br />

February 23 2 Vista Drive $145,320<br />

March 15 205 Rodeo Circle $389,100<br />

FIGURE 7-2 Real Estate Sales for First Quarter of Year<br />

Figure 7-3 shows the output of a program that displays the total sales for the quarter year,<br />

with a subtotal for each month.<br />

FIGURE 7-3 Output of Example 3<br />

A program to produce the output of Figure 7-3 must calculate a subtotal at the end of<br />

each month. The variable holding the month triggers a subtotal whenever its value changes.<br />

Such a variable is called a control variable <strong>and</strong> each change of its value is called a break.<br />

EXAMPLE 3<br />

The following program produces the output of Figure 7-2. The data of Figure 7-2 are stored in the sequential<br />

file HOMESALE.TXT. The program allows for months with no sales. Because monthly subtotals will<br />

be printed, the month-of-sale field is an appropriate control variable.<br />

Private Sub cmdCreateReport_Click()<br />

Dim currentMonth As String, newMonth As String<br />

Dim dayNum As Integer, address As String<br />

Dim price As Single, monthTotal As Single<br />

Dim yearTotal As Single, doneFlag As Boolean<br />

‘Display home sales by month<br />

picReport.Cls<br />

Open “HOMESALE.TXT” For Input As #1<br />

currentMonth = “” ‘Name of month being subtotaled<br />

monthTotal = 0<br />

yearTotal = 0<br />

doneFlag = False ‘Flag to indicate end of list<br />

Do While Not doneFlag<br />

Using Sequential Files 227

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!