Code:
Option Compare Database
Private Sub Command0_Click()
DoCmd.SetWarnings False
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Query = "SELECT MONK.Location, MONK.Status, MONK.Received, MONK.CircuitSize " & _
"FROM MONK " & _
"WHERE (((MONK.Status) = "" is above 65% threshold"")) " & _
"ORDER BY MONK.Location, MONK.Received;"
rs.Open Query, CurrentProject.Connection, adOpenKeyset, adLockBatchOptimistic
Dim rs1 As ADODB.Recordset
Set rs1 = New ADODB.Recordset
Query = "DELETE [Monk Report].* FROM [Monk Report];"
DoCmd.RunSQL Query
Dim loc, CircuitSize As String
Dim start_time, end_time As Date
loc = rs("Location")
While Not rs.EOF
loc = rs("Location")
CircuitSize = rs("CircuitSize")
start_time = rs("Received")
rs.MoveNext
If Not rs.EOF Then
If loc = rs("Location") Then
end_time = rs("Received")
Set rs1 = Nothing
Set rs1 = New ADODB.Recordset
Query = "SELECT MONK.Location, MONK.Status, MONK.Received " & _
"FROM MONK " & _
"WHERE (((MONK.Location)=""" & loc & """) AND ((MONK.Status)="" is below threshold"") AND ((MONK.Received)>#" & start_time & "# And (MONK.Received)<#" & end_time & "#));"
rs1.Open Query, CurrentProject.Connection, adOpenKeyset, adLockBatchOptimistic
If Not rs1.EOF Then
Query = "INSERT INTO [Monk Report] ( Location, CircuitSize, [Above Threshold], [Below Threshold] ) " & _
"SELECT """ & loc & """ AS Expr1, """ & CircuitSize & """ AS Expr2, #" & start_time & "# AS Expr3, #" & DateAdd("n", 10, rs1("received")) & "# AS Expr4;"
DoCmd.RunSQL Query
End If
Else
Set rs1 = Nothing
Set rs1 = New ADODB.Recordset
Query = "SELECT MONK.Location, MONK.Status, MONK.Received " & _
"FROM MONK " & _
"WHERE (((MONK.Location)=""" & loc & """) AND ((MONK.Status)="" is below threshold"") AND ((MONK.Received)>#" & start_time & "#));"
rs1.Open Query, CurrentProject.Connection, adOpenKeyset, adLockBatchOptimistic
If Not rs1.EOF Then
Query = "INSERT INTO [Monk Report] ( Location, CircuitSize, [Above Threshold], [Below Threshold] ) " & _
"SELECT """ & loc & """ AS Expr1, """ & CircuitSize & """ AS Expr2, #" & start_time & "# AS Expr3, #" & DateAdd("n", 10, rs1("received")) & "# AS Expr4;"
DoCmd.RunSQL Query
End If
End If
Else
Set rs1 = Nothing
Set rs1 = New ADODB.Recordset
Query = "SELECT MONK.Location, MONK.Status, MONK.Received " & _
"FROM MONK " & _
"WHERE (((MONK.Location)=""" & loc & """) AND ((MONK.Status)="" is below threshold"") AND ((MONK.Received)>#" & start_time & "#));"
rs1.Open Query, CurrentProject.Connection, adOpenKeyset, adLockBatchOptimistic
If Not rs1.EOF Then
Query = "INSERT INTO [Monk Report] ( Location, CircuitSize, [Above Threshold], [Below Threshold] ) " & _
"SELECT """ & loc & """ AS Expr1, """ & CircuitSize & """ AS Expr2, #" & start_time & "# AS Expr3, #" & DateAdd("n", 10, rs1("received")) & "# AS Expr4;"
DoCmd.RunSQL Query
End If
End If
Wend
DoCmd.OpenQuery "Format for Report", acViewNormal, acEdit
End Sub