【问题标题】:How to Sum Calculated Fields with VBA on MS Access Report如何在 MS Access 报告中使用 VBA 对计算字段求和
【发布时间】: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


【解决方案1】:

由于您透露在报表的文本框中调用了函数,因此可以说您永远无法确定在初始化报表时 Access 调用它们的顺序。

我提出了另外两种可能性:

1.使用带有额外字段的表达式

在您的报告详细信息行中设置一个实际每日小时的字段,将其命名为例如DAILY_CORRECT_FIELD,将其隐藏设置为 false。插入另一个文本字段,将其命名为例如DAILY_ROUNDED_FIELD,在其中插入四舍五入表达式。在一周的总和字段上,参考 sum(DAILY_CORRECT_FIELD),不可见字段,不取整。

2。基于代码

如果您想使用 VBA,请不要编写对所有字段求和的模块,尤其是当您无法控制调用函数的顺序时。写一个查询来总结一周并将其设置到报告上的字段。网上有很多教程,我就不多解释了。

虽然我是使用 VBA 的忠实拥护者,但我强烈推荐建议 1,因为它的实现简单。

【讨论】:

  • 大声笑直到我​​想出一个解决方案后才看到这篇文章,但你的第二个解决方案也将我带到了我最终结束的地方。然而;我没有使用 VBA。
【解决方案2】:

我想出了一个解决方案,@asdev,你让我再次考虑使用访问表达式,但这次我添加了一种绕过聚合查询来按员工和周分组,然后将总和四舍五入查询给我的总数与我在报告的周页脚中获得的总数相同。然后,我使用 DSum 函数按员工 ID 过滤绕过查询,并在我的 Period 页脚中求和总和,给我正确的总和。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-23
    相关资源
    最近更新 更多