Group tables (1 Viewer)

Kepcla72

New member
Local time
Today, 03:51
Joined
Jul 3, 2024
Messages
4
Hi Guys, I'm trying to group a table, but it´s impossible for me...

I have this table:

CarFromToColor
Ford
1​
2​
Red
Ford
2​
3​
Red
Ford
4​
5​
Blue
Ford
6​
7​
Blue
Ford
8​
9​
Blue
Ford
10​
11​
Blue
Ford
12​
13​
Red
Ford
14​
15​
Red
Ford
16​
17​
Green
Ford
18​
19​
Green
Ford
19​
20​
Blue
Ford
21​
22​
Blue
Ford
23​
24​
Blue

And I need to achieve this:

CarFromToColor
Ford
1​
3​
Red
Ford
4​
11​
Blue
Ford
12​
15​
Red
Ford
16​
19​
Green
Ford
20​
24​
Blue

I appreciate a lot your help.
Regards
K
 

bob fitz

AWF VIP
Local time
Today, 08:51
Joined
May 23, 2011
Messages
4,742
EDITED
How do you arrive at the values you have in the "From" and "To" columns of the second diagram
 

Gasman

Enthusiastic Amateur
Local time
Today, 08:51
Joined
Sep 21, 2011
Messages
14,796
DMAX() ?
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 08:51
Joined
Jul 9, 2003
Messages
16,484
I think this might do what you want - See Attached:-
 

Attachments

  • GroupTables_2a.zip
    550.8 KB · Views: 19

jdelano

New member
Local time
Today, 03:51
Joined
Mar 22, 2024
Messages
19
I was thinking that because the colors aren't grouped (so they aren't looking for the min/max) I used VBA on a form to group the records into another table.

Code:
Option Compare Database

Private Sub Command1_Click()
    
    Dim carTable As Recordset
    Dim groupedTable As Recordset
    
    Dim currentColor As String
    Dim previousToQTY As Integer
    Dim previousCar As String
    Dim firstFromQty As Integer
    Dim lastToQTY As Integer
    
    ' get the records from the car table
    Set carTable = CurrentDb.OpenRecordset("Select * from tblCar")
    
    ' clear the grouped table
    CurrentDb.Execute "Delete * from tblCarGrouped"
    
    ' opent the grouped table
    Set groupedTable = CurrentDb.OpenRecordset("Select * from tblCarGrouped")
    
    Do While Not carTable.EOF
        If currentColor <> carTable("Color") Then
            ' a different car has been reached
            
            If currentColor <> "" Then
                ' the loop has gone through at least 1 row
                ' write the group to the group table
                groupedTable.AddNew
                groupedTable("Car") = previousCar
                groupedTable("FromQty") = firstFromQty
                groupedTable("ToQty") = previousToQTY
                groupedTable("color") = currentColor
                groupedTable.Update
            End If
            
            firstFromQty = carTable("FromQty")
            currentColor = carTable("Color")
        End If
        
        previousToQTY = carTable("ToQty")
        previousCar = carTable("Car")
        
        carTable.MoveNext
    Loop
    
    ' write the last grouping to the group table
    groupedTable.AddNew
    groupedTable("Car") = previousCar
    groupedTable("FromQty") = firstFromQty
    groupedTable("ToQty") = previousToQTY
    groupedTable("color") = currentColor
    groupedTable.Update
    
    ' close and release the recordsets
    carTable.Close
    Set carTable = Nothing
    
    groupedTable.Close
    Set groupedTable = Nothing
    
    Me.Grouped_Data.Requery ' force the subform to display the new records
End Sub

You can add another level to the loop if the car changes as well.
 

Attachments

  • Screenshot 2024-07-03 080339.png
    Screenshot 2024-07-03 080339.png
    60.6 KB · Views: 18

Pat Hartman

Super Moderator
Staff member
Local time
Today, 03:51
Joined
Feb 19, 2002
Messages
44,043
Select Car, Min(From) as MinFrom, Max(To) as MaxTo, Color
From YourTable
Group by Car, Color;
 

Kepcla72

New member
Local time
Today, 03:51
Joined
Jul 3, 2024
Messages
4
I was thinking that because the colors aren't grouped (so they aren't looking for the min/max) I used VBA on a form to group the records into another table.

Code:
Option Compare Database

