Option Compare Database
Private Sub Command0_Click()
'Clear out tblOutput
'-------------------
DoCmd.SetWarnings False
DoCmd.RunSQL "DELETE tblOutput.* FROM tblOutput"
DoCmd.SetWarnings True
'Insert P1 Opening as 0
'----------------------
Dim mySQL100 As String
mySQL100 = "INSERT INTO tblOutput ( Period, Activity, OutputValue ) "
mySQL100 = mySQL100 + "VALUES (1, 'Opening', 0)"
DoCmd.SetWarnings False
DoCmd.RunSQL mySQL100
DoCmd.SetWarnings True
'----------
'Start Loop
'----------
Dim myCounter As Integer
Dim myPeriod As Integer
For Counter = 1 To 9
myPeriod = 0 + Counter
'Insert Add and Take into tblOutput
'----------------------------------
Dim mySQL200 As String
mySQL200 = "INSERT INTO tblOutput ( Period, Activity, OutputValue ) "
mySQL200 = mySQL200 + "SELECT tblInput.Period, tblInput.Activity, tblInput.InputValue "
mySQL200 = mySQL200 + "FROM tblInput "
mySQL200 = mySQL200 + "WHERE (((tblInput.Period)=" & myPeriod & "))"
DoCmd.SetWarnings False
DoCmd.RunSQL mySQL200
DoCmd.SetWarnings True
'Calculate Closing Value and Insert Into tblOutput
'-------------------------------------------------
Dim mySQL300 As String
mySQL300 = "INSERT INTO tblOutput ( Period, Activity, OutputValue ) "
mySQL300 = mySQL300 + "SELECT tblOutput.Period, 'Closing' AS Activity, Sum(tblOutput.OutputValue) AS SumOfOutputValue "
mySQL300 = mySQL300 + "FROM tblOutput "
mySQL300 = mySQL300 + "GROUP BY tblOutput.Period, 'Closing'"
mySQL300 = mySQL300 + "HAVING (((tblOutput.Period) = " & myPeriod & "))"
DoCmd.SetWarnings False
DoCmd.RunSQL mySQL300
DoCmd.SetWarnings True
'Calculate Opening Value Base On Previous Period's Closing Value
'---------------------------------------------------------------
Dim mySQL400
mySQL400 = "INSERT INTO tblTemp ( Period, Activity, OutputValue ) "
mySQL400 = mySQL400 + "SELECT tblOutput.Period, tblOutput.Activity, tblOutput.OutputValue "
mySQL400 = mySQL400 + "FROM tblOutput "
mySQL400 = mySQL400 + "WHERE (((tblOutput.Period)=" & myPeriod & ") AND ((tblOutput.Activity)='Closing'))"
DoCmd.SetWarnings False
DoCmd.RunSQL mySQL400
DoCmd.SetWarnings True
Dim mySQL500
mySQL500 = "UPDATE tblTemp SET tblTemp.Period = " & myPeriod + 1 & ", tblTemp.Activity = 'Opening'"
DoCmd.SetWarnings False
DoCmd.RunSQL mySQL500
DoCmd.SetWarnings True
Dim mySQL600
mySQL600 = "INSERT INTO tblOutput ( Period, Activity, OutputValue ) "
mySQL600 = mySQL600 + "SELECT tblTemp.Period, tblTemp.Activity, tblTemp.OutputValue "
mySQL600 = mySQL600 + "FROM tblTemp"
DoCmd.SetWarnings False
DoCmd.RunSQL mySQL600
DoCmd.SetWarnings True
DoCmd.SetWarnings False
DoCmd.RunSQL "DELETE tblTemp.* FROM tblTemp"
DoCmd.SetWarnings True
Next Counter
End Sub