简体   繁体   English

从Access 2010中的子表单获取小计的问题

[英]Problems getting a subtotal from a subform in Access 2010

I've got a form (frmNewHours) with some controls on it which I use to alter the subform (tblHoursSubform), which is a datasheet view subform containing 2 visible columns for the user (Project and MTDHours). 我有一个窗体(frmNewHours),上面有一些控件,可用来更改子窗体(tblHoursSubform),这是一个数据表视图子窗体,其中包含2个对用户可见的列(Project和MTDHours)。 I want the subtotal of the MTDHours column displayed in an unbound field on my main form. 我希望在主窗体的未绑定字段中显示MTDHours列的小计。

What I tried first: 我首先尝试的是:

  • I put an unbound control (Subtotal) in the main form 我在主表单中放置了一个未绑定的控件(小计)
  • In the Form Footer of the subform I put an unbound control (HoursSubtotal) with a control source =Sum([MTDHours]) 在子表单的表单页脚中,我放置了一个未绑定的控件(HoursSubtotal),其控件来源为= Sum([MTDHours])
  • In the Subtotal control on the main form, I set it's control source to =[tblHoursSubform]![HoursSubtotal] 在主窗体的小计控件中,我将其控件源设置为= [tblHoursSubform]![HoursSubtotal]

This worked perfectly at first, but as I continued to develop the form (maybe irrelevant details, but I use a couple controls on the main form to change the recordsource for the subform and the recordsource of the Project combobox control in the subform) the subtotal display stopped working. 首先,这很完美,但是随着我继续开发表单(也许无关紧要的细节,但是我在主表单上使用了几个控件来更改子表单的记录源和子表单中Project组合框控件的记录源)小计显示器停止工作。 I deleted the subtotal controls and recreated them the exact same way and it worked again, which didn't really make sense to me but I continued on. 我删除了小计控件,然后以完全相同的方式重新创建了它们,然后再次起作用,这对我来说真的没有意义,但我继续。

I finished developing the form and tested it on another computer, and the subtotal field stopped working again (the subform's control is erroring for some reason) 我完成了表单的开发,并在另一台计算机上对其进行了测试,小计字段再次停止工作(由于某种原因,子表单的控件出现错误)

What I tried next: 我接下来尝试的是:

  • I wrote a function that queries and returns the subtotal, and changed the control source to =GetHoursSubtotal(GetUserID(),[SelectedSession]) 我编写了一个查询并返回小计的函数,并将控制源更改为= GetHoursSubtotal(GetUserID(),[SelectedSession])

This works when the form is loaded, but as data in the subform changes the subtotal does not update, so I added the following code to the subform: 这在加载表单时有效,但是随着子表单中数据的更改,小计不会更新,因此我在子表单中添加了以下代码:

Private Sub Form_Current()
    Me.Parent.Controls("Subtotal").Requery
End Sub

While this works, it takes about a full second to refresh the subtotal control, during which the control is empty. 在此过程中,刷新小计控件大约需要一整秒钟,在此期间控件为空。 So while it's functional, it's definitely not working as intended. 因此,尽管功能正常,但绝对无法按预期工作。

So now I'm at a loss for what to try next. 所以现在我不知所措。 Has anyone else experienced this and found a solution? 其他人有没有遇到过这种情况并找到了解决方案?

EDIT: Got it working. 编辑:得到它的工作。 I went back to the original solution somewhat, but I changed the subtotal's control source to =Nz(Sum([MTDHours]),0) and now no more problems. 我稍微回到了原始解决方案,但是我将小计的控制源更改为= Nz(Sum([MTDHours]),0),现在不再有问题。

In the 1st attempt seems that when you changed the recordsource for subform, something got lost in the properties of the Sum control. 第一次尝试是,当您更改子窗体的记录源时,Sum控件的属性中有些丢失。 Really I can't understand why it doesn't work on another computer. 真的我不明白为什么它不能在另一台计算机上工作。 It makes no sense! 这个不成立! When you requery the subform you update the sum control. 当您重新查询子窗体时,您将更新求和控件。 You should also requery the main form to get the new value. 您还应该重新查询主窗体以获取新值。

The 2nd solution is surely slower cause you are using a custom function instead of an embedded one. 第二种解决方案肯定较慢,因为您使用的是自定义函数而不是嵌入式函数。 If it's a simple sum you can try to use the DSum function, filtering the recordset with the ID of the main form if required. 如果是简单的总和,则可以尝试使用DSum函数,并根据需要使用主窗体的ID过滤记录集。 You can assign the DSum function to the subtotal control of the main form. 您可以将DSum函数分配给主窗体的小计控件。 Again after requerying the sub you should have to requery the main. 同样,在重新查询子程序之后,您还必须重新查询主程序。

Note that current should not be the right event 'cause you're forcing to update at each record (this doesn't matter if the form is not a continuous form). 请注意,当前不应该是正确的事件,因为您被迫在每条记录上进行更新(如果表单不是连续表单,则无关紧要)。 It would be better to update the subtotal when you fire the update subform event. 当您触发update subform事件时,最好更新小计。

Let me know if it worked. 让我知道它是否有效。

Bye 再见

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM