I have a query that has a column with “true” or “false” in it.
I have vba code that loops through all the rows and finds the first “true” value in a column and saves certain info to a table based on that first row of the record set; it continues to loop until it finds the first “false” value and saves info from that row to the same table.
Now, I need to take the first record set of “true” values and loop through that record set and find the minimum and maximum value from a field in that record set, then update the table with those 2 amounts; then continue looping until the next record set of “true” values.
Here is the code I’m using to loop. This works well.
I have a function in a module to find min or max code, but it looks at the entire file and gives a min and max of the entire file, not just each record set.
The function code I'm using is from here:
http://allenbrowne.com/func-09.html
Can you tell me how to call the min-max function into the loop for each recordset, rather than the whole file?
Thank you.
I have vba code that loops through all the rows and finds the first “true” value in a column and saves certain info to a table based on that first row of the record set; it continues to loop until it finds the first “false” value and saves info from that row to the same table.
Now, I need to take the first record set of “true” values and loop through that record set and find the minimum and maximum value from a field in that record set, then update the table with those 2 amounts; then continue looping until the next record set of “true” values.
Here is the code I’m using to loop. This works well.
Code:
Private Sub CmdAnalyzeFridge_Click()
On Error GoTo Err_cmdTest_Click
Dim MyDB As DAO.Database
Dim rst As DAO.Recordset
Dim rstResults As DAO.Recordset
Dim intFldCtr As Integer
Dim blnFoundATrue As Boolean
Dim varEventID As Variant
Dim varPIN As Variant
Dim varColumnName As Variant
Dim varStartDate As Variant
Dim varStartTime As Variant
Dim varEndDate As Variant
Dim varEndTime As Variant
Dim intResponse As Integer
Dim varStartDateTime As Variant
'Dim varMin As Variant
'Dim varMax As Variant
blnFoundATrue = False
'DELETE all previous Results from the Results Table
CurrentDb.Execute "DELETE * FROM tblResults", dbFailOnError
Set MyDB = CurrentDb
Set rst = MyDB.OpenRecordset("qryFridge", dbOpenSnapshot)
Set rstResults = MyDB.OpenRecordset("tblResults", dbOpenDynaset)
DoCmd.Hourglass True 'Indicate activity, at least
With rst
For intFldCtr = 7 To .Fields.Count - 1 'Skip all Fields except 5 (flagged as true or false)
Do While Not .EOF
If .Fields(intFldCtr) = True Then
If Not blnFoundATrue Then '1st true Value not found, but Field is True
varEventID = .Fields(0)
varPIN = .Fields(2)
varStartDateTime = .Fields(1)
varColumnName = .Fields(intFldCtr).Name
varStartDate = Format(.Fields(1), "mm/dd/yyyy")
varStartTime = Format(.Fields(1), "hh:mm AM/PM")
'call Min and Max Module
mdl_MinMax.MinOfList
varMin = .Fields(3)
mdl_MinMax.MaxOfList
varMax = .Fields(3)
blnFoundATrue = True 'Found 1st True, Reset
End If '1st True was found, now need the 1st False
Else 'Field is False
If blnFoundATrue Then 'False Value with a True found
varEndDate = Format(.Fields(1), "mm/dd/yyyy")
varEndTime = Format(.Fields(1), "hh:mm AM/PM")
With rstResults
.AddNew
![EventID] = varEventID
![PIN] = varPIN
![Column] = varColumnName
![Start Date] = varStartDate
![Start Time] = varStartTime
![End Date] = varEndDate
![End Time] = varEndTime
![Diff (mins)] = DateDiff("n", varStartDateTime, rst.Fields(1))
![Diff2] = fCalcTime(DateDiff("n", varStartDateTime, rst.Fields(1)))
![MinTemp] = varMin
![MaxTemp] = varMax
.Update
End With
Debug.Print varMin & " " & varMax
'Debug.Print varEventID & " " & varColumnName & " " & " " & varStartDate & " " & _
varStartTime & " " & varEndDate & " " & varEndTime & " " & _
DateDiff("n", varStartTime, varEndTime) & " minutes"
blnFoundATrue = False 'RESET for next True Run - same Column
End If
End If
.MoveNext
Loop
'RESET Variables in preparation for next Column move
varEventID = Null
varPIN = Null
varColumnName = Null
varStartDate = Null
varStartTime = Null
varEndDate = Null
varEndTime = Null
varStartDateTime = Null
varMin = Null
varMax = Null
blnFoundATrue = False
.MoveFirst
Next
End With
DoCmd.Hourglass False
rstResults.Close
rst.Close
Set rst = Nothing
Set rstResults = Nothing
intResponse = MsgBox("View Results Table?", vbInformation + vbYesNo + vbDefaultButton1, "Results Table")
If intResponse = vbYes Then
DoCmd.OpenQuery "qryResults", acViewNormal, acReadOnly
DoCmd.Maximize
End If
Exit_CmdAnalyzeFridge_Click:
Exit Sub
Err_CmdAnalyzeFridge_Click:
DoCmd.Hourglass False
MsgBox Err.Description, vbExclamation, "Error in CmdAnalyzeFridge_Click()"
Resume Exit_CmdAnalyzeFridge_Click
End Sub
I have a function in a module to find min or max code, but it looks at the entire file and gives a min and max of the entire file, not just each record set.
The function code I'm using is from here:
http://allenbrowne.com/func-09.html
Can you tell me how to call the min-max function into the loop for each recordset, rather than the whole file?
Thank you.