根据当前记录计算子表单的控制

发布于 2024-07-21 16:16:03 字数 545 浏览 9 评论 0原文

我有以下内容: 来自“客户”表的主窗体“客户”。 生成“发票”子表单,其中包含“发票”表中的“发票日期”、“发票金额”、“客户 ID”等字段

每当用户单击或转到“发票”子表单中的记录时,都会 。 我想要一个“到目前为止总计”控件来计算“发票金额”的总和,直到“单击”或选择当前记录的日期为止。

即对于带有发票的客户微软: 1) 2009 年 5 月 2 日,150 美元 2) 2009 年 5 月 3 日,200 美元 3) 09 年 5 月 4 日,$500

如果用户点击记录 2),“迄今为止总计”应显示 $350 如果用户点击记录 1),“迄今为止总计”应显示 $150 如果用户单击记录 3),“迄今为止总计”应显示 $850

目前,我正在子表单“发票”中的事件“OnCurrent”上使用 DSum 函数来设置“迄今为止总计”值。 这种方法是否缓慢、低效?

还有其他更简单、更干净、更优雅、更快、更高效的使用 ms access 功能的方法吗?

我希望“发票”子表单显示该客户的所有发票,无论单击哪条记录。

I have the following:
main form "customer" from a "customer" table.
subform "invoices" with fields "invoice date", "invoice amount" "customer id" etc. from a table "invoices"

whenever user clicks or goes to a record in the "invoices" sub form.
I would like a "total so far" control to calculate the sum of the "invoices amount" up until the date of the current record being "clicked" or selected.

i.e. for customer microsoft with invoices:
1) may 2 09, $150
2) may 3 09, $200
3) may 4 09, $500

If user clicks on record 2), "total so far" should show $350
If user clicks on record 1), "total so far" should show $150
If user clicks on record 3), "total so far" should show $850

Currently, I am using DSum function on an event "OnCurrent" in the subform "invoices" to set the "total so far" value. Is this method slow, inefficient?

Any other simpler,cleaner,more elegant,faster, efficient method using ms access features?

I want the "invoices" subform to show ALL the invoices for this customer no matter which record is clicked.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紫轩蝶泪 2024-07-28 16:16:03

如果 DSum 方法适合您,则使用它。

如果它太慢,那么另一种方法是使用记录集克隆并循环记录。
这是更多的代码,但效率更高,因为它不必访问数据库。 您确实需要一个唯一的密钥。

Private Sub Form_Current()

  Dim rst As DAO.Recordset
  Dim subTotal As Currency
  Dim rec_id As Long

  'get clone of current records in subform'
  Set rst = Me.RecordsetClone

  'save current record id'
  rec_id = Me.rec_id

  rst.MoveFirst

  'loop and total until current is reached'
  Do Until rst![rec_id] = rec_id
    subTotal = subTotal + rst![InvoiceAmt]
    rst.MoveNext
  Loop

  'add last amount on current record' 
  subTotal = subTotal + rst![InvoiceAmt]

  Set rst = Nothing

  'set text box with subtotal'
  Me.Text2 = subTotal

End Sub

另一种方法是使用 sum() 构建 sql 查询,但这需要更多代码并再次访问数据库。

If the DSum method works for you then use it.

If it's too slow then another way is to use a recordsetclone and loop through the records.
This is more code but it's more efficient since it doesn't have to hit the database. You do need a unique key.

Private Sub Form_Current()

  Dim rst As DAO.Recordset
  Dim subTotal As Currency
  Dim rec_id As Long

  'get clone of current records in subform'
  Set rst = Me.RecordsetClone

  'save current record id'
  rec_id = Me.rec_id

  rst.MoveFirst

  'loop and total until current is reached'
  Do Until rst![rec_id] = rec_id
    subTotal = subTotal + rst![InvoiceAmt]
    rst.MoveNext
  Loop

  'add last amount on current record' 
  subTotal = subTotal + rst![InvoiceAmt]

  Set rst = Nothing

  'set text box with subtotal'
  Me.Text2 = subTotal

End Sub

The other way is to build a sql query with a sum() but that takes even more code and hits the database again.

深者入戏 2024-07-28 16:16:03

您可以在子窗体的页脚中放置一个带有 Dsum 的隐藏控件,然后从主窗体中引用该控件。
Dsum 将有其第三个参数,例如 "InvoiceId <= " & InvoiceId

在这种情况下不需要任何 VBA/事件。

You could put a hidden control with a Dsum in the footer of the subform, and then refer to that one from the main form.
The Dsum would have its 3rd argument like "InvoiceId <= " & InvoiceId

No need for any VBA/event in that case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文