I have this code that runs on my SharePoint linked database that is used to calculate the length of certain tests. The issue is that the code takes a decent amount of time to run and whenever it runs there is a time in which the interface is unusable while the code runs. I have it set to run "on load" of a form, which is set to open when the database opens, the main switchboard. However whenever the user returns to this main screen the code runs and the database freezes until the code finishes. My question, is there anyway to have to code only run once per session or some way to make the code better and run faster?
Code is attached below.
Code is attached below.
Code:
Public Function FrequencyCalc()
Dim db As Database, rst1 As Recordset, rst2 As Recordset
Dim m_diff As Double
On Error GoTo FrequencyCalc_Error
Set db = CurrentDb
Set rst1 = db.OpenRecordset("qryTestDataDaysPerPhaseStatus", dbOpenDynaset)
Set rst2 = db.OpenRecordset("qryTestDataDaysPerPhaseStatus", dbOpenDynaset)
rst2.MoveNext
Do While Not rst1.EOF
If rst1!vehicleNumber = rst2!vehicleNumber Then
If Not rst2.EOF Then
m_diff = rst2!testDateTime - rst1!testDateTime
m_diff = Round(m_diff, 2)
If rst1!TestStatus = "Phase Complete" Or rst1!TestPurpose = "Vehicle Release" Then
m_diff = 0
End If
With rst1
.Edit
!Days = m_diff
.Update
rst2.MoveNext
.MoveNext
End With
End If
Else:
m_diff = Now() - rst1!testDateTime
m_diff = Round(m_diff, 2)
If rst1!TestStatus = "Phase Complete" Or rst1!TestPurpose = "Vehicle Release" Then
m_diff = 0
End If
With rst1
.Edit
!Days = m_diff
.Update
rst2.MoveNext
.MoveNext
End With
End If
If rst2.EOF Then
m_diff = Now() - rst1!testDateTime
m_diff = Round(m_diff, 2)
If rst1!TestStatus = "Phase Complete" Or rst1!TestPurpose = "Vehicle Release" Then
m_diff = 0
End If
With rst1
.Edit
!Days = m_diff
.Update
.MoveNext
End With
Exit Do
End If
Loop
rst1.Close
Set rst1 = Nothing
rst2.Close
Set rst2 = Nothing
db.Close
Set db = Nothing
FrequencyCalc_Exit:
Exit Function
FrequencyCalc_Error:
MsgBox Err & " : " & Err.Description, , "FrequencyCalc()"
Resume FrequencyCalc_Exit
End Function