Hello,
I have to denormalize/flatten a table and I can't get things to work correctly. I have a table called PERSON which contains contact names and titles for certain companies. There are three columns in the table - Company_ID (which is the primary key), Fulnm (which is the contact name), and Title (the title for the contact name). There are multiple contacts per company_id, our customers want us to denormalize/flatten the data so that all contacts belonging to one company_id are in one record/row. I need the column headings to be Person01, Title01, Person02, Title02, etc. I have the code somewhat close to what I need but all I am getting is duplicates right now. I would really appreciate any help given. Below is the code I am working with:
I have to denormalize/flatten a table and I can't get things to work correctly. I have a table called PERSON which contains contact names and titles for certain companies. There are three columns in the table - Company_ID (which is the primary key), Fulnm (which is the contact name), and Title (the title for the contact name). There are multiple contacts per company_id, our customers want us to denormalize/flatten the data so that all contacts belonging to one company_id are in one record/row. I need the column headings to be Person01, Title01, Person02, Title02, etc. I have the code somewhat close to what I need but all I am getting is duplicates right now. I would really appreciate any help given. Below is the code I am working with:
Code:
Option Compare Database
Option Explicit
Sub DenormalizeTable()
'this is the main subroutine which calls the others
CreateDenormalizedTable (MaxNumberOfFields)
Denormalize
End Sub
Function MaxNumberOfFields()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String
Dim NumberOfFields As Integer
Set db = CurrentDb
strSQL = "SELECT TOP 1 Count(PERSON.COMPANY_ID) AS FieldCount " _
& "FROM PERSON " _
& "GROUP BY PERSON.Company_ID " _
& "ORDER BY Count(PERSON.COMPANY_ID) ASC;"
Set rs = db.OpenRecordset(strSQL)
MaxNumberOfFields = rs!FieldCount
End Function
Sub CreateDenormalizedTable(FieldCount As Integer)
On Error GoTo Err_CreateDenormalizedTable
Dim db As DAO.Database
Dim tblNew As DAO.TableDef
Dim fld As Field
Dim IndexNumber As Integer
Set db = CurrentDb
' Create the table and a field
Set tblNew = db.CreateTableDef("PERSONNEL")
Set fld = tblNew.CreateField("COMPANY_ID" & IndexNumber, dbDouble)
Set fld = tblNew.CreateField("PERSON01" & IndexNumber, dbText)
Set fld = tblNew.CreateField("TITLE01" & IndexNumber, dbText)
Set fld = tblNew.CreateField("PERSON02" & IndexNumber, dbText)
Set fld = tblNew.CreateField("TITLE02" & IndexNumber, dbText)
tblNew.Fields.Append fld
For IndexNumber = 1 To FieldCount
' Set field properties here if you want ie.
' fld.Required = True
'
' Append field to Fields collection
tblNew.Fields.Append fld
Next IndexNumber
' Append table to TableDef collection
db.TableDefs.Append tblNew
Exit_CreateDenormalizedTable:
Exit Sub
Err_CreateDenormalizedTable:
If Err.Number = 3265 Then
Resume Next
Else
MsgBox Err.Description
Resume Exit_CreateDenormalizedTable
End If
End Sub
Sub Denormalize()
Dim db As DAO.Database
Dim rs1 As DAO.Recordset
Dim rs2 As DAO.Recordset
Dim FieldCount As Integer
Dim currentCompany_ID As Double, previousCompany_ID As Double
Set db = CurrentDb
Set rs1 = db.OpenRecordset("PERSON") 'table with old format
Set rs2 = db.OpenRecordset("PERSONNEL") 'table with new format
DoCmd.SetWarnings False
DoCmd.RunSQL ("Delete * from PERSONNEL")
DoCmd.SetWarnings True
FieldCount = 1
rs1.MoveFirst
Do While Not rs1.EOF
currentCompany_ID = rs1!Company_ID
If currentCompany_ID <> previousCompany_ID Then
FieldCount = 1
rs2.AddNew
rs2!Company_ID = rs1!Company_ID
rs2!PERSON01 = rs1!FULNM
rs2!TITLE01 = rs1!TITLE
rs2!PERSON02 = rs1!FULNM
rs2!TITLE02 = rs1!TITLE
rs2.Update
Else
FieldCount = FieldCount + 1
rs2.MoveLast
rs2.Edit
rs2!Company_ID = rs1!Company_ID
rs2!PERSON01 = rs1!FULNM
rs2!TITLE01 = rs1!TITLE
rs2!PERSON02 = rs1!FULNM
rs2!TITLE02 = rs1!TITLE
rs2.Update
End If
previousCompany_ID = currentCompany_ID
rs1.MoveNext
Loop
End Sub