I have the following loop. After I increment the counter, it sees EOF and doesn't calculate the time difference on the last record. I've tried several elseif statements, but to no avail.
Thanks for any help in advance.
BusMgr
Public Function CorrTimeDiff()
'
'This function is used when a record is deleted from the
'Operation form so that new Hours will be calculated
'automatically.
'
Dim db As Database
Dim rs As DAO.Recordset
Dim qdfMyQuery As QueryDef
Dim CurrTime As Date
Dim PrevTime As Date
Dim ctlOpsID As Long
Dim temtim As Single
Dim intctr As Integer
Dim strSQL As String
On Error GoTo Err_CorrTimeDiff
'Set DB = DBEngine.Workspaces(0).Databases(0)
Set db = CurrentDb()
Set qdfMyQuery = db.QueryDefs("qryReSortToTime"
' Create the recordset (or dynaset)
Set rs = qdfMyQuery.OpenRecordset()
' Test to see if there are any records
With rs
If Not rs.BOF And Not rs.EOF Then
intctr = 0 'Set increment counter to zero - correlates to first record
Do Until rs.EOF 'Initate loop
DoCmd.SetWarnings False 'Turn off warnings
.AbsolutePosition = intctr
PrevTime = rs(4) 'Set previous time to previous record time
.AbsolutePosition = intctr + 1
CurrTime = rs(4) 'Set current time to current record time
ctlOpsID = rs(2) 'Set OpsID to current record Operations ID
temtim = GetElapsedTime(Nz(CurrTime - PrevTime)) ' Calculate time difference
'Update the table with the time difference based on current Operations ID
strSQL = "UPDATE OperationsNew SET OperationsNew.OpsHrs = " & temtim & " WHERE OperationsNew.OpsID = " & ctlOpsID
DoCmd.RunSQL strSQL 'Run strSQL statement
intctr = intctr + 1 'Increment counter
Loop 'Loop
Else
'Message if there are records
MsgBox "There are no time records.", vbInformation, "Not Correcting Time Differential"
Exit Function
End If
End With
Bye_CorrTimeDiff:
Exit Function
Err_CorrTimeDiff:
Resume Bye_CorrTimeDiff
End Function
Thanks for any help in advance.
BusMgr
Public Function CorrTimeDiff()
'
'This function is used when a record is deleted from the
'Operation form so that new Hours will be calculated
'automatically.
'
Dim db As Database
Dim rs As DAO.Recordset
Dim qdfMyQuery As QueryDef
Dim CurrTime As Date
Dim PrevTime As Date
Dim ctlOpsID As Long
Dim temtim As Single
Dim intctr As Integer
Dim strSQL As String
On Error GoTo Err_CorrTimeDiff
'Set DB = DBEngine.Workspaces(0).Databases(0)
Set db = CurrentDb()
Set qdfMyQuery = db.QueryDefs("qryReSortToTime"
' Create the recordset (or dynaset)
Set rs = qdfMyQuery.OpenRecordset()
' Test to see if there are any records
With rs
If Not rs.BOF And Not rs.EOF Then
intctr = 0 'Set increment counter to zero - correlates to first record
Do Until rs.EOF 'Initate loop
DoCmd.SetWarnings False 'Turn off warnings
.AbsolutePosition = intctr
PrevTime = rs(4) 'Set previous time to previous record time
.AbsolutePosition = intctr + 1
CurrTime = rs(4) 'Set current time to current record time
ctlOpsID = rs(2) 'Set OpsID to current record Operations ID
temtim = GetElapsedTime(Nz(CurrTime - PrevTime)) ' Calculate time difference
'Update the table with the time difference based on current Operations ID
strSQL = "UPDATE OperationsNew SET OperationsNew.OpsHrs = " & temtim & " WHERE OperationsNew.OpsID = " & ctlOpsID
DoCmd.RunSQL strSQL 'Run strSQL statement
intctr = intctr + 1 'Increment counter
Loop 'Loop
Else
'Message if there are records
MsgBox "There are no time records.", vbInformation, "Not Correcting Time Differential"
Exit Function
End If
End With
Bye_CorrTimeDiff:
Exit Function
Err_CorrTimeDiff:
Resume Bye_CorrTimeDiff
End Function