如何在 MS Access 连续表单中引用各个表单元素

发布于 2024-09-28 19:43:49 字数 959 浏览 1 评论 0原文

MS Access 场景:

我使用一个表单(我们将其称为 select_contract 表单)和一个子表单(我们将其称为 employee_allocations_by_contract 子表单)。

select_contract 形式:

  1. 未绑定。
  2. 包含一个组合框,允许用户选择合同 ID。

employee_allocations_by_contract 子表单:

  1. 是一个连续表单。
  2. 绑定到一个表(我将其称为 hours_allocation 表),该表包含每个员工分配给每个合同的小时数。
  3. 仅将一个字段(employee_id 字段)绑定到 hours_allocation 表。此字段的行源是一个查询,该查询返回已分配给用户在 select_contract 表单中选择的合同的工作时间的员工 ID。
  4. 包含其他十二个未绑定字段,一个代表一年中的每个月。这些字段旨在显示一年中每个月分配给 employee_id 字段中列出的员工的小时数。

问题:为了按月显示分配给 employee_allocations_by_contract 子表单中列出的每个员工的小时数,每个月字段的查询需要访问 employee_id它们出现的行的字段。我一直无法弄清楚如何引用该字段。由于 employee_allocations_by_contract 子表单是连续表单,因此对 employee_id 字段名称的引用似乎引用了表单上的每一行。

我搜索了几个论坛并发现了相关的连续表单问题,但没有任何明确解决此问题的方法。有什么想法吗?

MS Access Scenario:

I am using a form (we'll call it the select_contract form) and a subform (we'll call it the employee_allocations_by_contract subform).

The select_contract form:

  1. Is unbound.
  2. Contains a combobox that allows the user to select a contract id.

The employee_allocations_by_contract subform:

  1. Is a continuous form.
  2. Is bound to a table (which I'll call the hours_allocation table) that contains the number of hours that each employee is allocated to each contract.
  3. Binds only one field, the employee_id field, to the hours_allocation table. The row source for this field is a query that returns the ids of employees that have hours allocated to the contract that the user has selected in the select_contract form.
  4. Contains twelve other, unbound fields, one for each month of the year. These fields are intended to display the number of hours allocated to the employee listed in the employee_id field for each month of the year.

The Problem: In order to display the number of hours by month that are allocated to each employee listed on the employee_allocations_by_contract subform, the queries for each month field need access to the employee_id field of the row on which they appear. I haven't been able to figure out how to reference this field. Because the employee_allocations_by_contract subform is a continuous form, a reference to the the employee_id field name appears to reference each row on the form.

I have searched several forums and have found related continuous form issues, but nothing that clearly addresses this problem. Any thoughts?

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

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

发布评论

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

评论(3

煮茶煮酒煮时光 2024-10-05 19:43:49

那么,您可以为需要总计的每个月份列构建一个子查询。

例如:

Select id, employee_ID, bla, bla, bla, 
     (select sum(hours) where month = 1 and year = 2010 and 
       employee_id = ehours.Employee_id from tblProjectHours)
 as month1,
     (select sum(hours) where month = 2 and year = 2010 and 
      employee_id = ehours.Employee_id from tblProjectHours) 
 as month2,
      (select sum(hours) where month = 3 and year = 2010 and 
       employee_id = ehours.Employee_id from tblProjectHours)
  as month3 
  etc. for each column needed

来自 ehours

注意,我使用月份和年份作为列,您可能必须使用月份([SomeDate])= 2 和年份([SomeDate])= 2010,但您明白了。

此外,虽然您无法寻址每一行并将值填充到未绑定的文本框中,但实际上您可以将文本框绑定到返回所需值/表达式的函数。

txtBoxMonth1                               txtboxMonth2              etc.
=MyMonth(1,[employee_id])                  =MyMonth(2,[Employee_id])

然后,在表单中,您有一个名为

Public Function MyMonth(intMonth as interger, lngEMPID as long) as long

      ' code here to get total based on employee id and month

End Funciton

So 的公共函数,您可以将函数绑定到这些文本框,并且当前行 emp ID 可以驱动每个文本框显示的内容。请记住,该函数应该预处理所有数据,并且您不希望为每个函数调用重新运行并重新加载数据,因为那样会太慢。然而,我使用这种方法取得了很大的成功。因此,表单的每一行只有一个实际列 (empID),其余列源自绑定到上述公共函数的文本框,其中月份与 emp id 一起传递以返回该值柱子。

所以,上面是我成功使用的两种可能的方法。

Well, you could build a sub-query for each of the month columns you need that total for.

Something like:

Select id, employee_ID, bla, bla, bla, 
     (select sum(hours) where month = 1 and year = 2010 and 
       employee_id = ehours.Employee_id from tblProjectHours)
 as month1,
     (select sum(hours) where month = 2 and year = 2010 and 
      employee_id = ehours.Employee_id from tblProjectHours) 
 as month2,
      (select sum(hours) where month = 3 and year = 2010 and 
       employee_id = ehours.Employee_id from tblProjectHours)
  as month3 
  etc. for each column needed

from ehours

Note I used month and year as columns and you likely have to use month([SomeDate]) = 2 and year([SomeDate]) = 2010, but you get the idea.

Also, while you can't address each row and stuff a value into that un-bound text box, you CAN in fact bind the text box to a function that returns the value/expression you need.

txtBoxMonth1                               txtboxMonth2              etc.
=MyMonth(1,[employee_id])                  =MyMonth(2,[Employee_id])

And, then in the form, you have a public function called

Public Function MyMonth(intMonth as interger, lngEMPID as long) as long

      ' code here to get total based on employee id and month

End Funciton

So, you can bind a function to those text boxes, and the current row emp ID can thus drive what each text box displays. Just remember that this function should have all of the data pre-processed, and you don't want to have to re-run and re-load the data for each function call as that will be FAR too slow. However, I have used this approach to much success. So, each row of the form only has one real column (empID), the rest of the columns are derived from text boxes bound to the public function as per above with the month being passed along with the emp id to return the value for that column.

So, above is two possible approaches I used with success.

秋心╮凉 2024-10-05 19:43:49

连续表单上的未绑定控件只能引用具有焦点的记录。因此,如果将数据输入到未绑定的控件中,您将看到连续表单中每条记录的相同数据。

An unbound control on a continuous form can only refer to the record that has the focus. There fore, if data is entered into an unbound control, you will see the same data for every record in the continuous form.

空宴 2024-10-05 19:43:49

您不能在 MS Access 连续表单中“单独”(“在行级别”)引用控件。

这里唯一的解决方案是将这些控件绑定到基础记录集\记录源,其中相应的字段保存要显示的值。

例如,如果您想将时间段显示为“日期输入”和“日期输出”之间的差异,您的记录集\记录源可能类似于:

select id_person,dateIn,dateOut,dateDiff(xx,dateOut, dateIn) as timeIn from ...

然后您的时间计算控件必须绑定到“timeIn”记录集的字段。

(*) 请检查 dateDiff 参数。我这里没有任何帮助..

You cannot reference controls "individually" ("at the line level") in a MS Access continuous form.

Your only solution here is to bound these controls to an underlying recordset\recordsource where corresponding fields hold the values to be displayed.

For example, if you want to display a time period as the difference between a "date in" and a "date out", your recordset\recorsource could be something like:

select id_person,dateIn,dateOut,dateDiff(xx,dateOut, dateIn) as timeIn from ...

Then your time calculation control has to be bound to the 'timeIn' field of the recordset.

(*) please check the dateDiff arguments. I do not have any help available here ..

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