访问 - 不使用 UNION 的 FULL OUTER JOIN,因此记录集是可更新的
如果我在 Access 中对子表单记录集使用 UNION,则它不允许使该记录集可更新。
问题是,我需要能够更新记录集。
我有两张表 - 一张是员工(员工)列表。另一个是记录每个员工当天关闭了多少张票。因此,该表 (INCTech) 使用 EmployeeID 和 Date 作为联合主键,因为每个员工在给定日期只能有一条记录。
我无法使用 LEFT OUTER JOIN 检索完整的员工列表,因为它将排除在 INCTech 中具有给定日期以外的日期记录的任何员工。这是行不通的:
SELECT INCTech.EmployeeID, INCTech.Assigned, INCTech.Closed, INCTech.Submitted,
INCTech.StillOpen, INCTech.TheDate, Employees.FirstName, Employees.LastName
FROM Employees LEFT JOIN INCTech ON Employees.EmployeeID = INCTech.EmployeeID
WHERE Employees.GroupID = 8 AND (INCTech.TheDate = #4/15/2011# OR
INCTech.TheDate IS NULL)
因为如果员工拥有与 2011 年 4 月 15 日不同的日期的记录,则他们不满足两个日期条件中的任何一个 - 日期不再为 NULL,但它也不是给定日期。
我的有效查询是:
SELECT Employees.EmployeeID, Employees.FirstName, Employees.LastName,
Employees.FirstName & " " & Employees.LastName AS FullName, INCTech.TheDate,
INCTech.Assigned, INCTech.Closed, INCTech.Submitted, INCTech.StillOpen,
Employees.GroupID
FROM Employees
LEFT OUTER JOIN INCTech ON
Employees.EmployeeID=INCTech.EmployeeID
WHERE (INCTech.TheDate)=Calendar.Value AND Employees.GroupID = ddlGroup.Value
UNION
SELECT Employees.EmployeeID, Employees.FirstName, Employees.LastName,
Employees.FirstName & " " & Employees.LastName AS FullName, NULL AS TheDate,
NULL AS Assigned, NULL AS Closed, NULL As Submitted, NULL As StillOpen,
Employees.GroupID
FROM Employees
LEFT OUTER JOIN INCTech ON Employees.EmployeeID=INCTech.EmployeeID
WHERE Employees.EmployeeID NOT IN
(SELECT INCTech.EmployeeID FROM INCTech WHERE INCTech.TheDate=Calendar.Value)
AND Employees.GroupID = ddlGroup.Value
但由于我必须使用 UNION 才能工作,因此生成的记录集是只读的。这是行不通的——我需要能够修改或添加每个员工的日常记录的数据。
帮助!
If I use UNION for a subform recordset in Access, it disallows making that recordset updateable.
The problem is, I need to be able to update the recordset.
I have two tables - One is a list of employees (Employees). The other is a record of how many tickets each employee closed for the day. As a result, that table (INCTech) uses EmployeeID and Date as a joint primary key, since each employee can only have one record for a given date.
I can't use a LEFT OUTER JOIN to retrieve a complete list of employees, because it will exclude any employee that has a record in INCTech for a date other than the given date. This doesn't work:
SELECT INCTech.EmployeeID, INCTech.Assigned, INCTech.Closed, INCTech.Submitted,
INCTech.StillOpen, INCTech.TheDate, Employees.FirstName, Employees.LastName
FROM Employees LEFT JOIN INCTech ON Employees.EmployeeID = INCTech.EmployeeID
WHERE Employees.GroupID = 8 AND (INCTech.TheDate = #4/15/2011# OR
INCTech.TheDate IS NULL)
Because if an employee has a record for a different date than 4/15/2011, they don't meet either of the two date criteria - Date isn't NULL anymore, but it's also not the given date.
My query that works is:
SELECT Employees.EmployeeID, Employees.FirstName, Employees.LastName,
Employees.FirstName & " " & Employees.LastName AS FullName, INCTech.TheDate,
INCTech.Assigned, INCTech.Closed, INCTech.Submitted, INCTech.StillOpen,
Employees.GroupID
FROM Employees
LEFT OUTER JOIN INCTech ON
Employees.EmployeeID=INCTech.EmployeeID
WHERE (INCTech.TheDate)=Calendar.Value AND Employees.GroupID = ddlGroup.Value
UNION
SELECT Employees.EmployeeID, Employees.FirstName, Employees.LastName,
Employees.FirstName & " " & Employees.LastName AS FullName, NULL AS TheDate,
NULL AS Assigned, NULL AS Closed, NULL As Submitted, NULL As StillOpen,
Employees.GroupID
FROM Employees
LEFT OUTER JOIN INCTech ON Employees.EmployeeID=INCTech.EmployeeID
WHERE Employees.EmployeeID NOT IN
(SELECT INCTech.EmployeeID FROM INCTech WHERE INCTech.TheDate=Calendar.Value)
AND Employees.GroupID = ddlGroup.Value
But since I have to use a UNION for that to work, the resulting recordset is read-only. This isn't going to work - I need to be able to modify or add data for each employee's daily record.
Help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用此查询作为其记录源构建一个表单:
向其中添加一个使用 INCTech 作为记录源的子表单。
使用 EmployeeID;TheDate 作为链接子字段和链接主字段。
选择“单一表单”作为子表单的默认视图。设置导航按钮 = 否。将循环属性设置为当前记录。
然后,对于主窗体中的任何员工记录,如果存在行,子窗体将显示匹配的 INCTech 行(同一员工、同一日期),或者允许您添加行(如果不存在)。
但您可能想要编辑今天以外日期的 INCTech 数据。在这种情况下,您可以将未绑定的文本框控件添加到主窗体的标题(例如 txtWhichDate),并允许用户输入日期值。在这种情况下,您可以调整主窗体的查询以引用文本框值而不是 Date()。
或者,您可以将代码添加到 txtWhichDate 的更新后事件中,以更新记录源以匹配。
Build a form with this query as its Record Source:
Add a subform to it which uses INCTech as it's Record Source.
Use EmployeeID;TheDate as the Link Child Fields and Link Master Fields.
Choose Single Form as the subform's Default View. Set Navigation Buttons = No. Set the Cycle property to Current Record.
Then for any Employee record in the main form, your subform will display the matching INCTech row (same employee, same date) if there is a row, or allow you to add a row if none exists.
But you will probably want to edit INCTech data for dates other than today. In that case you could add an unbound text box control to the main form's header, say txtWhichDate, and allow the user to input a date value. In that case you can adjust the main form's query to reference the text box value instead of Date().
Or you can add code to txtWhichDate's After Update event to update the Record Source to match.
我认为问题在于您的
WHERE
条件将 INCTech 表中的结果限制为与 Calendar.Value 匹配的记录。您可以尝试做的是创建一个单独的查询来从 INCTech 检索记录,并在查询中应用
WHERE INCTech.TheDate = Calendar.Value
子句。在主查询中,使用
LEFT JOIN
直接引用此查询,而不是直接引用 INCTech 表。这应该返回 Employee 的记录,并显示基于 INCTech 表的查询中存在匹配项的每个员工记录的相关详细信息。
I think the issue is that your
WHERE
condition is restricting the results from the INCTech table to those records that match Calendar.Value.What you could try doing is creating a separate query to retrieve the records from INCTech and apply the
WHERE INCTech.TheDate = Calendar.Value
clause in the query.In your main query, reference this query instead of the INCTech table directly by using a
LEFT JOIN
.This should return the records from Employee and show the relevant details for each employee record where there is a match in the query based on the INCTech table.
您将无法在记录集中包含多个表并使其可更新。您的表单应编辑员工记录表并作为子表单链接到 INCTech 表。
否则,您可以创建一个查询来显示联合和多个表的所有数据,但您必须在两个表上编写更新语句,以便在行更改时执行。您需要根据 EmployeeID 和 EmployeeID 以及 TheDate 或 INCTech 表筛选要更新的行。在 Access 中搜索更新多个表。
这将解决您的首要查询问题:
You're not going to be able to include multiple tables in a record set and make it updatable. Your form should edit the employee records table and link to the INCTech table as a subform.
Otherwise, you can create a query to display all of your data with the unions and multiple tables, but you'll have to code update statements on both tables to execute when a row is changed. You'll need to filter the row to update based on EmployeeID and EmployeeID and TheDate or the INCTech table. Do a search for updating multiple tables in Access.
This would solve your top query issue: