Question : Reversing a cross tab using ado

Hello,
I've imported a spreadsheet, that I need to normalize and store in a normalized form.
I've ramed this table Sheet1 in the attached spreadsheet which I copied and pasted from the access database table named sheet 1.  In this table, field1 to field9 will alway be there.  However, all fields to the right of field9 are metric fields.  Each metric consists of three components.  
1. MN - metric name
2. UM - unit of measure
3. MV - metric value
For example, In the attached example I have 4 different metrics, which have their respective metric name, unit of measure, and metric malue.  In addition, the number of metrics can vary depending on the import file.  I am trying to store all the metrics in three fields, where all the metric names will be stored in the metric name field, units of measure stored in the  unit of measure field, and the metric values is stored in the metric value fields.  My goal is to get the data into the 1st normal form, and eventually to the 3rd normal form.  In the attached code, I've stored the contents of Sheet1 in an array and Ive verified that the data is being stored there by looping through the array and viewing the contents.  I am able to populate the destination table with the non-metric data, but I am having a devil of a time populating the metric fields.  I know it's something simple, but I've tried all day yesterday.  I need another pair of eyes.
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
Private Sub PopulateStagingTable()

Dim rst1 As New ADODB.Recordset
Dim rst2 As New ADODB.Recordset
Dim cn As New ADODB.Connection
Dim intR As Integer         'Row counter - number of records
Dim intC As Integer         'Column counter - number of columns
Dim arrInput() As String

Set cn = CurrentProject.Connection

rst1.Open "Sheet1", cn, adOpenStatic, adLockOptimistic

intR = rst1.RecordCount
intC = rst1.Fields.Count

ReDim arrInput(intR, intC)
Dim x As Integer
Dim y As Integer
For x = 0 To intR - 1
    For y = 0 To intC - 1
        arrInput(x, y) = Nz(rst1(y), "")
        Debug.Print rst1.Fields(y).Name
    Next y
    rst1.MoveNext
Next x


rst2.Open "tbl2NFStaging", cn, adOpenStatic, adLockOptimistic

Dim intMetric As Integer

Dim intMetricStart As Integer
intMetricStart = 9

intMetric = UBound(arrInput, 2) - 9
'Debug.Print intMetric / 3
Dim z As Integer

Dim j As Integer
Dim i As Integer
Dim u As Integer
u = 10
i = 10
For x = 1 To UBound(arrInput, 1)
 For z = 0 To intMetric - 1
    
            rst2.AddNew
            rst2(1) = Nz(arrInput(x, 0), " ")
            rst2(2) = Nz(arrInput(x, 1), " ")
            rst2(3) = Nz(arrInput(x, 2), " ")
            rst2(4) = Nz(arrInput(x, 3), " ")
            rst2(5) = Nz(arrInput(x, 4), " ")
            rst2(6) = Nz(arrInput(x, 5), " ")
            rst2(7) = Nz(arrInput(x, 6), " ")
            rst2(8) = Nz(arrInput(x, 7), " ")
            rst2(9) = Nz(arrInput(x, 8), " ")
            
            
            rst2.Update
    
    Next z
    
     
    rst2.MoveNext
 
Next x





'Cleanup
rst1.Close
Set rst1 = Nothing
cn.Close
Set cn = Nothing


End Sub

Answer : Reversing a cross tab using ado

Hello,

To store your data, you need two tables, not one. Basically:

For x = 1 To UBound(arrInput, 1)
    rst2.AddNew
    rst2(1) = arrInput(x, 0)
    ' etc.
    rst2.Update
    For z = 0 To intMetric - 1
        rst3.AddNew
        rst3(0) = arrInput(x, 0)   ' ID field?
        rst3(1) = arrInput(x, 9 + intMetric * 3 + 0)
        rst3(2) = arrInput(x, 9 + intMetric * 3 + 1)
        rst3(3) = arrInput(x, 9 + intMetric * 3 + 2)
        rst3.Update
    Next z
Next x

Something like that?
(°v°)
Random Solutions  
 
programming4us programming4us