Hello gurus, I have been struggling for the past three days with this. I really need this to be done.
I need my columns with data un-pivoted and also have a title column adjacent populated with “column names” for data reference.
I did achieve a bit of it but the Do loop is not working for me. Below is my test case.
This is de-normalized source table:
Entry form:
This is what my results should look like:
Result should be
Things to note:
1. In the result set some of the columns are skipped during pivot like Fname, LName, TotalCount.
2. I also need the item count to be “Numeric field”. When I change it in my table I get an error during run time.
3. The above source table is de-normalized and done this way to meet a certain business need.
4. The id repeats with every represented data column. Like in my above source have 2 (id) but result set shows 3 (id) rows. This is correct because 3 columns have the related data (NumOfCars, NumOfBikes, NumOfVans). Even a null or a default zero.
5. The bottom table shows the result from my code. Think I got the column data. Column names are problem. The column names should show to their related id and data. I my case (NumOfCars, NumOfBikes, NumOfVans) twice for each unique id.
This is what I got so far:
Code:
I really appreciate any help I can get with this. Thanks in advance.
I need my columns with data un-pivoted and also have a title column adjacent populated with “column names” for data reference.
I did achieve a bit of it but the Do loop is not working for me. Below is my test case.
This is de-normalized source table:
Entry form:
Code:
id Fname Lname Cars Bikes Vans Count
1 James Philman 2 0 1 3
2 Robert Kinsey 1 1 2 4
This is what my results should look like:
Result should be
Code:
id Title Count
1 Cars 2
1 Bikes 0
1 Vans 1
2 Cars 1
2 Bikes 1
2 Vans 2
Things to note:
1. In the result set some of the columns are skipped during pivot like Fname, LName, TotalCount.
2. I also need the item count to be “Numeric field”. When I change it in my table I get an error during run time.
3. The above source table is de-normalized and done this way to meet a certain business need.
4. The id repeats with every represented data column. Like in my above source have 2 (id) but result set shows 3 (id) rows. This is correct because 3 columns have the related data (NumOfCars, NumOfBikes, NumOfVans). Even a null or a default zero.
5. The bottom table shows the result from my code. Think I got the column data. Column names are problem. The column names should show to their related id and data. I my case (NumOfCars, NumOfBikes, NumOfVans) twice for each unique id.
This is what I got so far:
Code:
id Title Count
id
FName
Lname
Cars
Bikes
Vans
Count
id
FName
Lname
Cars
Bikes
Vans
Count
1 2
1 0
1 1
2 1
2 1
2 2
Code:
Code:
Private Sub Command1_Click()
Dim db As DAO.Database
Dim sqlStr1 As String
Dim rs2 As DAO.TableDef
DoCmd.SetWarnings False
Set db = Currentdb
sqlStr1 = "SELECT * FROM tblData"
Set rs1 = db.OpenRecordset(sqlStr1)
Set rs2 = db.TableDefs("tblData")
Do While Not rs1.EOF = True
For i = 3 To rs1.Fields.Count - 2
CurrentProject.Connection.Execute "INSERT Into tblResult (id,ItemCount) Values('" & (rs1!id) & "','" & (rs1(i - l)) & "') "
Next
For Each p In rs2.Fields
CurrentProject.Connection.Execute "INSERT Into tblResult (Title) Values('" & (p.Name) & "') "
Next
rs1.MoveNext
Loop
Set rs1 = Nothing
Set rs2 = Nothing
DoCmd.SetWarnings True
End Sub
I really appreciate any help I can get with this. Thanks in advance.