home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Toolbox / Visual Basic Toolbox (P.I.E.)(1996).ISO / arrays / vsflex / mrgout.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1995-06-19  |  9.0 KB  |  308 lines

  1. VERSION 2.00
  2. Begin Form fTotals 
  3.    BackColor       =   &H00C0C0C0&
  4.    BorderStyle     =   3  'Fixed Double
  5.    Caption         =   "FlexTotals"
  6.    ClientHeight    =   6030
  7.    ClientLeft      =   900
  8.    ClientTop       =   900
  9.    ClientWidth     =   6270
  10.    Height          =   6495
  11.    Left            =   810
  12.    LinkTopic       =   "Form1"
  13.    ScaleHeight     =   6030
  14.    ScaleWidth      =   6270
  15.    Top             =   525
  16.    Width           =   6450
  17.    Begin CheckBox Check1 
  18.       BackColor       =   &H00C0C0C0&
  19.       Caption         =   "Only Subtotals"
  20.       FontBold        =   0   'False
  21.       FontItalic      =   0   'False
  22.       FontName        =   "MS Sans Serif"
  23.       FontSize        =   8.25
  24.       FontStrikethru  =   0   'False
  25.       FontUnderline   =   0   'False
  26.       ForeColor       =   &H00000000&
  27.       Height          =   210
  28.       Left            =   3405
  29.       TabIndex        =   3
  30.       Top             =   690
  31.       Width           =   1395
  32.    End
  33.    Begin CommandButton Command1 
  34.       BackColor       =   &H00C0C0C0&
  35.       Caption         =   "Rollups"
  36.       FontBold        =   0   'False
  37.       FontItalic      =   0   'False
  38.       FontName        =   "MS Sans Serif"
  39.       FontSize        =   8.25
  40.       FontStrikethru  =   0   'False
  41.       FontUnderline   =   0   'False
  42.       Height          =   345
  43.       Left            =   4935
  44.       TabIndex        =   2
  45.       Top             =   585
  46.       Width           =   990
  47.    End
  48.    Begin vsFlexArray fa 
  49.       BackColor       =   &H00FFFFFF&
  50.       BackColorFixed  =   &H00800000&
  51.       Cols            =   4
  52.       DragIcon        =   MRGOUT.FRX:0000
  53.       FixedCols       =   0
  54.       FocusRect       =   0  'None
  55.       FontBold        =   -1  'True
  56.       FontItalic      =   0   'False
  57.       FontName        =   "Arial"
  58.       FontSize        =   9.75
  59.       FontStrikethru  =   0   'False
  60.       FontUnderline   =   0   'False
  61.       ForeColor       =   &H00000000&
  62.       ForeColorFixed  =   &H00FFFFFF&
  63.       FormatString    =   "^Region           |^Product       |^Employee    |>Sales            "
  64.       Height          =   4905
  65.       HighLight       =   0  'Never
  66.       Left            =   270
  67.       MergeCells      =   2  'Restrict Rows
  68.       ScrollTrack     =   -1  'True
  69.       TabIndex        =   0
  70.       Top             =   1050
  71.       Width           =   5760
  72.    End
  73.    Begin Label Label1 
  74.       BackColor       =   &H00000000&
  75.       BackStyle       =   0  'Transparent
  76.       Caption         =   "Drag headings over other columns to reorganize the summary sheet. You can show sales TOTALS by region, product, or employee."
  77.       FontBold        =   -1  'True
  78.       FontItalic      =   0   'False
  79.       FontName        =   "Arial"
  80.       FontSize        =   9.75
  81.       FontStrikethru  =   0   'False
  82.       FontUnderline   =   0   'False
  83.       ForeColor       =   &H00000000&
  84.       Height          =   915
  85.       Left            =   240
  86.       TabIndex        =   1
  87.       Top             =   75
  88.       Width           =   5760
  89.    End
  90. Option Explicit
  91. Sub Check1_Click ()
  92.    Dim i%
  93.    For i = 1 To fa.Rows - 1
  94.       If Not fa.MergeRow(i) Then fa.RowHeight(i) = check1 - 1
  95.    Next i
  96. End Sub
  97. Sub Command1_Click ()
  98.    MsgBox "Double-click on the yellow cells to collapse or expand detailed information"
  99. End Sub
  100. Sub DoCalculateTotals ()
  101.     '----------------------------------------------------------------
  102.     ' Loop thru all the cells and calculate totals and subtotals
  103.     '
  104.     ' SaveGroupingKeys
  105.     ' Do
  106.     '    ForAllLevels
  107.     '       if SameCategory then
  108.     '          AddTotals
  109.     '       Else
  110.     '          PrintTotals
  111.     '          Totals = 0
  112.     '          SaveSortingKeys
  113.     '       endif
  114.     ' Loop
  115.     Dim i%, j%, k%, h%
  116.     Dim s$(), tot()
  117.     Dim TotLevels%, TotOnColumn%
  118.     TotLevels = 2
  119.     TotOnColumn = 3
  120.     ReDim tot(TotLevels), s(TotLevels)
  121.     ' save first grouping keys
  122.     i = 1
  123.     For k = 0 To TotLevels - 1
  124.       s(k) = fa.TextArray(fai(1, k))
  125.     Next
  126.     Do
  127.         For h = 0 To TotLevels - 1
  128.            If s(h) = fa.TextArray(fai(i, h)) Then
  129.                tot(h) = tot(h) + Val(fa.TextArray(fai(i, TotOnColumn)))
  130.            Else
  131.                ' print totals
  132.                For k = TotLevels - 1 To h Step -1
  133.                   DoSubTotals k, s(k), tot(), i - (k - 1)
  134.                Next
  135.                i = i + TotLevels - h
  136.                ' reset totals
  137.                For k = h + 1 To TotLevels - 1
  138.                   tot(k) = 0
  139.                Next
  140.                For k = 0 To h
  141.                   tot(h) = fa.TextArray(fai(i, TotOnColumn))
  142.                Next
  143.                ' save new category index
  144.                For k = 0 To TotLevels - 1
  145.                  s(k) = fa.TextArray(fai(i, k))
  146.                Next
  147.            End If
  148.         Next h
  149.         
  150.         i = i + 1
  151.         If i >= fa.Rows Then
  152.             For k = TotLevels - 1 To 0 Step -1
  153.                   DoSubTotals k, s(k), tot(), i - (k - 1)
  154.             Next
  155.             Exit Do
  156.         End If
  157.     Loop
  158.     fa.MergeCells = 2
  159. End Sub
  160. Sub DoFillData ()
  161.     Dim i%
  162.     Dim sreg$, sprod$, semp$, ssale$
  163.     For i = fa.FixedRows To fa.Rows - 1
  164.       sreg = Region()
  165.       sprod = Product()
  166.       semp = Employee()
  167.       ssale = Sales()
  168.       fa.TextArray(fai(i, 0)) = sreg
  169.       fa.TextArray(fai(i, 1)) = sprod
  170.       fa.TextArray(fai(i, 2)) = semp
  171.       fa.TextArray(fai(i, 3)) = ssale
  172.     Next
  173. End Sub
  174. Sub DoRemoveTotals ()
  175.    Dim i%
  176.     '-------------------------------------------------
  177.     ' remove old totals
  178.     i = 1
  179.     While i < fa.Rows
  180.         fa.RowHeight(i) = -1
  181.         If fa.MergeRow(i) Then
  182.             fa.RemoveItem i
  183.         Else
  184.             i = i + 1
  185.         End If
  186.     Wend
  187. End Sub
  188. Sub DoSort ()
  189.     DoRemoveTotals
  190.     ' sort
  191.     fa.Col = 0
  192.     fa.ColSel = fa.Cols - 1
  193.     fa.Sort = 1
  194.     DoCalculateTotals
  195. End Sub
  196. Sub DoSubTotals (Level%, s$, tot(), i%)
  197.     Dim itm$, t$, k%
  198.     t = Chr(9)
  199.     Select Case Level
  200.       Case 1   ' Second Column Totals
  201.          itm = fa.TextArray(fai(i - 1, 0)) + t
  202.          For k = Level To fa.Cols - 2
  203.             itm = itm + s & " Total " & t
  204.          Next k
  205.          itm = itm + Format(tot(1), "#,##0.00")
  206.          
  207.          fa.AddItem itm, i
  208.          fa.MergeRow(i) = True
  209.          
  210.          ' change visual attributes
  211.          fa.Row = i
  212.          fa.Col = 1
  213.          fa.ColSel = 3
  214.          fa.CellBackColor = &H80FFFF    ' yellow
  215.          fa.CellFontBold = True
  216.       Case 0
  217.          itm = s + " Total"
  218.          itm = itm + t + itm + t + itm + t + Format(tot(0), "#,##0.00")
  219.          
  220.          fa.AddItem itm, i
  221.          fa.MergeRow(i) = True
  222.          
  223.          fa.Row = i
  224.          fa.Col = 0
  225.          fa.ColSel = 3
  226.          fa.CellBackColor = 1
  227.          fa.CellForeColor = RGB(255, 255, 255)
  228.          fa.CellFontBold = True
  229.     End Select
  230.        
  231. End Sub
  232. Function Employee () As String
  233.     Select Case (Rnd * 1000) Mod 3
  234.         Case 0: Employee = "Mary"
  235.         Case 1: Employee = "Sarah"
  236.         Case Else: Employee = "Paula"
  237.     End Select
  238. End Function
  239. Sub fa_DblClick ()
  240.     Dim r%
  241.     ' find totals row
  242.     r = fa.MouseRow
  243.     If r = 0 Then Exit Sub
  244.     If Not fa.MergeRow(r) Then Exit Sub
  245.     While Not fa.MergeRow(r)
  246.         r = r - 1
  247.     Wend
  248.     ' expand or contract?
  249.     r = r - 1
  250.     If fa.RowHeight(r) = 0 Then
  251.         Do While Not fa.MergeRow(r)
  252.             fa.RowHeight(r) = -1
  253.             r = r - 1
  254.             If r = 0 Then Exit Do
  255.         Loop
  256.     Else
  257.         Do While Not fa.MergeRow(r)
  258.             fa.RowHeight(r) = 0
  259.             r = r - 1
  260.             If r = 0 Then Exit Do
  261.         Loop
  262.     End If
  263. End Sub
  264. Sub fa_DragDrop (Source As Control, X As Single, Y As Single)
  265.     If fa.Tag = "" Then Exit Sub
  266.     If fa.Tag = fa.MouseCol Then Exit Sub
  267.     fa.Redraw = False
  268.     fa.ColPosition(Val(fa.Tag)) = fa.MouseCol
  269.     DoSort
  270.     fa.Redraw = True
  271. End Sub
  272. Sub fa_MouseDown (Button As Integer, Shift As Integer, X As Single, Y As Single)
  273.     fa.Tag = ""
  274.     If fa.MouseRow <> 0 Or fa.MouseCol = 3 Then Exit Sub
  275.     fa.Tag = Str(fa.MouseCol)
  276.     fa.Drag 1
  277. End Sub
  278. Function fai (r%, c%) As Integer
  279.     fai = c + fa.Cols * r
  280. End Function
  281. Sub Form_Load ()
  282.     DoFillData
  283.     fa.MergeCol(0) = True
  284.     fa.MergeCol(1) = True
  285.     fa.MergeCol(2) = True
  286.     fa.FillStyle = 1
  287.     DoSort
  288.     check1 = 0
  289. End Sub
  290. Function Product () As String
  291.     Select Case (Rnd * 1000) Mod 4
  292.         Case 0: Product = "Wahoos"
  293.         Case 1: Product = "Foobars"
  294.         Case Else: Product = "Applets"
  295.     End Select
  296. End Function
  297. Function Region () As String
  298.     Select Case (Rnd * 1000) Mod 4
  299.         Case 0: Region = "North"
  300.         Case 1: Region = "South"
  301.         Case 2: Region = "West"
  302.         Case Else: Region = "East"
  303.     End Select
  304. End Function
  305. Function Sales () As String
  306.     Sales = Format(Rnd * 10000, "#.00")
  307. End Function
  308.