T
Tim Ferguson
Let me just get my head round where we are at the moment, and then do
the code for putting in the records.
Okay: this should work. Put this code at the bottom of your module, again
below the last End Sub line:-
' CODE BEGINS HERE
'
' Read the DailySessions table and de-normalise it to
' get the weekly schedule for each resident
'
Public Sub FillTempTable(NewTableName As String)
' variables as usual
Dim db As Database ' currentdb
Dim rsDS As Recordset ' old DailySessions
Dim rsTT As Recordset ' new TempTable
Dim strSQL As String
' start as usual
Set db = CurrentDb()
' we need all the session allocations in order of resident
' and then by time of week
' Thinking about it, this could just be a querydef, but let's
' keep going for now
strSQL = "SELECT ds.fldDS_ResidentID, " & vbNewLine & _
"ds.fldDS_SessionTime, " & vbNewLine & _
"ds.fldDS_WorksiteID " & vbNewLine & _
"FROM tblDailySessions AS ds " & vbNewLine & _
" LEFT JOIN tblSessionLookup AS sl " & vbNewLine & _
" ON ds.fldDS_SessionTime = " & vbNewLine & _
" sl.fldSessionLookup " & vbNewLine & _
"ORDER BY ds.fldDS_ResidentID, " & vbNewLine & _
" sl.fldSessionSortNo; "
' this is another information window!
MsgBox strSQL
' get the recordset
Set rsDS = db.OpenRecordset(strSQL, dbOpenSnapshot, dbForwardOnly)
' open the new table
strSQL = "SELECT * FROM " & NewTableName & " WHERE FALSE;"
Set rsTT = db.OpenRecordset(strSQL, dbOpenDynaset)
' now go round each resident: outer loop
Do While Not rsDS.EOF
' create a new temp record and mark it with the resident
rsTT.AddNew
rsTT!fldResidentID = rsDS!fldDS_ResidentID
' go round each timeslot and put it in
' complex exit strategy needs to be done in order
Do While True
' end of input recordset: quit now
If rsDS.EOF Then Exit Do
' it's a different resident: finished with this temp record
If rsDS!fldDS_ResidentID <> rsTT!fldResidentID Then Exit Do
' okay, copy the worksite to the correct column
rsTT.Fields(rsDS!fldDS_SessionTime) = rsDS!fldDS_WorksiteID
' move to next daily session record
rsDS.MoveNext
Loop
' we have to save the temp record
rsTT.Update
' and go round again: we'll drop out of the top of the
' loop if that was the last resident.
Loop
rsTT.Close
rsDS.Close
' finished
End Sub
' *** CODE ENDS HERE
You call it by editing the bottom of the TryThis procedure, just below the
line you added last time. Look down the Public Sub TryThis code until you
get to the End If; and make rest of it look like this:
' Call a proc to create the new table
MakeTempTable c_strTempTable
' Call a proc to put in the records
FillTempTable c_strTempTable
End Sub
Incidentally, from your last post you seem to have a stray line in there:
if you see these here, they should be deleted:
'okay, run the command
db.Execute strSQL, dbFailOnError
Remember about those fingers this time!
All the best
Tim F