【发布时间】:2015-09-22 18:47:41
【问题描述】:
我希望有人可以在这里为我指出正确的方向,并希望我解释得足够清楚。我的报告得到了不理想的结果,因为我需要对计算字段求和。 Employee ID 标题中的 Employee Name、Week 标题中的标签以及 Details 标题中的 Dates 和 Calculated Hours 都正确显示。但是,Access 以错误的顺序填写信息。
这是它正在做的事情:
计算并填写期间最后一周最后一天的小时数 ->
填写期间最后一周的小时数 ->
填写期间的小时数 ->
计算并填写周期前一周的最后一天->
填写期间前一周的小时数 ->
重复直到第一周。
然后填写剩余的计算小时数。
所以我的结果显示如下:(请注意舍入是故意的,我不能只在页脚中执行 =Sum([Hours]) 的原因)
Employee Name
Date Hours Reg OT
9/8/15 8:00
9/9/15 8:28
9/10/15 8:00
9/11/15 7:32 <--Inputs this fourth
------------------------------
Week Totals: 7:30 7:30 0:00 <--Inputs this fifth then fills in other dates
Date Hours Reg OT
9/14/15 9:43
9/15/15 8:00
9/16/15 13:14
9/17/15 11:39
9/18/15 5:25 <--Starts here first
------------------------------
Week Totals: 5:30 5:30 0:00 <--Inputs this second
------------------------------
Period Totals: 5:30 5:30 0:00 <--Inputs this third
这是我现在设置报告的方式:
Employee Name (Employee ID Header)
Date Hours Regular Overtime (Week Header)
=[Date] =CalcHours([Hours]) (Details)
=GetWeekTot() =GetWeekReg() =GetWeekOT() (Week Footer)
=GetPeriodTot() =GetPeriodReg() =GetPeriodOT() (Employee ID Footer)
这是我的模块:
Option Compare Database
Option Explicit
Private WeekTot As Double
Private WeekReg As Double
Private WeekOT As Double
Private PeriodTot As Double
Private PeriodReg As Double
Private PeriodOT As Double
Public Function CalcHours(ByVal Hours As Double) As String
'Add the Daily Hours to the Total Weekly Hours
WeekTot = WeekTot + Hours
'Format and Return the Daily Hours
CalcHours = Int(Hours) & ":" & Format((Hours * 60) Mod 60, "00")
End Function
Public Function GetWeekTot() As String
'Round the Total Weekly Hours to the Nearest Quarter Hour
WeekTot = Round(WeekTot * 4, 0) / 4
'Calculate the Regular Weekly Hours and the Overtime Weekly Hours
If WeekTot > 40 Then
WeekReg = 40
WeekOT = WeekTot - 40
Else
WeekReg = WeekTot
WeekOT = 0
End If
'Add the Total Weekly Hours to the Total Period Hours
PeriodTot = PeriodTot + WeekTot
'Format and Return the Total Weekly Hours
GetWeekTot = Int(WeekTot) & ":" & Format(WeekTot * 60 Mod 60, "00")
'Reset the Total Weekly Hours
WeekTot = 0
End Function
Public Function GetWeekReg() As String
'Add the Regular Weekly Hours to the Regular Period Hours
PeriodReg = PeriodReg + WeekReg
'Format and Return the Regular Weekly Hours
GetWeekReg = Int(WeekReg) & ":" & Format(WeekReg * 60 Mod 60, "00")
'Reset the Regular Weekly Hours
WeekReg = 0
End Function
Public Function GetWeekOT() As String
'Add the Overtime Weekly Hours to the Overtime Period Hours
PeriodOT = PeriodOT + WeekOT
'Format and Return the Overtime Weekly Hours
GetWeekOT = Int(WeekOT) & ":" & Format(WeekOT * 60 Mod 60, "00")
'Reset the Overtime Weekly Hours
WeekOT = 0
End Function
Public Function GetPeriodTot() As String
'Format and Return Total Period Hours
GetPeriodTot = Int(PeriodTot) & ":" & Format((PeriodTot * 60) Mod 60, "00")
'Reset the Total Period Hours
PeriodTot = 0
End Function
Public Function GetPeriodReg() As String
'Format and Return Total Period Hours
GetPeriodReg = Int(PeriodReg) & ":" & Format((PeriodReg * 60) Mod 60, "00")
'Reset the Total Period Hours
PeriodReg = 0
End Function
Public Function GetPeriodOT() As String
'Format and Return Total Period Hours
GetPeriodOT = Int(PeriodOT) & ":" & Format((PeriodOT * 60) Mod 60, "00")
'Reset the Total Period Hours
PeriodOT = 0
End Function
如果有人能想出办法让它展示这一点,我将不胜感激:
Employee Name
Date Hours Reg OT
9/8/15 8:00
9/9/15 8:28
9/10/15 8:00
9/11/15 7:32
------------------------------
Week Totals: 32:00 32:00 0:00
Date Hours Reg OT
9/14/15 9:43
9/15/15 8:00
9/16/15 13:14
9/17/15 11:39
9/18/15 5:25
------------------------------
Week Totals: 48:00 40:00 8:00
------------------------------
Period Totals: 80:00 72:00 8:00
【问题讨论】:
-
你的计算代码没问题。有没有调用这些函数的代码?说,因为四舍五入不能使用页脚:你有没有考虑在页脚表达式中添加round函数?
-
@asdev 这些函数仅在报告的文本框中调用。一切正常执行,没有执行错误。起初我只是使用以下 Access 表达式来计算每日时间:=Int([Hours]) & ":" & Format([Hours]*60 Mod 60,"00") 和每周和期间时间是:= Int(Round(Sum([Hours])*4,0)/4) & ":" & Format((Round(Sum([Hours])*4,0)/4)*60 Mod 60,"00" )。然而;这导致了实际每周时间可能是 40:07 和 40:07 的情况,这两个时间都将四舍五入为 40:00,但期间总时间将四舍五入为 80:15,这是不正确的。
标签: vba ms-access report calculated-field