Question : Reversing cross tab

I am trying to partially normalize a table that is in cross tab format.  I've imported an exce spreadsheet into an access staging table and I want to then import the data into another table using vba/ADO to first transform the data into a form that can more easily be normalized.  I've attached an Excel spreadsheet showing both the contents of the staging table and the desired output.  The field names are in bold within the grey background.  Thanks

Answer : Reversing cross tab

This code works on tables, Staging, Desired.
It takes diffeent year groups.

I assumed Desired table exists ith the 3 fields (YearGroup, Benifit, Qty)
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
Private Sub cmdRevertXTab_Click()
    Dim rs As Recordset
    Set rs = CurrentDb.OpenRecordset("Staging")
    Dim g(100) As String
    Dim n As Integer
    n = rs.Fields.Count - 1
    For i = 2 To rs.Fields.Count - 1
       g(i) = rs(i).Name
    Next
    DoCmd.SetWarnings off
    DoCmd.RunSQL "Delete * from Desired"
    For i = 2 To n
        rs.MoveFirst
        Do While Not (rs.EOF) '
            sql = "Insert into Desired Values ('" & g(i) & "', '" & rs(1) & "','" & Nz(rs(i), "0") & "')"
            
            DoCmd.RunSQL sql
            rs.MoveNext
        
        Loop
    Next i
    DoCmd.SetWarnings True
End Sub
Random Solutions  
 
programming4us programming4us