Recordset将新记录添加到子表单中,但在保存时没有添加到表中

时间:2014-11-05 12:49:08

标签: vba access-vba

您好我正在通过recordsetclone方法将新记录添加到子表单中。问题是新记录出现在表单中,但在保存时它不会出现在表中。如果手动添加此记录一切正常。如果手动添加记录if语句的更新部分工作正常。

这是我的代码。

'Add Wastage value to flooring area section
Private Sub Wastage_AfterUpdate()
    Dim rsFlArea As DAO.Recordset
    Dim Wastage As Double
    Dim Item As String
    Set rsFlArea = Me.OrderFloorAreaEdit.Form.RecordsetClone
    'Caluate Wastage amount based on the percentage in parent form
    Wastage = Format((Me.FloorMeterage.Value * Me.Wastage.Value), "#,##0.00")

    'Check if it has been already added if not than add - note 12 is ID of floor area
    rsFlArea.FindFirst "[Item] LIKE '12'"
    If rsFlArea.NoMatch Then
            rsFlArea.AddNew
            rsFlArea!Item = 12
            rsFlArea!Units = Wastage
            rsFlArea!uPrice = Me.flPrice.Value
            rsFlArea.Update
        Else
        ' If value exist in recordset than update the value
        Do Until rsFlArea.EOF
            If rsFlArea!Item = 12 Then
                rsFlArea.Edit
                rsFlArea!Units = Format(((Me.FloorMeterage.Value - Nz(rsFlArea!Units, 0)) * Me.Wastage.Value), "#,##0.00")
                rsFlArea.Update
            End If
            rsFlArea.MoveNext
        Loop
    End If

    rsFlArea.Close
    Set rsFlArea = Nothing
End Sub

1 个答案:

答案 0 :(得分:0)

问题是我已将子窗体查询链接到OrderID并忘记添加它,因此附加字段修复了它。所以它添加了新的字段,但没有OrderID,这就是为什么它没有显示在子窗体中,因为它被过滤掉了。

'Add Wastage value to flooring area section
Private Sub Wastage_AfterUpdate()
    Dim rsFlArea As DAO.Recordset
    Dim Wastage As Double
    Dim Item As String
    Set rsFlArea = Me.OrderFloorAreaEdit.Form.RecordsetClone
    'Caluate Wastage amount based on the percentage in parent form
    Wastage = Format((Me.FloorMeterage.Value * Me.Wastage.Value), "#,##0.00")

    'Check if it has been already added if not than add - note 12 is ID of floor area
    rsFlArea.FindFirst "[Item] LIKE '12'"
    If rsFlArea.NoMatch Then
            rsFlArea.AddNew
            rsFlArea!OrderID = Me.OrderID.Value
            rsFlArea!Item = 12
            rsFlArea!Units = Wastage
            rsFlArea!uPrice = Me.flPrice.Value
            rsFlArea.Update
        Else
        ' If value exist in recordset than update the value
        Do Until rsFlArea.EOF
            If rsFlArea!Item = 12 Then
                rsFlArea.Edit
                rsFlArea!Units = Format(((Me.FloorMeterage.Value - Nz(rsFlArea!Units, 0)) * Me.Wastage.Value), "#,##0.00")
                rsFlArea.Update
            End If
            rsFlArea.MoveNext
        Loop
    End If

    rsFlArea.Close
    Set rsFlArea = Nothing
End Sub