Private Sub Command1_Click()
   
    Dim carTable As Recordset
    Dim groupedTable As Recordset
   
    Dim currentColor As String
    Dim previousToQTY As Integer
    Dim previousCar As String
    Dim firstFromQty As Integer
    Dim lastToQTY As Integer
   
    ' get the records from the car table
    Set carTable = CurrentDb.OpenRecordset("Select * from tblCar")
   
    ' clear the grouped table
    CurrentDb.Execute "Delete * from tblCarGrouped"
   
    ' opent the grouped table
    Set groupedTable = CurrentDb.OpenRecordset("Select * from tblCarGrouped")
   
    Do While Not carTable.EOF
        If currentColor <> carTable("Color") Then
            ' a different car has been reached
           
            If currentColor <> "" Then
                ' the loop has gone through at least 1 row
                ' write the group to the group table
                groupedTable.AddNew
                groupedTable("Car") = previousCar
                groupedTable("FromQty") = firstFromQty
                groupedTable("ToQty") = previousToQTY
                groupedTable("color") = currentColor
                groupedTable.Update
            End If
           
            firstFromQty = carTable("FromQty")
            currentColor = carTable("Color")
        End If
       
        previousToQTY = carTable("ToQty")
        previousCar = carTable("Car")
       
        carTable.MoveNext
    Loop
   
    ' write the last grouping to the group table
    groupedTable.AddNew
    groupedTable("Car") = previousCar
    groupedTable("FromQty") = firstFromQty
    groupedTable("ToQty") = previousToQTY
    groupedTable("color") = currentColor
    groupedTable.Update
   
    ' close and release the recordsets
    carTable.Close
    Set carTable = Nothing
   
    groupedTable.Close
    Set groupedTable = Nothing
   
    Me.Grouped_Data.Requery ' force the subform to display the new records
End Sub

You can add another level to the loop if the car changes as well.
It works Perfect!!!
 

Cronk

Registered User.
Local time
Today, 17:51
Joined
Jul 4, 2013
Messages
2,786
Those VBA solutions will work fine as long as only one make of car is used. If you only have one make (Ford) why bother having a field in your table?
 

jdelano

New member
Local time
Today, 03:51
Joined
Mar 22, 2024
Messages
19
This handles if there could be a different car
Code:
Option Compare Database

Private Sub Command1_Click()
    
    Dim carTable As Recordset
    Dim groupedTable As Recordset
    
    Dim currentCar As String
    Dim currentColor As String
    Dim previousToQTY As Integer
    Dim previousCar As String
    Dim firstFromQty As Integer
    Dim lastToQTY As Integer
    
    ' get the records from the car table
    Set carTable = CurrentDb.OpenRecordset("Select * from tblCar")
    
    ' clear the grouped table
    CurrentDb.Execute "Delete * from tblCarGrouped"
    
    ' opent the grouped table
    Set groupedTable = CurrentDb.OpenRecordset("Select * from tblCarGrouped")
    
    Do While Not carTable.EOF
        If currentCar <> carTable("Car") Then
        
            If currentCar <> "" Then
                ' the loop has gone through at least 1 row
                ' write the group to the group table
                groupedTable.AddNew
                groupedTable("Car") = previousCar
                groupedTable("FromQty") = firstFromQty
                groupedTable("ToQty") = previousToQTY
                groupedTable("color") = currentColor
                groupedTable.Update
            End If
            
            firstFromQty = carTable("FromQty")
            currentColor = carTable("Color")
            currentCar = carTable("Car")
        
        End If
    
        If currentColor <> carTable("Color") Then
            ' a different car has been reached
            
            If currentColor <> "" Then
                ' the loop has gone through at least 1 row
                ' write the group to the group table
                groupedTable.AddNew
                groupedTable("Car") = previousCar
                groupedTable("FromQty") = firstFromQty
                groupedTable("ToQty") = previousToQTY
                groupedTable("color") = currentColor
                groupedTable.Update
            End If
            
            firstFromQty = carTable("FromQty")
            currentColor = carTable("Color")
        End If
        
        previousToQTY = carTable("ToQty")
        previousCar = carTable("Car")
        
        carTable.MoveNext
    Loop
    
    ' write the last grouping to the group table
    groupedTable.AddNew
    groupedTable("Car") = previousCar
    groupedTable("FromQty") = firstFromQty
    groupedTable("ToQty") = previousToQTY
    groupedTable("color") = currentColor
    groupedTable.Update
    
    ' close and release the recordsets
    carTable.Close
    Set carTable = Nothing
    
    groupedTable.Close
    Set groupedTable = Nothing
    
    Me.Grouped_Data.Requery ' force the subform to display the new records
End Sub
 

Attachments

  • Screenshot 2024-07-06 044058.png
    Screenshot 2024-07-06 044058.png
    92.8 KB · Views: 8

ebs17

Well-known member
Local time
Today, 09:51
Joined
Feb 7, 2020
Messages
2,090
Nice, my issue is I see VBA as my hammer, and everything is a nail.
At this point, a VBA solution like the one shown is quick and easy, and for most people it is easy to read and maintain. So this is a good option.

A query solution is of course also possible. You could proceed as follows, for example:
- Generate a sequentiell number to display the sequence shown
- For each group (blue Ford), the first record (no predecessor) and the last record (no successor) are determined. Predecessor and successor related to this sequentiell number.
- From this, you can then determine the From value and the To value.

Overall, however, this is a bit complex.
 
Last edited:

Cronk

Registered User.
Local time
Today, 17:51
Joined
Jul 4, 2013
Messages
2,786
This handles if there could be a different car

Without any order, your code assumes the data is ordered by Car, To and Color

Maybe

Code:
    Set groupedTable = CurrentDb.OpenRecordset("Select * from tblCarGrouped" By order Car,To,Color)
 

jdelano

New member
Local time
Today, 03:51
Joined
Mar 22, 2024
Messages
19
True, that could be the Order By given the list looking at the example.
 

Users who are viewing this thread

Top Bottom