2017-06-13 148 views
1

當表單打開時(通過追加查詢)添加數據時,我無法獲取子表單以顯示最新數據。子表單不會隨着新添加的數據而更新

的表/表格的簡單說明/ VBA &與問題相關的SQL:
我有記錄我的部門內的團隊,在團隊提供的工作崗位和職位總數可供三個表每個角色。

的表是:

  • :TeamID (AUTONUM,PK),TeamName (文本),CostCode (文本)
  • 角色:角色ID ( AutoNum,PK),RoleDesc (Text),Abbrev (Text)
  • Team_Composition:TeamID (民,PK),角色ID (民,PK),RoleCount (NUM)

形式是如下面TeamID連接主/兒童字段:
enter image description here

記錄源爲第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代碼移動到該記錄並在子窗體中顯示鏈接的數據。然後我可以爲每個球隊添加總數。 enter image description here

是移動到正確記錄的代碼如下:

'---------------------------------------------------------------------------------- 
' 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 enter image description here

的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.RefreshMe.RequeryMe.Repaint

Me.RequeryMe.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的子窗體利用主/子鏈接。

回答

1

爲什麼綁定主窗體,如果你沒有利用窗體/子窗體的主/子鏈接?子表單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; 
+0

感謝您的回覆六月。你的第一段讓我面對自己 - 當然,如果我要告訴它過濾到組合框,這可能是錯誤的,我的主/子連接不起作用。讓我自己如此混亂,我忘記了主/子之間的自然聯繫。 –

+0

有了這個鏈接的一點幫助:http://www.tek-tips.com/viewthread.cfm?qid=1433416我得到了你的第二段 - 它繼續運行'NotInList'代碼,直到我將組合框設置爲第一個重新查詢之前的值。然後,我必須重新查詢組合框的新值以顯示在列表中(在我已經要求整個表單之後)。 –

+0

我不得不離開Union查詢來查看SingleRecord - 當數據庫爲空(即沒有團隊)時,組合框不包含任何值。有一件事讓我感到困惑的是,當我在幾天前嘗試將它與團隊聯繫起來時,我可以發誓,它爲團隊中的每條記錄返回了< New Team >值,所以我有五六個< New Team >。我的第一個想法是我使用'UNION ALL'可能會導致這樣的結果(?),所以我使用'UNION'和'UNION ALL'進行測試,但得到的只有一個< New Team >。儘管如此,如果沒有團隊,組合框就會變成空白點。 –

相關問題