Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ms access 如何在子窗体上设置记录限制,然后在达到子限制时移动到父窗体中的下一个记录?_Ms Access_Vba_Ms Access 2003 - Fatal编程技术网

Ms access 如何在子窗体上设置记录限制,然后在达到子限制时移动到父窗体中的下一个记录?

Ms access 如何在子窗体上设置记录限制,然后在达到子限制时移动到父窗体中的下一个记录?,ms-access,vba,ms-access-2003,Ms Access,Vba,Ms Access 2003,我正在创建2003 access数据库,其中包含两个表,一个表名为Advice,另一个表名为AdviceDetails,具有一对多关系 我想要每一张现场记录[建议]![AdviceNum]主键将在表[AdviceDetails]中重用![AdviceNum]FK最多30次,在第31次输入时,用户将收到如下消息:您已超过此建议的最大输入。我在表Advice中还有一个名为ListTotal的字段,我想捕获与每个建议相关的总AdviceDetails 我创建了一个名为frmAdvice的用户表单,其中

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

我想要每一张现场记录[建议]![AdviceNum]主键将在表[AdviceDetails]中重用![AdviceNum]FK最多30次,在第31次输入时,用户将收到如下消息:您已超过此建议的最大输入。我在表Advice中还有一个名为ListTotal的字段,我想捕获与每个建议相关的总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
我知道我的问题有点复杂,但我希望它足够清楚,可以得到一些答案。谢谢:

在子窗体的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

经过长时间的试验和故障排除,我终于想出了下面的代码,它工作得很好。所以我是为了他人的利益而分享

像Parfait一样,我排除了本例中的错误句柄

第一件事是在子窗体中创建一个未绑定的文本框this case 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
对于FRM设备加载事件,请使用:

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

享受:

为什么你如此热衷于用这种方式来对抗关系模型?如果我想自动更新父/子模型,我只是用我认为正确的方式来做。如果你有更好的方法,请分享。我想在这个周末完成这件事,所以请分享。谢谢,不要担心细节的最大数量——这是一个转移视线的问题,也是一个不必要的复杂问题。我知道这可能是不必要的,但要求每个建议不超过30条。如果我能找出在子窗体中的每个记录之间移动后触发的事件,那么我想我可能能够解决它。