子表单不会随着新添加的数据而更新
当表单打开时(通过追加查询)添加数据时,我无法获取子表单以显示最新数据。子表单不会随着新添加的数据而更新
的表/表格的简单说明/ VBA &与问题相关的SQL:
我有记录我的部门内的团队,在团队提供的工作岗位和职位总数可供三个表每个角色。
的表是:
- 队:TeamID (AUTONUM,PK),TeamName (文本),CostCode (文本)
- 角色:角色ID ( AutoNum,PK),RoleDesc (Text),Abbrev (Text)
- Team_Composition:TeamID (民,PK),角色ID (民,PK),RoleCount (NUM)
的记录源为第e主要表格是队伍表。
的记录源的子窗体的查询,允许用户为每个团队每个角色在RoleCount字段中输入所需的数字:
SELECT Team_Composition.TeamID
, Roles.RoleDesc
, Roles.Abbrev
, Team_Composition.RoleCount
FROM Team_Composition INNER JOIN Roles ON Team_Composition.RoleID = Roles.RoleID
WHERE Team_Composition.TeamID=[Forms]![Edit_Teams]![cmbTeamName]
主窗体上的队名组合框从球队表加入<新团队>在列表中的第一个项目得到它的数据(SingleRecord表就是这样 - 用1场和1个记录表,因此选择将工作):
SELECT DISTINCT 0 AS TeamID
, '<New Team>' AS TeamName
FROM SingleRecord
UNION ALL SELECT TeamID
, TeamName
FROM Teams
ORDER BY TeamName
这一切都很好,当一切都已经存在打开窗体。我可以更改组合框中的值,并触发VBA代码移动到该记录并在子窗体中显示链接的数据。然后我可以为每个球队添加总数。
是移动到正确记录的代码如下:
'----------------------------------------------------------------------------------
' Procedure : cmbTeamName_AfterUpdate
' Author : Darren Bartrup-Cook
' Date : 12/06/2017
' Purpose : Keeps the details on the form in sync with the team selected in the combo box.
' Ensures all teams have all roles available to them by updating the team_composition
' table with new roles whenever the team is selected.
'-----------------------------------------------------------------------------------
Private Sub cmbTeamName_AfterUpdate()
'The first item in cmbTeamName is <New Team> which will not exist in the recordset.
'To avoid FindFirst going to the wrong record an attempt is made to create a new record
'allowing the form to filter to a non-existant record.
If cmbTeamName = 0 Then
DoCmd.GoToRecord , , acNewRec
Else
Dim rs As DAO.Recordset
Set rs = Me.RecordsetClone
rs.FindFirst "[TeamID]=" & cmbTeamName
If Not (rs.BOF And rs.EOF) Then
Me.Recordset.Bookmark = rs.Bookmark
End If
rs.Close
Set rs = Nothing
If cmbTeamName <> 0 Then
Update_TeamComposition cmbTeamName.Column(1)
End If
End If
End Sub
的Update_TeamComposition
程序执行SQL语句,以确保球队有多达可用角色的最新名单:
Private Sub Update_TeamComposition(TeamName As String)
With DoCmd
.SetWarnings False
.RunSQL "INSERT INTO Team_Composition(TeamID, RoleID) " & _
"SELECT TeamID, RoleID " & _
"FROM Teams, Roles " & _
"WHERE TeamID = (SELECT TeamID FROM Teams WHERE TeamName='" & TeamName & "')"
.SetWarnings True
End With
End Sub
现在为问题代码(或至少在哪里我认为问题是):
当一个新的茶m被添加到组合框中它被插入队伍表中并且各种角色也被添加到Team_Composition表中。这有效 - 我可以打开表格并查看其中的记录,但该表单拒绝更新并显示新记录。数据库ID显示1.表格底部的记录数显示记录1 of 6
,即使这是我添加的第7条记录 - 队表显示7条记录,Team_Composition表显示角色具有被添加到团队ID 7
的VBA添加一个新的团队下面是:
Private Sub cmbTeamName_NotInList(NewData As String, Response As Integer)
With DoCmd
.SetWarnings False
If cmbTeamName.OldValue = 0 Then
'A new team needs adding to the Team table.
.RunSQL "INSERT INTO Teams(TeamName) VALUES ('" & NewData & "')"
Response = acDataErrAdded
'The job roles for the team are inserted.
Update_TeamComposition NewData
Else
.RunSQL "UPDATE Teams SET TeamName = '" & NewData & "'" & _
"WHERE TeamID = " & cmbTeamName.Column(0)
Response = acDataErrAdded
End If
.SetWarnings True
End With
End Sub
我尝试添加代码只是Else
语句之前刷新形式 - Me.Refresh
,Me.Requery
, Me.Repaint
。
Me.Requery
和Me.Refresh
导致NotInList代码运行多次并最终给出run-time 2237 - The text you entered isn't an item in the list
(在Me.
行上)。 Me.Repaint
似乎没有做任何事情。
我想我已经包括了一切 - 有没有人知道如何在添加新团队时获得填充角色的子表单?对我来说,它看起来像表索引不更新,并且窗体不能识别新记录已创建。
编辑:
意见后从@ June7我已经更新了我NotInList
代码:通过删除WHERE
条款,允许形式
Private Sub cmbTeamName_NotInList(NewData As String, Response As Integer)
With DoCmd
.SetWarnings False
If Me.cmbTeamName.OldValue = 0 Then
'A new team needs adding to the Team table.
.RunSQL "INSERT INTO Teams(TeamName) VALUES ('" & NewData & "')"
Response = acDataErrAdded
'The job roles for the team are inserted.
Update_TeamComposition NewData
'To stop the Requery from making NotInList fire multiple times
'the combo box is moved to a team that does exist before the requery.
'Then it can move to the new record.
Me.cmbTeamName = Me.cmbTeamName.ItemData(0)
Me.Requery
Dim rs As DAO.Recordset
Set rs = Me.RecordsetClone
rs.FindFirst "[TeamName]='" & NewData & "'"
Me.Recordset.Bookmark = rs.Bookmark
rs.Close
Set rs = Nothing
Me.cmbTeamName.Requery
Me.cmbTeamName = CLng(Me.txtTeamID)
Else
.RunSQL "UPDATE Teams SET TeamName = '" & NewData & "'" & _
"WHERE TeamID = " & Me.cmbTeamName.OldValue
Response = acDataErrAdded
End If
.SetWarnings True
End With
End Sub
我还更新了SQL的子窗体利用主/子链接。
为什么绑定主窗体,如果你没有利用窗体/子窗体的主/子链接?子表单RecordSource具有引用组合框的过滤条件。那么,如果组合框的TeamID为0,则不存在关联的Team_Composition记录。建议您使用子表单容器的主/子链接属性,而不是查询中的动态筛选参数。我从不使用动态参数化查询。
在向两个表中添加新记录后,重新查询主窗体(它也应该同时重新查询子窗体)。但是,由于请求集关注于第一条记录,因此还需要移至刚刚在主表单上创建的记录(最后如果按TeamID排序),或者将排序顺序设置为TeamID DESCENDING
或使用RecordsetClone和Bookmark代码。
可以在没有SingleRecord表的组合框RowSource UNION查询中创建< New Team>行。
SELECT 0 As TeamID, "<New Team>" AS TeamName FROM Teams
UNION SELECT TeamID, TeamName FROM Teams ORDER BY TeamName;
感谢您的回复六月。你的第一段让我面对自己 - 当然,如果我要告诉它过滤到组合框,这可能是错误的,我的主/子连接不起作用。让我自己如此混乱,我忘记了主/子之间的自然联系。 –
有了这个链接的一点帮助:http://www.tek-tips.com/viewthread.cfm?qid=1433416我得到了你的第二段 - 它继续运行'NotInList'代码,直到我将组合框设置为第一个重新查询之前的值。然后,我必须重新查询组合框的新值以显示在列表中(在我已经要求整个表单之后)。 –
我不得不离开Union查询来查看SingleRecord - 当数据库为空(即没有团队)时,组合框不包含任何值。有一件事让我感到困惑的是,当我在几天前尝试将它与团队联系起来时,我可以发誓,它为团队中的每条记录返回了值,所以我有五六个。我的第一个想法是我使用'UNION ALL'可能会导致这样的结果(?),所以我使用'UNION'和'UNION ALL'进行测试,但得到的只有一个。尽管如此,如果没有团队,组合框就会变成空白点。 –