Sub createFlatTable()
Dim rs As DAO.Recordset, j As Integer, i, sPL As String, sFld
Dim rsMax As DAO.Recordset, rsNew As DAO.Recordset, maxProd
Dim rs1 As DAO.Recordset
Set rsMax = CurrentDb.OpenRecordset("select top 1 count(AssistedPersonID) from AssistNew group by AssistedPersonID order by count(AssistedPersonID) desc")
maxProd = rsMax(0)
For i = 1 To maxProd
sPL = sPL & "," & "AssistServiceCode" & i & " Text" & "," & "AssistProgramCode" & i & " Text" & ", " & "AssistDateBegin" & i & " text"
Next
sPL = Mid(sPL, 2)
sFld = "AssistedPersonID Text,"
If Not IsNull(DLookup("[name]", "msysobjects", "[name]='newTblData'")) Then
CurrentDb.Execute "drop table newtblData"
End If
CurrentDb.Execute "create table newTblData( " & sFld & sPL & ")"
Set rs = CurrentDb.OpenRecordset("select distinct AssistedPersonID from AssistNew")
Set rsNew = CurrentDb.OpenRecordset("newtbldata")
rs.MoveFirst
Do Until rs.EOF
Set rs1 = CurrentDb.OpenRecordset("select * from AssistNew where AssistedPersonID='" & rs!AssistedPersonID & "'")
rsNew.AddNew
rsNew!AssistedPersonID = rs1!AssistedPersonID
j = 1
Do Until rs1.EOF
rsNew("AssistServiceCode" & j) = rs1!AssistServiceCode
rsNew("AssistProgramCode" & j) = rs1!AssistProgramCode
rsNew("AssistDateBegin" & j) = rs1!AssistDateBegin
j = j + 1
rs1.MoveNext
Loop
rsNew.Update
rs.MoveNext
Loop
rs.Close
rs1.Close
rsNew.Close
rsMax.Close
End Sub
|