'Vb6 Grouping Values in Flexgrid

I have a Flexgrid (vb6 not .Net) disconnected from any Db, having some rows like this:

135,00  4
218,00  4
100,00  10
6,00    4
15,00   22

I'm not able to create a cycle to obtain how many different groups (by vat) i have on the grid and calculate the relative amount so to have 3 Groups (in this case 4 / 10 / 22) 4 = 359,00 10 = 100 22 = 15,00 I don't want to use a database with select group by, rather pre-calculate the amount and visualize in n text box.

This is the code i have tried

Dim A, b, c, d 
if A = "" Then
A = xNum
  ElseIf A <> "" Then
 If A <> xNum Then
 If b = "" Then
   b = xNum
     ElseIf b <> "" Then
        If b <> xNum Then
          If c = "" Then
             c = xNum
              ElseIf c <> "" Then
                 If c <> xNum Then
                    If d = "" Then
                       d = xNum
                        ElseIf d <> "" Then [etc...]

Thanks for your help.



Solution 1:[1]

I assume you are using a Microsoft FlexGrid control where the first column is a quantity and the second column is the vat.

enter image description here

The following code will produce the results shown in the TextBox:

Option Explicit

Private Sub cmdCalc_Click()
   On Error Resume Next

   Dim c As Collection
   Dim i As Integer
   Dim g As Group
   Dim data As String

   'calculate groups
   Set c = New Collection

   For i = 1 To MSFlexGrid1.Rows - 1
      Set g = New Group
      g.VAT = CInt(MSFlexGrid1.TextMatrix(i, 2))
      g.Qty = CDbl(MSFlexGrid1.TextMatrix(i, 1))

      Err.Clear
      c.Add g, CStr(g.VAT)
      If Err.Number > 0 Then c(CStr(g.VAT)).Qty = c(CStr(g.VAT)).Qty + g.Qty
   Next

   'and visualize the data
   For Each g In c
      data = data & g.VAT & vbTab & g.Qty & vbCrLf
   Next

   Text1.Text = data
End Sub

The basic idea of this code is to build a collection of group objects, one group for each row in the grid. As groups are being added to the collection, error trapping is utilized to detect duplicates and to increase the quantity in those cases.

Here is the Group class used by the above code:

Option Explicit

Private m_VAT As Integer
Private m_Qty As Double

Public Property Get VAT() As Integer
   VAT = m_VAT
End Property

Public Property Let VAT(ByVal Value As Integer)
   m_VAT = Value
End Property

Public Property Get Qty() As Double
   Qty = m_Qty
End Property

Public Property Let Qty(ByVal Value As Double)
   m_Qty = Value
End Property

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1