當表單打開時(通過追加查詢)添加數據時,我無法獲取子表單以顯示最新數據。子表單不會隨着新添加的數據而更新
的表/表格的簡單說明/ 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的子窗體利用主/子鏈接。
感謝您的回覆六月。你的第一段讓我面對自己 - 當然,如果我要告訴它過濾到組合框,這可能是錯誤的,我的主/子連接不起作用。讓我自己如此混亂,我忘記了主/子之間的自然聯繫。 –
有了這個鏈接的一點幫助:http://www.tek-tips.com/viewthread.cfm?qid=1433416我得到了你的第二段 - 它繼續運行'NotInList'代碼,直到我將組合框設置爲第一個重新查詢之前的值。然後,我必須重新查詢組合框的新值以顯示在列表中(在我已經要求整個表單之後)。 –
我不得不離開Union查詢來查看SingleRecord - 當數據庫爲空(即沒有團隊)時,組合框不包含任何值。有一件事讓我感到困惑的是,當我在幾天前嘗試將它與團隊聯繫起來時,我可以發誓,它爲團隊中的每條記錄返回了< New Team >值,所以我有五六個< New Team >。我的第一個想法是我使用'UNION ALL'可能會導致這樣的結果(?),所以我使用'UNION'和'UNION ALL'進行測試,但得到的只有一個< New Team >。儘管如此,如果沒有團隊,組合框就會變成空白點。 –