R
Rob Hick
Hi all,
I was wondering whether anyone could advise me on speeding up the
following vba function. It calculates the number of days an amount of
stock would last if the inventory depletes by an average amount each
successive day. I've used an iterative procedure which reduces a
value by succesive amounts from a separate table and counts the number
of iterations until the value is less than zero. It will return a
value something like 10.45. It doesn't run terribly slowly but i
think it could be speeded up if i wasn't opening a new recordset for
the average values every time a new value is calculated. The function
will eventually be used to calculate values for 9 products per day so
the quicker the better. Function follows:
Public Function LiteralStockCover(TotalStock As Double, ByVal
BloodGroup As String, ByVal ProductGroupID As Integer, ByVal StockDate
As Date) As Single
Dim db As Database, rst As Recordset, strSQL As String, RunningTotal
As Double, LSC As Single, DayCount As Integer, _
sysJumpOut As Integer, IssueMonth As Variant, IssueDay As
Integer
On Error GoTo Err_Cleanup
IssueMonth = Format(StockDate, "yyyymm")
IssueDay = WeekDay(StockDate)
strSQL = "SELECT IssueDay, c" & BloodGroup & " FROM tbl_NominalLSC
" _
& "WHERE Month = " & IssueMonth & " AND
ProductGroupID=" & ProductGroupID
Set db = CurrentDb
Set rst = db.OpenRecordset(strSQL, dbOpenSnapshot, dbDenyWrite,
dbOptimistic)
sysJumpOut = 0
RunningTotal = TotalStock
DayCount = IssueDay
Do Until sysJumpOut = 1
rst.FindFirst "IssueDay = " & DayCount
If rst.NoMatch = True Then
MsgBox "no match - problem"
GoTo Err_Cleanup
End If
RunningTotal = RunningTotal - rst(1).Value
If RunningTotal >= 0 Then
LSC = LSC + 1
DayCount = DayCount + 1
If DayCount > 7 Then: DayCount = 1
Else
LSC = LSC + ((RunningTotal / rst(1).Value) + 1)
sysJumpOut = 1
End If
Loop
LiteralStockCover = LSC
Err_Cleanup:
If Err Then: MsgBox Err.Description
Set db = Nothing
Set rst = Nothing
End Function
Any help greatly appreciated
Rob
I was wondering whether anyone could advise me on speeding up the
following vba function. It calculates the number of days an amount of
stock would last if the inventory depletes by an average amount each
successive day. I've used an iterative procedure which reduces a
value by succesive amounts from a separate table and counts the number
of iterations until the value is less than zero. It will return a
value something like 10.45. It doesn't run terribly slowly but i
think it could be speeded up if i wasn't opening a new recordset for
the average values every time a new value is calculated. The function
will eventually be used to calculate values for 9 products per day so
the quicker the better. Function follows:
Public Function LiteralStockCover(TotalStock As Double, ByVal
BloodGroup As String, ByVal ProductGroupID As Integer, ByVal StockDate
As Date) As Single
Dim db As Database, rst As Recordset, strSQL As String, RunningTotal
As Double, LSC As Single, DayCount As Integer, _
sysJumpOut As Integer, IssueMonth As Variant, IssueDay As
Integer
On Error GoTo Err_Cleanup
IssueMonth = Format(StockDate, "yyyymm")
IssueDay = WeekDay(StockDate)
strSQL = "SELECT IssueDay, c" & BloodGroup & " FROM tbl_NominalLSC
" _
& "WHERE Month = " & IssueMonth & " AND
ProductGroupID=" & ProductGroupID
Set db = CurrentDb
Set rst = db.OpenRecordset(strSQL, dbOpenSnapshot, dbDenyWrite,
dbOptimistic)
sysJumpOut = 0
RunningTotal = TotalStock
DayCount = IssueDay
Do Until sysJumpOut = 1
rst.FindFirst "IssueDay = " & DayCount
If rst.NoMatch = True Then
MsgBox "no match - problem"
GoTo Err_Cleanup
End If
RunningTotal = RunningTotal - rst(1).Value
If RunningTotal >= 0 Then
LSC = LSC + 1
DayCount = DayCount + 1
If DayCount > 7 Then: DayCount = 1
Else
LSC = LSC + ((RunningTotal / rst(1).Value) + 1)
sysJumpOut = 1
End If
Loop
LiteralStockCover = LSC
Err_Cleanup:
If Err Then: MsgBox Err.Description
Set db = Nothing
Set rst = Nothing
End Function
Any help greatly appreciated
Rob