【问题标题】:How to set record limit on a child form then move to next record in parent form when child limit is reached?如何在子表单上设置记录限制,然后在达到子限制时移动到父表单中的下一条记录?
【发布时间】:2014-06-02 16:26:00
【问题描述】:

我正在创建 2003 年访问数据库,其中包含 2 个表,一个名为 Advice,另一个名为 AdviceDetails,具有一对多关系

我希望 [Advice]![AdviceNum] PK 字段的每条记录在表 [AdviceDetails]![AdviceNum] FK 中最多重复使用 30 次,在第 31 次输入时,用户将收到如下消息:"You have exceeded the maximum entries for this Advice"。我在 Advice 表中还有一个名为 ListTotal 的字段,我想捕获与每个 Advice. 相关的 AdviceDetails 的总数

我创建了一个名为 frmAdvice 的用户表单和一个名为 frmAdviceDetailsSub 的子表单

现在我希望每次将新记录添加到 frmAdviceDetailsSub. 时,都使用子表单的总记录集或记录计数来更新父表单中的 ListTotal

我想出了一个代码来捕获列表总数,但我不知道每次在子表单中创建新条目时会触发哪个事件:

    Dim rst As Object
    Set rst = Me.AdviceDetailsSub.Form.RecordsetClone
    On Error Resume Next
    rst.MoveLast
    On Error GoTo 0
    Me.ListTotal = rst.RecordCount

我知道我的问题有点复杂,但我希望它足够清楚以得到一些答案。谢谢:)

【问题讨论】:

  • 你为什么如此热衷于以这种方式对抗关系模型?
  • 如果我想让父/子自动更新,我只是按照我认为正确的方式来做。如果你有更好的方法请分享。我想在这个周末完成这项工作,所以请分享。谢谢
  • 不要再担心细节的最大数量了——那是一个红鲱鱼和不必要的复杂性。
  • 我知道它可能看起来没有必要,但它要求每个 Advice. 不超过 30 个如果我能弄清楚在子表单中的每条记录之间移动后触发了什么事件,那么我想我可能能够解决它。

标签: ms-access vba ms-access-2003


【解决方案1】:

在子表单的 AfterInsert 事件中,输入您指定的代码(我省略了错误句柄)。但是,您不能使用相对引用 (Me) 别名,因为您在子窗体上并且必须对主窗体进行绝对引用。

Private Sub Form_AfterInsert()    
    Dim rst As Object
    Set rst = Forms!frmAdvice!AdviceDetailsSub.Form.RecordsetClone    
    rst.MoveLast
    rst.MoveFirst

    Forms!frmAdvice!ListTotal = rst.RecordCount
    rst.Close
    Set rst = Nothing
End Sub

在子表单的BeforeUpdate事件中,检查ListTotal是否达到30。如果达到了,它会向用户发送关于允许的最大级别的消息并取消当前记录的自动保存.撤消会清除用户可能输入的任何更改。

Private Sub Form_BeforeUpdate(Cancel As Integer)
   If Forms!frmAdvice!ListBox >= 30 Then
      Msgbox "You have exceeded the maximum entries for this Advice.", vbInformation, "Maximum limit"
      Cancel = True
      Me.Undo
   End If
End Sub

【讨论】:

    【解决方案2】:

    经过大量时间的试验和故障排除后,我能够提出以下代码,并且运行良好。所以我分享是为了他人的利益。

    与 Parfait 一样,我已经排除了此示例的错误句柄。

    第一件事是在子表单中创建一个未绑定的文本框(本例DetailCount):frmAdviceDetailsSub 来存储记录集中的总记录数

    frmAdviceDetailsSub当前事件使用:

    Private Sub Form_Current()
    
        Dim rst As Object
        Dim RecSum As Integer    'Stores the sum of records in the recordset (yes, weird sounding variable lol)
    
        Set rst = Me.RecordsetClone
        rst.MoveLast        
        RecSum = rst.RecordCount
    
        'Because a new record will read (1 of 0) I had to make the following twist to get 1 of 1
        If Me.NewRecord Then
            Me.DetailCount = Me.CurrentRecord & " of " & (RecSum + 1)     'eg. 1 of 1
            If (RecSum + 1) > 30 Then
                MsgBox "You reached the maximum entries for this Advice", vbOKOnly, "Notice"
                Me.AllowAdditions = False    'PS. no need to undo just prevent addition as this test is before entry.
            End If
        Else
            Me.AllowAdditions = True
            Me.DetailCount = Me.CurrentRecord & " of " & RecSum
        End If
    
        If RecSum > 0 Then
            Me.Parent.ListTotal = RecSum    'to prevent this line from generating errors make the subform unbound then use the main form's on load event to assign it's recordsource. (child forms are loaded before parent)
        End If
        Me.Refresh
    End Sub
    

    对于frmAdvice On Load 事件使用:

    Private Sub Form_Load()
        Me.AdviceDetailsSub.SourceObject = "AdviceDetailsSub"
    End Sub
    

    享受:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-31
      • 1970-01-01
      • 2016-03-09
      • 1970-01-01
      • 2015-12-11
      • 1970-01-01
      相关资源
      最近更新 更多