粘贴子窗体中的多个记录时的子窗体校准错误

时间:2017-06-29 08:01:48

标签: ms-access events subform

我对子窗体上的所有三个事件进行计算以更新主窗体:

Private Sub Form_AfterDelConfirm(Status As Integer)
  Me.Parent.UpdateStunden
End Sub

Private Sub Form_AfterInsert()
  Me.Parent.UpdateStunden
End Sub

Private Sub Form_AfterUpdate() 
  Me.Parent.UpdateStunden
End Sub

Public Sub UpdateStunden(Optional BeforeUpdateEvent As Boolean = False)
On Error GoTo ErrorHandler
Dim rst As Recordset
Dim sql As String
Dim NewStunden As Variant

    If Me.NewRecord Then Exit Sub

    sql = _
    "SELECT Sum(Stunden) AS SumStunden " & _
    "FROM Tätigkeiten " & _
    "WHERE Tätigkeitsdatum = #" & Format(Me!Tätigkeitsdatum, "yyyy-mm-dd") & "#;"

    Set rst = CurrentDb().OpenRecordset(sql, dbOpenSnapshot)

    If Not rst.EOF Or Not rst.BOF Then
        NewStunden = rst!SumStunden
        If Nz(NewStunden) <> Nz(Me.Stunden) Or IsNull(Me.Stunden) Then
            Me.Stunden = NewStunden
        End If
    End If

ExitPoint:
    On Error Resume Next
    If Me.Dirty And Not BeforeUpdateEvent Then Me.Dirty = False
    rst.Close
    Set rst = Nothing
    Exit Sub

ErrorHandler:
    Select Case Err
        Case Else:  LogNTEvent Now & "Error: " & Err & ": " & Err.Description & ": UpdateStunden", EVENTLOG_ERROR_TYPE, 1000, "Error: " & Err.Number
    End Select
    Resume ExitPoint
End Sub

这通常可以正常工作。

但如果我将多个记录复制并粘贴到子表单中,则会出现错误。当我将多个记录从一个子表单复制并粘贴到同一个子表单但在另一个主表单记录上时,计算错误。

这应该是标准的,因为我在任何地方都使用它。

我们需要一个After Paste事件!

有谁知道怎么做?

问候理查德

2 个答案:

答案 0 :(得分:1)

只需从子表单中将主表单值设置为null:

Private Sub Form_AfterDelConfirm(Status As Integer)
    Me.Parent.Stunden = Null
End Sub

Private Sub Form_AfterInsert()
    Me.Parent.Stunden = Null
End Sub

Private Sub Form_AfterUpdate() 
    Me.Parent.Stunden = Null 
End Sub

然后使用计时器:

Private Sub Form_Timer()
    If IsNull(Me.Stunden) Then UpdateStunden
End Sub

Private Sub Stunden_AfterUpdate()
    Me.Dirty = False
End Sub

Private Sub Form_BeforeUpdate(Cancel As Integer)
    UpdateStunden True
End Sub

Public Sub UpdateStunden(Optional BeforeUpdateEvent As Boolean = False)
On Error GoTo ErrorHandler
Dim rst As Recordset
Dim sql As String
Dim NewStunden As Variant

    If Me.NewRecord Then Exit Sub

    sql = _
    "SELECT Sum(Stunden) AS SumStunden " & _
    "FROM Tätigkeiten " & _
    "WHERE Tätigkeitsdatum = #" & Format(Me!Tätigkeitsdatum, "yyyy-mm-dd") & "#;"

    Set rst = CurrentDb().OpenRecordset(sql, dbOpenSnapshot)

    If Not rst.EOF Or Not rst.BOF Then
        NewStunden = rst!SumStunden
        If Nz(NewStunden) <> Nz(Me.Stunden) Or IsNull(Me.Stunden) Then
            Me.Stunden = NewStunden
        End If
    End If

ExitPoint:
    On Error Resume Next
    If Me.Dirty And Not BeforeUpdateEvent Then Me.Dirty = False
    rst.Close
    Set rst = Nothing
    Exit Sub

ErrorHandler:
    Select Case Err
        Case Else:  LogNTEvent Now & "Error: " & Err & ": " & Err.Description & ": UpdateStunden", EVENTLOG_ERROR_TYPE, 1000, "Error: " & Err.Number
    End Select
    Resume ExitPoint
End Sub

答案 1 :(得分:1)

只需将主窗体上Textbox-Control的Control-Source设置为:

=DomSumme("Stunden";"Tätigkeiten";"Tätigkeitsdatum = #" & Format([Tätigkeitsdatum];"jjjj-mm-tt") & "#")

然后在所有三个事件之后重新查询:

Private Sub Form_AfterDelConfirm(Status As Integer)
    Me.Parent!Test.Requery
End Sub

Private Sub Form_AfterInsert()
    Me.Parent!Test.Requery
End Sub

Private Sub Form_AfterUpdate()
    Me.Parent!Test.Requery
End Sub

N.B。德国办事处2010