【发布时间】:2014-04-29 15:36:45
【问题描述】:
需要遍历日期记录以计算工作天数。日期的数量因人而异,可能少至 1 个,也可多至 11 个,以从序列中的最后一个日期到当前日期的计算结束。
数据示例:
Employee No Date
4522 08/11/2011
4522 09/06/2011
4522 05/04/2013
4522 07/15/2013
4522 01/31/2014
所以今天是 2014 年 4 月 29 日,所以从 01/31/2014 到 04/29/2014 是 88 天,从 05/04/2013 到 07/15/2013 是 72 天,从 08/11/2011 到2011 年 9 月 6 日是 26 天。将这些加在一起,您总共工作了 186 天。
同样,日期的数量在每种情况下都会有所不同。
这是我目前所拥有的......
Sub getActualEmployment(empID As Variant)
Dim strconnection, strSQL As String
Dim conn As ADODB.Connection
Dim tbl As ADODB.Recordset
Dim OptimumCode As String
Dim orghrdt As Date
Dim intNumDays As Integer
strSQL = "SELECT EmployeeID,EffectiveDate, OriginalHireDate FROM "
strSQL = strSQL & "EmploymentStatusChanges WHERE EmployeeID= '" & empID & "'"
strSQL = strSQL & " Order by EffectiveDate ASC"
Set tbl = New ADODB.Recordset
With tbl
Set .ActiveConnection = conn
.Source = strSQL
.LockType = adLockOptimistic
.CursorType = adOpenKeyset
.CursorLocation = adUseClient
.Open
End With
With tbl
On Error Resume Next
.MoveFirst
Do Until tbl.EOF
If tbl!OriginalHireDate = tbl!EffectiveDate Then
orghrdt = tbl!EffectiveDate
Else
intNumDays = CInt(DateValue(tbl!EffectiveDate) - orghrdt)
End If
.MoveNext
Loop
End With
End Sub
这是整个函数代码,希望这更有意义。
【问题讨论】:
标签: sql function loops ms-access vba